blob: 30f7bcadf4ffd1db55b4f5c164a6edc600988967 [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(),
205 legend.key.size = unit( 1.5, 'lines' ) )
206
207barColors <- scale_fill_manual( values=c( "#F77670",
Jeremy Ronquillo9ea85d02017-11-27 10:21:03 -0800208 "#619DFA" ) )
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700209
Jeremy Ronquillo0e27b912017-10-21 14:34:24 -0700210wrapLegend <- guides( fill=guide_legend( nrow=1, byrow=TRUE ) )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000211
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700212# ----------------------------------
213# Error Bar Graph Generate Main Plot
214# ----------------------------------
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000215
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700216print( "Creating main plot." )
217
218mainPlot <- ggplot( data = dataFrame, aes( x = scale,
219 y = ms,
220 ymin = ms,
221 ymax = ms + stds,
222 fill = type ) )
223
224# ----------------------------------------------
225# Error Bar Graph Fundamental Variables Assigned
226# ----------------------------------------------
227
228print( "Generating fundamental graph data for the error bar graph." )
229
230errorBarColor <- rgb( 140, 140, 140, maxColorValue=255 )
231
232title <- ggtitle( chartTitle )
233
234fundamentalGraphData <- mainPlot +
235 xScaleConfig +
236 xLabel +
237 yLabel +
238 fillLabel +
239 theme +
240 title +
241 wrapLegend
242
243# -------------------------------------------
244# Error Bar Graph Generating Bar Graph Format
245# -------------------------------------------
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000246
247print( "Generating bar graph with error bars." )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000248
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700249barGraphFormat <- geom_bar( stat = "identity",
250 position = position_dodge(),
251 width = barWidth )
252
253errorBarFormat <- geom_errorbar( width = barWidth,
254 position = position_dodge(),
255 color = errorBarColor )
256
257values <- geom_text( aes( x = dataFrame$scale,
258 y = dataFrame$ms + 0.02 * max( dataFrame$ms ),
259 label = format( dataFrame$ms,
260 digits = 3,
261 big.mark = ",",
262 scientific = FALSE ) ),
263 size = 7.0,
264 fontface = "bold",
265 position = position_dodge( 0.9 ) )
266
267result <- fundamentalGraphData +
268 barGraphFormat +
269 barColors +
270 errorBarFormat +
271 values
272
273# ---------------------------------------
274# Error Bar Graph Exporting Graph to File
275# ---------------------------------------
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000276
277print( paste( "Saving bar chart with error bars to", errBarOutputFile ) )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000278
Jeremy Ronquillo9ea85d02017-11-27 10:21:03 -0800279tryCatch( ggsave( errBarOutputFile,
280 width = imageWidth,
281 height = imageHeight,
282 dpi = imageDPI ),
283 error = function( e ){
284 print( "[ERROR] There was a problem saving the graph due to a graph formatting exception. Error dump:" )
285 print( e )
286 quit( status = 1 )
287 }
288 )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000289
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700290print( paste( "[SUCCESS] Successfully wrote bar chart with error bars out to", errBarOutputFile ) )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000291
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700292# ------------------------------------------------
293# Stacked Bar Graph Fundamental Variables Assigned
294# ------------------------------------------------
295
296print( "Generating fundamental graph data for the stacked bar graph." )
297
298title <- ggtitle( chartTitle )
299
300fundamentalGraphData <- mainPlot +
301 xScaleConfig +
302 xLabel +
303 yLabel +
304 fillLabel +
305 theme +
306 title +
307 wrapLegend
308
309# ---------------------------------------------
310# Stacked Bar Graph Generating Bar Graph Format
311# ---------------------------------------------
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000312
313print( "Generating stacked bar chart." )
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700314stackedBarFormat <- geom_bar( stat = "identity",
315 width = barWidth )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000316
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700317values <- geom_text( aes( x = dataFrame$scale,
318 y = sum + 0.02 * max( sum ),
319 label = format( sum,
320 digits = 3,
321 big.mark = ",",
322 scientific = FALSE ) ),
323 size = 7.0,
324 fontface = "bold" )
325
326result <- fundamentalGraphData +
327 stackedBarFormat +
328 barColors +
329 title +
330 values
331
332# -----------------------------------------
333# Stacked Bar Graph Exporting Graph to File
334# -----------------------------------------
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000335
336print( paste( "Saving stacked bar chart to", stackedBarOutputFile ) )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000337
Jeremy Ronquillo9ea85d02017-11-27 10:21:03 -0800338tryCatch( ggsave( stackedBarOutputFile,
339 width = imageWidth,
340 height = imageHeight,
341 dpi = imageDPI ),
342 error = function( e ){
343 print( "[ERROR] There was a problem saving the graph due to a graph formatting exception. Error dump:" )
344 print( e )
345 quit( status = 1 )
346 }
347 )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000348
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700349print( paste( "[SUCCESS] Successfully wrote stacked bar chart out to", stackedBarOutputFile ) )
Jeremy Ronquillo9ea85d02017-11-27 10:21:03 -0800350quit( status = 0 )