Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 1 | # 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 Ronquillo | b626884 | 2017-10-03 13:02:58 -0700 | [diff] [blame] | 21 | # please contact Jeremy Ronquillo: j_ronquillo@u.pacific.edu |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 22 | |
| 23 | # ********************************************************** |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 24 | # STEP 1: Data management. |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 25 | # ********************************************************** |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 26 | print( "**********************************************************" ) |
| 27 | print( "STEP 1: Data management." ) |
| 28 | print( "**********************************************************" ) |
Devin Lim | 0e96716 | 2017-11-03 15:59:53 -0700 | [diff] [blame] | 29 | database_host = 1 |
| 30 | database_port = 2 |
| 31 | database_u_id = 3 |
| 32 | database_pw = 4 |
| 33 | test_name = 5 |
| 34 | branch_name = 6 |
| 35 | save_directory = 7 |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 36 | |
Jeremy Ronquillo | b626884 | 2017-10-03 13:02:58 -0700 | [diff] [blame] | 37 | # Command line arguments are read. |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 38 | print( "Reading commmand-line args." ) |
| 39 | args <- commandArgs( trailingOnly=TRUE ) |
| 40 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 41 | # ---------------- |
| 42 | # Import Libraries |
| 43 | # ---------------- |
| 44 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 45 | print( "Importing libraries." ) |
| 46 | library( ggplot2 ) |
| 47 | library( reshape2 ) |
| 48 | library( RPostgreSQL ) # For databases |
| 49 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 50 | # ------------------- |
| 51 | # Check CLI Arguments |
| 52 | # ------------------- |
| 53 | |
| 54 | print( "Verifying CLI args." ) |
| 55 | |
Devin Lim | 0e96716 | 2017-11-03 15:59:53 -0700 | [diff] [blame] | 56 | if ( is.na( args[ save_directory ] ) ){ |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 57 | |
| 58 | print( paste( "Usage: Rscript SCPFgraphGenerator", |
| 59 | "<database-host>", |
| 60 | "<database-port>", |
| 61 | "<database-user-id>", |
| 62 | "<database-password>", |
| 63 | "<test-name>", |
| 64 | "<branch-name>", |
| 65 | "<directory-to-save-graphs>", |
| 66 | sep=" ") ) |
| 67 | |
Jeremy Ronquillo | 9ea85d0 | 2017-11-27 10:21:03 -0800 | [diff] [blame] | 68 | quit( status = 1 ) # basically exit(), but in R |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 71 | # ----------------- |
| 72 | # Create File Names |
| 73 | # ----------------- |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 74 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 75 | print( "Creating filenames and title of graph." ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 76 | |
Devin Lim | 0e96716 | 2017-11-03 15:59:53 -0700 | [diff] [blame] | 77 | outputFile <- paste( args[ save_directory ], |
| 78 | args[ test_name ], |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 79 | "_", |
Devin Lim | 0e96716 | 2017-11-03 15:59:53 -0700 | [diff] [blame] | 80 | args[ branch_name ], |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 81 | "_graph.jpg", |
| 82 | sep="" ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 83 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 84 | chartTitle <- "Scale Topology Latency Test" |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 85 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 86 | # ------------------ |
| 87 | # SQL Initialization |
| 88 | # ------------------ |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 89 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 90 | print( "Initializing SQL" ) |
| 91 | |
| 92 | con <- dbConnect( dbDriver( "PostgreSQL" ), |
| 93 | dbname = "onostest", |
Devin Lim | 0e96716 | 2017-11-03 15:59:53 -0700 | [diff] [blame] | 94 | host = args[ database_host ], |
| 95 | port = strtoi( args[ database_port ] ), |
| 96 | user = args[ database_u_id ], |
| 97 | password = args[ database_pw ] ) |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 98 | |
| 99 | # -------------------------- |
| 100 | # Scale Topology SQL Command |
| 101 | # -------------------------- |
| 102 | |
| 103 | print( "Generating Scale Topology SQL Command" ) |
| 104 | |
| 105 | command <- paste( "SELECT * FROM scale_topo_latency_details WHERE branch = '", |
Devin Lim | 0e96716 | 2017-11-03 15:59:53 -0700 | [diff] [blame] | 106 | args[ branch_name ], |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 107 | "' AND date IN ( SELECT MAX( date ) FROM scale_topo_latency_details WHERE branch = '", |
Devin Lim | 0e96716 | 2017-11-03 15:59:53 -0700 | [diff] [blame] | 108 | args[ branch_name ], |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 109 | "' ) ", |
| 110 | sep = "" ) |
| 111 | |
| 112 | print( "Sending SQL command:" ) |
| 113 | print( command ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 114 | fileData <- dbGetQuery( con, command ) |
| 115 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 116 | # ********************************************************** |
| 117 | # STEP 2: Organize data. |
| 118 | # ********************************************************** |
| 119 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 120 | print( "**********************************************************" ) |
| 121 | print( "STEP 2: Organize Data." ) |
| 122 | print( "**********************************************************" ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 123 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 124 | # ------------ |
| 125 | # Data Sorting |
| 126 | # ------------ |
| 127 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 128 | print( "Sorting data." ) |
Jeremy Ronquillo | 9ea85d0 | 2017-11-27 10:21:03 -0800 | [diff] [blame] | 129 | |
| 130 | requiredColumns <- c( "last_role_request_to_last_topology", "last_connection_to_last_role_request", "first_connection_to_last_connection" ) |
| 131 | |
| 132 | tryCatch( avgs <- c( fileData[ requiredColumns] ), |
| 133 | error = function( e ) { |
| 134 | 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." ) |
| 135 | print( "Required columns: " ) |
| 136 | print( requiredColumns ) |
| 137 | print( "Actual columns: " ) |
| 138 | print( names( fileData ) ) |
| 139 | print( "Error dump:" ) |
| 140 | print( e ) |
| 141 | quit( status = 1 ) |
| 142 | } |
| 143 | ) |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 144 | |
| 145 | # -------------------- |
| 146 | # Construct Data Frame |
| 147 | # -------------------- |
| 148 | |
| 149 | print( "Constructing Data Frame" ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 150 | |
| 151 | # Parse lists into data frames. |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 152 | dataFrame <- melt( avgs ) |
| 153 | dataFrame$scale <- fileData$scale |
| 154 | colnames( dataFrame ) <- c( "s", |
| 155 | "type", |
| 156 | "scale") |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 157 | |
| 158 | # Format data frame so that the data is in the same order as it appeared in the file. |
| 159 | dataFrame$type <- as.character( dataFrame$type ) |
| 160 | dataFrame$type <- factor( dataFrame$type, levels=unique( dataFrame$type ) ) |
| 161 | dataFrame$iterative <- seq( 1, nrow( fileData ), by = 1 ) |
| 162 | |
Jeremy Ronquillo | 2d2649d | 2017-09-14 12:53:06 -0700 | [diff] [blame] | 163 | dataFrame <- na.omit( dataFrame ) # Omit any data that doesn't exist |
| 164 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 165 | sum <- fileData[ 'last_role_request_to_last_topology' ] + |
| 166 | fileData[ 'last_connection_to_last_role_request' ] + |
| 167 | fileData[ 'first_connection_to_last_connection' ] |
Jeremy Ronquillo | 4363d09 | 2017-10-13 13:28:47 -0700 | [diff] [blame] | 168 | |
Jeremy Ronquillo | 2d2649d | 2017-09-14 12:53:06 -0700 | [diff] [blame] | 169 | print( "Data Frame Results:" ) |
| 170 | print( dataFrame ) |
| 171 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 172 | # ********************************************************** |
| 173 | # STEP 3: Generate graphs. |
| 174 | # ********************************************************** |
| 175 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 176 | print( "**********************************************************" ) |
| 177 | print( "STEP 3: Generate Graph." ) |
| 178 | print( "**********************************************************" ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 179 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 180 | # ------------------ |
| 181 | # Generate Main Plot |
| 182 | # ------------------ |
| 183 | |
| 184 | print( "Creating main plot." ) |
| 185 | |
| 186 | mainPlot <- ggplot( data = dataFrame, aes( x = iterative, |
| 187 | y = s, |
| 188 | fill = type ) ) |
| 189 | |
| 190 | # ------------------------------ |
| 191 | # Fundamental Variables Assigned |
| 192 | # ------------------------------ |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 193 | |
| 194 | print( "Generating fundamental graph data." ) |
| 195 | |
Jeremy Ronquillo | 2d2649d | 2017-09-14 12:53:06 -0700 | [diff] [blame] | 196 | theme_set( theme_grey( base_size = 20 ) ) # set the default text size of the graph. |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 197 | width <- 0.6 # Width of the bars. |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 198 | xScaleConfig <- scale_x_continuous( breaks = dataFrame$iterative, |
| 199 | label = dataFrame$scale ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 200 | xLabel <- xlab( "Scale" ) |
Devin Lim | 3c2b9d6 | 2017-10-25 11:41:41 -0700 | [diff] [blame] | 201 | yLabel <- ylab( "Latency (s)" ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 202 | fillLabel <- labs( fill="Type" ) |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 203 | imageWidth <- 15 |
| 204 | imageHeight <- 10 |
| 205 | imageDPI <- 200 |
| 206 | |
| 207 | theme <- theme( plot.title = element_text( hjust = 0.5, size = 32, face = 'bold' ), |
| 208 | legend.position = "bottom", |
| 209 | legend.text = element_text( size=22 ), |
| 210 | legend.title = element_blank(), |
Jeremy Ronquillo | 94f99dd | 2018-01-05 11:11:27 -0800 | [diff] [blame^] | 211 | legend.key.size = unit( 1.5, 'lines' ), |
| 212 | plot.subtitle = element_text( size=16, hjust=1.0 ) ) |
| 213 | |
| 214 | subtitle <- paste( "Last Updated: ", format( Sys.time(), format = "%b %d, %Y at %I:%M %p %Z" ), sep="" ) |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 215 | |
| 216 | values <- geom_text( aes( x = dataFrame$iterative, |
| 217 | y = sum + 0.02 * max( sum ), |
| 218 | label = format( sum, |
| 219 | big.mark = ",", |
| 220 | scientific = FALSE ), |
| 221 | fontface = "bold" ), |
| 222 | size = 7.0 ) |
| 223 | |
| 224 | wrapLegend <- guides( fill = guide_legend( nrow=2, byrow=TRUE ) ) |
| 225 | |
Jeremy Ronquillo | 94f99dd | 2018-01-05 11:11:27 -0800 | [diff] [blame^] | 226 | title <- labs( title = chartTitle, subtitle = subtitle ) |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 227 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 228 | # Store plot configurations as 1 variable |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 229 | fundamentalGraphData <- mainPlot + |
| 230 | xScaleConfig + |
| 231 | xLabel + |
| 232 | yLabel + |
| 233 | fillLabel + |
| 234 | theme + |
| 235 | values + |
| 236 | wrapLegend + |
| 237 | title |
| 238 | |
| 239 | # --------------------------- |
| 240 | # Generating Bar Graph Format |
| 241 | # --------------------------- |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 242 | |
Jeremy Ronquillo | 4363d09 | 2017-10-13 13:28:47 -0700 | [diff] [blame] | 243 | print( "Generating bar graph." ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 244 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 245 | barGraphFormat <- geom_bar( stat = "identity", width = width ) |
| 246 | |
| 247 | result <- fundamentalGraphData + |
| 248 | barGraphFormat |
| 249 | |
| 250 | # ----------------------- |
| 251 | # Exporting Graph to File |
| 252 | # ----------------------- |
| 253 | |
Jeremy Ronquillo | 4363d09 | 2017-10-13 13:28:47 -0700 | [diff] [blame] | 254 | print( paste( "Saving bar chart to", outputFile ) ) |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 255 | |
Jeremy Ronquillo | 9ea85d0 | 2017-11-27 10:21:03 -0800 | [diff] [blame] | 256 | tryCatch( ggsave( outputFile, |
| 257 | width = imageWidth, |
| 258 | height = imageHeight, |
| 259 | dpi = imageDPI ), |
| 260 | error = function( e ){ |
| 261 | print( "[ERROR] There was a problem saving the graph due to a graph formatting exception. Error dump:" ) |
| 262 | print( e ) |
| 263 | quit( status = 1 ) |
| 264 | } |
| 265 | ) |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 266 | |
| 267 | print( paste( "[SUCCESS] Successfully wrote bar chart out to", outputFile ) ) |
Jeremy Ronquillo | 9ea85d0 | 2017-11-27 10:21:03 -0800 | [diff] [blame] | 268 | quit( status = 0 ) |