blob: 9a61592e4a327b5a7c8d445635881971206d4e10 [file] [log] [blame]
Jeremy Ronquillo6df87812017-08-28 16:17:36 +00001# Copyright 2017 Open Networking Foundation (ONF)
2#
3# Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
4# the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
5# or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg>
6#
7# TestON is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 2 of the License, or
10# (at your option) any later version.
11#
12# TestON is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with TestON. If not, see <http://www.gnu.org/licenses/>.
19#
20# If you have any questions, or if you don't understand R,
Jeremy Ronquillob6268842017-10-03 13:02:58 -070021# please contact Jeremy Ronquillo: j_ronquillo@u.pacific.edu
Jeremy Ronquillo6df87812017-08-28 16:17:36 +000022
23# **********************************************************
Jeremy Ronquillo7673f802017-10-30 09:42:44 -070024# STEP 1: Data management.
Jeremy Ronquillo6df87812017-08-28 16:17:36 +000025# **********************************************************
26
Jeremy Ronquillo7673f802017-10-30 09:42:44 -070027print( "**********************************************************" )
28print( "STEP 1: Data management." )
29print( "**********************************************************" )
Devin Lim0e967162017-11-03 15:59:53 -070030database_host = 1
31database_port = 2
32database_u_id = 3
33database_pw = 4
34test_name = 5
35branch_name = 6
36save_directory = 7
Jeremy Ronquillo6df87812017-08-28 16:17:36 +000037
Jeremy Ronquillob6268842017-10-03 13:02:58 -070038# Command line arguments are read.
Jeremy Ronquillo6df87812017-08-28 16:17:36 +000039print( "Reading commmand-line args." )
40args <- commandArgs( trailingOnly=TRUE )
41
Jeremy Ronquillo7673f802017-10-30 09:42:44 -070042# ----------------
43# Import Libraries
44# ----------------
45
Jeremy Ronquillo6df87812017-08-28 16:17:36 +000046print( "Importing libraries." )
47library( ggplot2 )
48library( reshape2 )
49library( RPostgreSQL ) # For databases
50
Jeremy Ronquillo7673f802017-10-30 09:42:44 -070051# -------------------
52# Check CLI Arguments
53# -------------------
54
55print( "Verifying CLI args." )
Devin Lim0e967162017-11-03 15:59:53 -070056if ( is.na( args[ save_directory ] ) ){
Jeremy Ronquillo7673f802017-10-30 09:42:44 -070057 print( paste( "Usage: Rscript SCPFmastershipFailoverLat",
58 "<database-host>",
59 "<database-port>",
60 "<database-user-id>",
61 "<database-password>",
62 "<test-name>",
63 "<branch-name>",
64 "<directory-to-save-graphs>",
65 sep=" " ) )
66
Jeremy Ronquillo9ea85d02017-11-27 10:21:03 -080067 quit( status = 1 ) # basically exit(), but in R
Jeremy Ronquillo6df87812017-08-28 16:17:36 +000068}
69
Jeremy Ronquillo7673f802017-10-30 09:42:44 -070070# -----------------
71# Create File Names
72# -----------------
Jeremy Ronquillo6df87812017-08-28 16:17:36 +000073
Jeremy Ronquillo7673f802017-10-30 09:42:44 -070074print( "Creating filenames and title of graph." )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +000075
76chartTitle <- "Mastership Failover Latency"
77
Devin Lim0e967162017-11-03 15:59:53 -070078errBarOutputFile <- paste( args[ save_directory ],
79 args[ test_name ],
Jeremy Ronquillo7673f802017-10-30 09:42:44 -070080 "_",
Devin Lim0e967162017-11-03 15:59:53 -070081 args[ branch_name ],
Jeremy Ronquillo7673f802017-10-30 09:42:44 -070082 "_errGraph.jpg",
83 sep="" )
84
Devin Lim0e967162017-11-03 15:59:53 -070085stackedBarOutputFile <- paste( args[ save_directory ],
86 args[ test_name ],
Jeremy Ronquillo7673f802017-10-30 09:42:44 -070087 "_",
Devin Lim0e967162017-11-03 15:59:53 -070088 args[ branch_name ],
Jeremy Ronquillo7673f802017-10-30 09:42:44 -070089 "_stackedGraph.jpg",
90 sep="" )
91
92# ------------------
93# SQL Initialization
94# ------------------
95
96print( "Initializing SQL" )
97
98con <- dbConnect( dbDriver( "PostgreSQL" ),
99 dbname = "onostest",
Devin Lim0e967162017-11-03 15:59:53 -0700100 host = args[ database_host ],
101 port = strtoi( args[ database_port ] ),
102 user = args[ database_u_id ],
103 password = args[ database_pw ] )
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700104
105# ---------------------------------------
106# Mastership Failover Latency SQL Command
107# ---------------------------------------
108
109print( "Generating Mastership Failover Latency SQL command" )
110
111command <- paste( "SELECT * FROM mastership_failover_tests WHERE branch = '",
Devin Lim0e967162017-11-03 15:59:53 -0700112 args[ branch_name ],
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700113 "' AND date IN ( SELECT MAX( date ) FROM mastership_failover_tests WHERE branch = '",
Devin Lim0e967162017-11-03 15:59:53 -0700114 args[ branch_name ],
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700115 "' ) ",
116 sep = "" )
117
118print( "Sending SQL command:" )
119print( command )
120
121fileData <- dbGetQuery( con, command )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000122
123# **********************************************************
124# STEP 2: Organize data.
125# **********************************************************
126
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700127print( "**********************************************************" )
128print( "STEP 2: Organize Data." )
129print( "**********************************************************" )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000130
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700131# ------------
132# Data Sorting
133# ------------
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000134
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700135print( "Combining averages into a list." )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000136
Jeremy Ronquillo9ea85d02017-11-27 10:21:03 -0800137requiredColumns <- c( "kill_deact_avg", "deact_role_avg" )
138
139tryCatch( avgs <- c( fileData[ requiredColumns] ),
140 error = function( e ) {
141 print( "[ERROR] One or more expected columns are missing from the data. Please check that the data and SQL command are valid, then try again." )
142 print( "Required columns: " )
143 print( requiredColumns )
144 print( "Actual columns: " )
145 print( names( fileData ) )
146 print( "Error dump:" )
147 print( e )
148 quit( status = 1 )
149 }
150 )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000151
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700152# --------------------
153# Construct Data Frame
154# --------------------
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000155
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700156print( "Constructing Data Frame from list." )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000157
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700158dataFrame <- melt( avgs )
159dataFrame$scale <- fileData$scale
160dataFrame$stds <- c( fileData$kill_deact_std,
161 fileData$deact_role_std )
162
163colnames( dataFrame ) <- c( "ms",
164 "type",
165 "scale",
166 "stds" )
167
168dataFrame <- na.omit( dataFrame ) # Omit any data that doesn't exist
169
170sum <- fileData[ 'deact_role_avg' ] +
171 fileData[ 'kill_deact_avg' ]
Jeremy Ronquillo2d2649d2017-09-14 12:53:06 -0700172
173print( "Data Frame Results:" )
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700174print( dataFrame )
Jeremy Ronquillo2d2649d2017-09-14 12:53:06 -0700175
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000176
177# **********************************************************
178# STEP 3: Generate graphs.
179# **********************************************************
180
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700181print( "**********************************************************" )
182print( "STEP 3: Generate Graph." )
183print( "**********************************************************" )
184
185# ------------------------------------
186# Initialize Variables for Both Graphs
187# ------------------------------------
188
189print( "Initializing variables used in both graphs." )
Jeremy Ronquillo2d2649d2017-09-14 12:53:06 -0700190
Jeremy Ronquillo0e27b912017-10-21 14:34:24 -0700191theme_set( theme_grey( base_size = 22 ) ) # set the default text size of the graph.
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700192xScaleConfig <- scale_x_continuous( breaks = c( 1, 3, 5, 7, 9) )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000193xLabel <- xlab( "Scale" )
194yLabel <- ylab( "Latency (ms)" )
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700195fillLabel <- labs( fill = "Type" )
196barWidth <- 0.9
197imageWidth <- 15
198imageHeight <- 10
199imageDPI <- 200
200
201theme <- theme( plot.title = element_text( hjust = 0.5, size = 32, face='bold' ),
202 legend.position = "bottom",
203 legend.text = element_text( size=22 ),
204 legend.title = element_blank(),
Jeremy Ronquillo94f99dd2018-01-05 11:11:27 -0800205 legend.key.size = unit( 1.5, 'lines' ),
206 plot.subtitle = element_text( size=16, hjust=1.0 ) )
207
208subtitle <- paste( "Last Updated: ", format( Sys.time(), format = "%b %d, %Y at %I:%M %p %Z" ), sep="" )
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700209
210barColors <- scale_fill_manual( values=c( "#F77670",
Jeremy Ronquillo9ea85d02017-11-27 10:21:03 -0800211 "#619DFA" ) )
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700212
Jeremy Ronquillo0e27b912017-10-21 14:34:24 -0700213wrapLegend <- guides( fill=guide_legend( nrow=1, byrow=TRUE ) )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000214
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700215# ----------------------------------
216# Error Bar Graph Generate Main Plot
217# ----------------------------------
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000218
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700219print( "Creating main plot." )
220
221mainPlot <- ggplot( data = dataFrame, aes( x = scale,
222 y = ms,
223 ymin = ms,
224 ymax = ms + stds,
225 fill = type ) )
226
227# ----------------------------------------------
228# Error Bar Graph Fundamental Variables Assigned
229# ----------------------------------------------
230
231print( "Generating fundamental graph data for the error bar graph." )
232
233errorBarColor <- rgb( 140, 140, 140, maxColorValue=255 )
234
Jeremy Ronquillo94f99dd2018-01-05 11:11:27 -0800235title <- labs( title = chartTitle, subtitle = subtitle )
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700236
237fundamentalGraphData <- mainPlot +
238 xScaleConfig +
239 xLabel +
240 yLabel +
241 fillLabel +
242 theme +
243 title +
244 wrapLegend
245
246# -------------------------------------------
247# Error Bar Graph Generating Bar Graph Format
248# -------------------------------------------
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000249
250print( "Generating bar graph with error bars." )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000251
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700252barGraphFormat <- geom_bar( stat = "identity",
253 position = position_dodge(),
254 width = barWidth )
255
256errorBarFormat <- geom_errorbar( width = barWidth,
257 position = position_dodge(),
258 color = errorBarColor )
259
260values <- geom_text( aes( x = dataFrame$scale,
261 y = dataFrame$ms + 0.02 * max( dataFrame$ms ),
262 label = format( dataFrame$ms,
263 digits = 3,
264 big.mark = ",",
265 scientific = FALSE ) ),
266 size = 7.0,
267 fontface = "bold",
268 position = position_dodge( 0.9 ) )
269
270result <- fundamentalGraphData +
271 barGraphFormat +
272 barColors +
273 errorBarFormat +
274 values
275
276# ---------------------------------------
277# Error Bar Graph Exporting Graph to File
278# ---------------------------------------
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000279
280print( paste( "Saving bar chart with error bars to", errBarOutputFile ) )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000281
Jeremy Ronquillo9ea85d02017-11-27 10:21:03 -0800282tryCatch( ggsave( errBarOutputFile,
283 width = imageWidth,
284 height = imageHeight,
285 dpi = imageDPI ),
286 error = function( e ){
287 print( "[ERROR] There was a problem saving the graph due to a graph formatting exception. Error dump:" )
288 print( e )
289 quit( status = 1 )
290 }
291 )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000292
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700293print( paste( "[SUCCESS] Successfully wrote bar chart with error bars out to", errBarOutputFile ) )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000294
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700295# ------------------------------------------------
296# Stacked Bar Graph Fundamental Variables Assigned
297# ------------------------------------------------
298
299print( "Generating fundamental graph data for the stacked bar graph." )
300
Jeremy Ronquillo94f99dd2018-01-05 11:11:27 -0800301title <- labs( title = chartTitle, subtitle = subtitle )
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700302
303fundamentalGraphData <- mainPlot +
304 xScaleConfig +
305 xLabel +
306 yLabel +
307 fillLabel +
308 theme +
309 title +
310 wrapLegend
311
312# ---------------------------------------------
313# Stacked Bar Graph Generating Bar Graph Format
314# ---------------------------------------------
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000315
316print( "Generating stacked bar chart." )
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700317stackedBarFormat <- geom_bar( stat = "identity",
318 width = barWidth )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000319
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700320values <- geom_text( aes( x = dataFrame$scale,
321 y = sum + 0.02 * max( sum ),
322 label = format( sum,
323 digits = 3,
324 big.mark = ",",
325 scientific = FALSE ) ),
326 size = 7.0,
327 fontface = "bold" )
328
329result <- fundamentalGraphData +
330 stackedBarFormat +
331 barColors +
332 title +
333 values
334
335# -----------------------------------------
336# Stacked Bar Graph Exporting Graph to File
337# -----------------------------------------
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000338
339print( paste( "Saving stacked bar chart to", stackedBarOutputFile ) )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000340
Jeremy Ronquillo9ea85d02017-11-27 10:21:03 -0800341tryCatch( ggsave( stackedBarOutputFile,
342 width = imageWidth,
343 height = imageHeight,
344 dpi = imageDPI ),
345 error = function( e ){
346 print( "[ERROR] There was a problem saving the graph due to a graph formatting exception. Error dump:" )
347 print( e )
348 quit( status = 1 )
349 }
350 )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000351
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700352print( paste( "[SUCCESS] Successfully wrote stacked bar chart out to", stackedBarOutputFile ) )
Jeremy Ronquillo9ea85d02017-11-27 10:21:03 -0800353quit( status = 0 )