Jeremy Ronquillo | dae1104 | 2018-02-21 09:21:44 -0800 | [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 | # Example script: |
| 21 | # Switch Latency Graph (https://jenkins.onosproject.org/view/QA/job/postjob-BM/lastSuccessfulBuild/artifact/SCPFswitchLat_master_UpErrBarWithStack.jpg): |
| 22 | # Rscript SCPFspecificGraphRScripts/SCPFswitchLat.R <url> <port> <username> <pass> SCPFswitchLat master /path/to/save/directory/ |
| 23 | |
| 24 | # ********************************************************** |
| 25 | # STEP 1: Data management. |
| 26 | # ********************************************************** |
| 27 | |
| 28 | print( "**********************************************************" ) |
| 29 | print( "STEP 1: Data management." ) |
| 30 | print( "**********************************************************" ) |
| 31 | |
| 32 | save_directory <- 7 |
| 33 | |
| 34 | # Command line arguments are read. |
| 35 | print( "Reading commmand-line args." ) |
| 36 | args <- commandArgs( trailingOnly=TRUE ) |
| 37 | |
| 38 | # ---------------- |
| 39 | # Import Libraries |
| 40 | # ---------------- |
| 41 | |
| 42 | print( "Importing libraries." ) |
| 43 | library( ggplot2 ) |
| 44 | library( reshape2 ) |
| 45 | library( RPostgreSQL ) # For databases |
| 46 | source( "dependencies/saveGraph.R" ) |
| 47 | source( "dependencies/fundamentalGraphData.R" ) |
| 48 | source( "dependencies/initSQL.R" ) |
| 49 | source( "dependencies/cliArgs.R" ) |
| 50 | |
| 51 | # ------------------- |
| 52 | # Check CLI Arguments |
| 53 | # ------------------- |
| 54 | |
| 55 | print( "Verifying CLI args." ) |
| 56 | |
| 57 | if ( length( args ) != save_directory ){ |
| 58 | usage( "SCPFswitchLat.R" ) |
| 59 | quit( status = 1 ) |
| 60 | } |
| 61 | |
| 62 | # ----------------- |
| 63 | # Create File Names |
| 64 | # ----------------- |
| 65 | |
| 66 | print( "Creating filenames and title of graph." ) |
| 67 | |
| 68 | errBarOutputFileUp <- paste( args[ save_directory ], |
| 69 | "SCPFswitchLat_", |
| 70 | args[ branch_name ], |
| 71 | "_UpErrBarWithStack.jpg", |
| 72 | sep="" ) |
| 73 | |
| 74 | errBarOutputFileDown <- paste( args[ save_directory ], |
| 75 | "SCPFswitchLat_", |
| 76 | args[ branch_name ], |
| 77 | "_DownErrBarWithStack.jpg", |
| 78 | sep="" ) |
| 79 | # ------------------ |
| 80 | # SQL Initialization |
| 81 | # ------------------ |
| 82 | |
| 83 | print( "Initializing SQL" ) |
| 84 | |
| 85 | con <- initSQL( args[ database_host ], |
| 86 | args[ database_port ], |
| 87 | args[ database_u_id ], |
| 88 | args[ database_pw ] ) |
| 89 | |
| 90 | # -------------------------- |
| 91 | # Switch Latency SQL Command |
| 92 | # -------------------------- |
| 93 | |
| 94 | print( "Generating Switch Latency SQL Command" ) |
| 95 | |
| 96 | command <- paste( "SELECT * FROM switch_latency_details WHERE branch = '", |
| 97 | args[ branch_name ], |
| 98 | "' AND date IN ( SELECT MAX( date ) FROM switch_latency_details WHERE branch='", |
| 99 | args[ branch_name ], |
| 100 | "' )", |
| 101 | sep="" ) |
| 102 | |
| 103 | fileData <- retrieveData( con, command ) |
| 104 | |
| 105 | # ********************************************************** |
| 106 | # STEP 2: Organize data. |
| 107 | # ********************************************************** |
| 108 | |
| 109 | print( "**********************************************************" ) |
| 110 | print( "STEP 2: Organize Data." ) |
| 111 | print( "**********************************************************" ) |
| 112 | |
| 113 | # ------------------------------- |
| 114 | # Switch Up Averages Data Sorting |
| 115 | # ------------------------------- |
| 116 | |
| 117 | print( "Sorting data for Switch Up Averages." ) |
| 118 | |
| 119 | requiredColumns <- c( "up_device_to_graph_avg", |
| 120 | "feature_reply_to_device_avg", |
| 121 | "tcp_to_feature_reply_avg" ) |
| 122 | |
| 123 | tryCatch( upAvgs <- c( fileData[ requiredColumns] ), |
| 124 | error = function( e ) { |
| 125 | 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." ) |
| 126 | print( "Required columns: " ) |
| 127 | print( requiredColumns ) |
| 128 | print( "Actual columns: " ) |
| 129 | print( names( fileData ) ) |
| 130 | print( "Error dump:" ) |
| 131 | print( e ) |
| 132 | quit( status = 1 ) |
| 133 | } |
| 134 | ) |
| 135 | |
| 136 | # ------------------------------ |
| 137 | # Switch Up Construct Data Frame |
| 138 | # ------------------------------ |
| 139 | |
| 140 | print( "Constructing Switch Up data frame." ) |
| 141 | |
| 142 | upAvgsData <- melt( upAvgs ) |
| 143 | upAvgsData$scale <- fileData$scale |
| 144 | upAvgsData$up_std <- fileData$up_std |
| 145 | upAvgsData <- na.omit( upAvgsData ) |
| 146 | |
| 147 | colnames( upAvgsData ) <- c( "ms", |
| 148 | "type", |
| 149 | "scale", |
| 150 | "stds" ) |
| 151 | |
| 152 | upAvgsData$type <- as.character( upAvgsData$type ) |
| 153 | upAvgsData$type <- factor( upAvgsData$type, levels=unique( upAvgsData$type ) ) |
| 154 | |
| 155 | sumOfUpAvgs <- fileData[ 'up_device_to_graph_avg' ] + |
| 156 | fileData[ 'feature_reply_to_device_avg' ] + |
| 157 | fileData[ 'tcp_to_feature_reply_avg' ] |
| 158 | |
| 159 | print( "Up Averages Results:" ) |
| 160 | print( upAvgsData ) |
| 161 | |
| 162 | # --------------------------------- |
| 163 | # Switch Down Averages Data Sorting |
| 164 | # --------------------------------- |
| 165 | |
| 166 | print( "Sorting data for Switch Down Averages." ) |
| 167 | |
| 168 | requiredColumns <- c( "down_device_to_graph_avg", |
| 169 | "ack_to_device_avg", |
| 170 | "fin_ack_to_ack_avg" ) |
| 171 | |
| 172 | tryCatch( downAvgs <- c( fileData[ requiredColumns] ), |
| 173 | error = function( e ) { |
| 174 | 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." ) |
| 175 | print( "Required columns: " ) |
| 176 | print( requiredColumns ) |
| 177 | print( "Actual columns: " ) |
| 178 | print( names( fileData ) ) |
| 179 | print( "Error dump:" ) |
| 180 | print( e ) |
| 181 | quit( status = 1 ) |
| 182 | } |
| 183 | ) |
| 184 | |
| 185 | # -------------------------------- |
| 186 | # Switch Down Construct Data Frame |
| 187 | # -------------------------------- |
| 188 | |
| 189 | print( "Constructing Switch Down data frame." ) |
| 190 | |
| 191 | downAvgsData <- melt( downAvgs ) |
| 192 | downAvgsData$scale <- fileData$scale |
| 193 | downAvgsData$down_std <- fileData$down_std |
| 194 | |
| 195 | colnames( downAvgsData ) <- c( "ms", |
| 196 | "type", |
| 197 | "scale", |
| 198 | "stds" ) |
| 199 | |
| 200 | downAvgsData$type <- as.character( downAvgsData$type ) |
| 201 | downAvgsData$type <- factor( downAvgsData$type, levels=unique( downAvgsData$type ) ) |
| 202 | |
| 203 | downAvgsData <- na.omit( downAvgsData ) |
| 204 | |
| 205 | sumOfDownAvgs <- fileData[ 'down_device_to_graph_avg' ] + |
| 206 | fileData[ 'ack_to_device_avg' ] + |
| 207 | fileData[ 'fin_ack_to_ack_avg' ] |
| 208 | |
| 209 | print( "Down Averages Results:" ) |
| 210 | print( downAvgsData ) |
| 211 | |
| 212 | # ********************************************************** |
| 213 | # STEP 3: Generate graphs. |
| 214 | # ********************************************************** |
| 215 | |
| 216 | print( "**********************************************************" ) |
| 217 | print( "STEP 3: Generate Graph." ) |
| 218 | print( "**********************************************************" ) |
| 219 | |
| 220 | # ------------------------------------ |
| 221 | # Initialize Variables For Both Graphs |
| 222 | # ------------------------------------ |
| 223 | |
| 224 | print( "Initializing variables used in both graphs." ) |
| 225 | |
| 226 | defaultTextSize() |
| 227 | xScaleConfig <- scale_x_continuous( breaks = c( 1, 3, 5, 7, 9 ) ) |
| 228 | |
| 229 | xLabel <- xlab( "Scale" ) |
| 230 | yLabel <- ylab( "Latency (ms)" ) |
| 231 | |
| 232 | errorBarColor <- webColor( "darkerGray" ) |
| 233 | barWidth <- 1 |
| 234 | |
| 235 | theme <- graphTheme() |
| 236 | |
| 237 | subtitle <- lastUpdatedLabel() |
| 238 | |
| 239 | colors <- scale_fill_manual( values=c( webColor( "redv2" ), |
| 240 | webColor( "light_blue" ), |
| 241 | webColor( "green" ) ) ) |
| 242 | |
| 243 | # ---------------------------- |
| 244 | # Switch Up Generate Main Plot |
| 245 | # ---------------------------- |
| 246 | |
| 247 | print( "Creating main plot (Switch Up Latency)." ) |
| 248 | |
| 249 | mainPlot <- ggplot( data = upAvgsData, aes( x = scale, |
| 250 | y = ms, |
| 251 | fill = type, |
| 252 | ymin = fileData[ 'up_end_to_end_avg' ], |
| 253 | ymax = fileData[ 'up_end_to_end_avg' ] + stds ) ) |
| 254 | |
| 255 | # ---------------------------------------- |
| 256 | # Switch Up Fundamental Variables Assigned |
| 257 | # ---------------------------------------- |
| 258 | |
| 259 | print( "Generating fundamental graph data (Switch Up Latency)." ) |
| 260 | |
| 261 | title <- labs( title = "Switch Up Latency", subtitle = subtitle ) |
| 262 | |
| 263 | fundamentalGraphData <- mainPlot + |
| 264 | xScaleConfig + |
| 265 | xLabel + |
| 266 | yLabel + |
| 267 | theme + |
| 268 | title + |
| 269 | colors |
| 270 | |
| 271 | # ------------------------------------- |
| 272 | # Switch Up Generating Bar Graph Format |
| 273 | # ------------------------------------- |
| 274 | |
| 275 | print( "Generating bar graph with error bars (Switch Up Latency)." ) |
| 276 | |
| 277 | barGraphFormat <- geom_bar( stat = "identity", width = barWidth ) |
| 278 | errorBarFormat <- geom_errorbar( width = barWidth, color = errorBarColor ) |
| 279 | |
| 280 | barGraphValues <- geom_text( aes( x = upAvgsData$scale, |
| 281 | y = sumOfUpAvgs + 0.04 * max( sumOfUpAvgs ), |
| 282 | label = format( sumOfUpAvgs, |
| 283 | digits = 3, |
| 284 | big.mark = ",", |
| 285 | scientific = FALSE ) ), |
| 286 | size = 7.0, |
| 287 | fontface = "bold" ) |
| 288 | |
| 289 | wrapLegend <- guides( fill = guide_legend( nrow = 2, byrow = TRUE ) ) |
| 290 | |
| 291 | result <- fundamentalGraphData + |
| 292 | barGraphFormat + |
| 293 | errorBarFormat + |
| 294 | barGraphValues + |
| 295 | wrapLegend |
| 296 | |
| 297 | # --------------------------------- |
| 298 | # Switch Up Exporting Graph to File |
| 299 | # --------------------------------- |
| 300 | |
| 301 | saveGraph( errBarOutputFileUp ) |
| 302 | |
| 303 | # ------------------------------ |
| 304 | # Switch Down Generate Main Plot |
| 305 | # ------------------------------ |
| 306 | |
| 307 | print( "Creating main plot (Switch Down Latency)." ) |
| 308 | |
| 309 | mainPlot <- ggplot( data = downAvgsData, aes( x = scale, |
| 310 | y = ms, |
| 311 | fill = type, |
| 312 | ymin = fileData[ 'down_end_to_end_avg' ], |
| 313 | ymax = fileData[ 'down_end_to_end_avg' ] + stds ) ) |
| 314 | |
| 315 | # ------------------------------------------ |
| 316 | # Switch Down Fundamental Variables Assigned |
| 317 | # ------------------------------------------ |
| 318 | |
| 319 | print( "Generating fundamental graph data (Switch Down Latency)." ) |
| 320 | |
| 321 | title <- labs( title = "Switch Down Latency", subtitle = subtitle ) |
| 322 | |
| 323 | fundamentalGraphData <- mainPlot + |
| 324 | xScaleConfig + |
| 325 | xLabel + |
| 326 | yLabel + |
| 327 | theme + |
| 328 | title + |
| 329 | colors |
| 330 | |
| 331 | # --------------------------------------- |
| 332 | # Switch Down Generating Bar Graph Format |
| 333 | # --------------------------------------- |
| 334 | |
| 335 | print( "Generating bar graph with error bars (Switch Down Latency)." ) |
| 336 | barGraphFormat <- geom_bar( stat = "identity", width = barWidth ) |
| 337 | errorBarFormat <- geom_errorbar( width = barWidth, color = errorBarColor ) |
| 338 | |
| 339 | barGraphValues <- geom_text( aes( x = downAvgsData$scale, |
| 340 | y = sumOfDownAvgs + 0.04 * max( sumOfDownAvgs ), |
| 341 | label = format( sumOfDownAvgs, |
| 342 | digits = 3, |
| 343 | big.mark = ",", |
| 344 | scientific = FALSE ) ), |
| 345 | size = 7.0, |
| 346 | fontface = "bold" ) |
| 347 | |
| 348 | wrapLegend <- guides( fill = guide_legend( nrow = 1, byrow = TRUE ) ) |
| 349 | |
| 350 | result <- fundamentalGraphData + |
| 351 | barGraphFormat + |
| 352 | errorBarFormat + |
| 353 | barGraphValues + |
| 354 | wrapLegend |
| 355 | |
| 356 | # ----------------------------------- |
| 357 | # Switch Down Exporting Graph to File |
| 358 | # ----------------------------------- |
| 359 | |
| 360 | saveGraph( errBarOutputFileDown ) |
| 361 | quit( status = 0 ) |