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 | # Mastership Failover Graph (https://jenkins.onosproject.org/view/QA/job/postjob-BM/lastSuccessfulBuild/artifact/SCPFmastershipFailoverLat_master_errGraph.jpg): |
| 22 | # Rscript SCPFspecificGraphRScripts/SCPFmastershipFailoverLat.R <url> <port> <username> <pass> SCPFmastershipFailoverLat 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 |
Devin Lim | 324806b | 2018-05-11 15:36:52 -0700 | [diff] [blame] | 46 | source( "~/OnosSystemTest/TestON/JenkinsFile/wikiGraphRScripts/dependencies/saveGraph.R" ) |
| 47 | source( "~/OnosSystemTest/TestON/JenkinsFile/wikiGraphRScripts/dependencies/fundamentalGraphData.R" ) |
| 48 | source( "~/OnosSystemTest/TestON/JenkinsFile/wikiGraphRScripts/dependencies/initSQL.R" ) |
| 49 | source( "~/OnosSystemTest/TestON/JenkinsFile/wikiGraphRScripts/dependencies/cliArgs.R" ) |
Jeremy Ronquillo | dae1104 | 2018-02-21 09:21:44 -0800 | [diff] [blame] | 50 | |
| 51 | # ------------------- |
| 52 | # Check CLI Arguments |
| 53 | # ------------------- |
| 54 | |
| 55 | print( "Verifying CLI args." ) |
| 56 | |
| 57 | if ( length( args ) != save_directory ){ |
| 58 | usage( "SCPFmastershipFailoverLat.R" ) |
| 59 | quit( status = 1 ) |
| 60 | } |
| 61 | |
| 62 | # ----------------- |
| 63 | # Create File Names |
| 64 | # ----------------- |
| 65 | |
| 66 | print( "Creating filenames and title of graph." ) |
| 67 | |
| 68 | chartTitle <- "Mastership Failover Latency" |
| 69 | |
| 70 | errBarOutputFile <- paste( args[ save_directory ], |
| 71 | args[ graph_title ], |
| 72 | "_", |
| 73 | args[ branch_name ], |
| 74 | "_errGraph.jpg", |
| 75 | sep="" ) |
| 76 | |
| 77 | stackedBarOutputFile <- paste( args[ save_directory ], |
| 78 | args[ graph_title ], |
| 79 | "_", |
| 80 | args[ branch_name ], |
| 81 | "_stackedGraph.jpg", |
| 82 | sep="" ) |
| 83 | |
| 84 | # ------------------ |
| 85 | # SQL Initialization |
| 86 | # ------------------ |
| 87 | |
| 88 | print( "Initializing SQL" ) |
| 89 | |
| 90 | con <- initSQL( args[ database_host ], |
| 91 | args[ database_port ], |
| 92 | args[ database_u_id ], |
| 93 | args[ database_pw ] ) |
| 94 | |
| 95 | # --------------------------------------- |
| 96 | # Mastership Failover Latency SQL Command |
| 97 | # --------------------------------------- |
| 98 | |
| 99 | print( "Generating Mastership Failover Latency SQL command" ) |
| 100 | |
| 101 | command <- paste( "SELECT * FROM mastership_failover_tests WHERE branch = '", |
| 102 | args[ branch_name ], |
| 103 | "' AND date IN ( SELECT MAX( date ) FROM mastership_failover_tests WHERE branch = '", |
| 104 | args[ branch_name ], |
| 105 | "' ) ", |
| 106 | sep = "" ) |
| 107 | |
| 108 | fileData <- retrieveData( con, command ) |
| 109 | |
| 110 | # ********************************************************** |
| 111 | # STEP 2: Organize data. |
| 112 | # ********************************************************** |
| 113 | |
| 114 | print( "**********************************************************" ) |
| 115 | print( "STEP 2: Organize Data." ) |
| 116 | print( "**********************************************************" ) |
| 117 | |
| 118 | # ------------ |
| 119 | # Data Sorting |
| 120 | # ------------ |
| 121 | |
| 122 | print( "Combining averages into a list." ) |
| 123 | |
| 124 | requiredColumns <- c( "kill_deact_avg", "deact_role_avg" ) |
| 125 | |
| 126 | tryCatch( avgs <- c( fileData[ requiredColumns] ), |
| 127 | error = function( e ) { |
| 128 | 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." ) |
| 129 | print( "Required columns: " ) |
| 130 | print( requiredColumns ) |
| 131 | print( "Actual columns: " ) |
| 132 | print( names( fileData ) ) |
| 133 | print( "Error dump:" ) |
| 134 | print( e ) |
| 135 | quit( status = 1 ) |
| 136 | } |
| 137 | ) |
| 138 | |
| 139 | # -------------------- |
| 140 | # Construct Data Frame |
| 141 | # -------------------- |
| 142 | |
| 143 | print( "Constructing Data Frame from list." ) |
| 144 | |
| 145 | dataFrame <- melt( avgs ) |
| 146 | dataFrame$scale <- fileData$scale |
| 147 | dataFrame$stds <- c( fileData$kill_deact_std, |
| 148 | fileData$deact_role_std ) |
| 149 | |
| 150 | colnames( dataFrame ) <- c( "ms", |
| 151 | "type", |
| 152 | "scale", |
| 153 | "stds" ) |
| 154 | |
| 155 | dataFrame <- na.omit( dataFrame ) # Omit any data that doesn't exist |
| 156 | |
| 157 | sum <- fileData[ 'deact_role_avg' ] + |
| 158 | fileData[ 'kill_deact_avg' ] |
| 159 | |
| 160 | print( "Data Frame Results:" ) |
| 161 | print( dataFrame ) |
| 162 | |
| 163 | |
| 164 | # ********************************************************** |
| 165 | # STEP 3: Generate graphs. |
| 166 | # ********************************************************** |
| 167 | |
| 168 | print( "**********************************************************" ) |
| 169 | print( "STEP 3: Generate Graph." ) |
| 170 | print( "**********************************************************" ) |
| 171 | |
| 172 | # ------------------------------------ |
| 173 | # Initialize Variables for Both Graphs |
| 174 | # ------------------------------------ |
| 175 | |
| 176 | print( "Initializing variables used in both graphs." ) |
| 177 | |
| 178 | defaultTextSize() |
| 179 | xScaleConfig <- scale_x_continuous( breaks = c( 1, 3, 5, 7, 9) ) |
| 180 | |
| 181 | xLabel <- xlab( "Scale" ) |
| 182 | yLabel <- ylab( "Latency (ms)" ) |
| 183 | fillLabel <- labs( fill = "Type" ) |
| 184 | |
| 185 | barWidth <- 0.9 |
| 186 | |
| 187 | theme <- graphTheme() |
| 188 | |
| 189 | barColors <- scale_fill_manual( values=c( webColor( "redv2" ), |
| 190 | webColor( "light_blue" ) ) ) |
| 191 | |
| 192 | wrapLegend <- guides( fill=guide_legend( nrow=1, byrow=TRUE ) ) |
| 193 | |
| 194 | # ---------------------------------- |
| 195 | # Error Bar Graph Generate Main Plot |
| 196 | # ---------------------------------- |
| 197 | |
| 198 | print( "Creating main plot." ) |
| 199 | |
| 200 | mainPlot <- ggplot( data = dataFrame, aes( x = scale, |
| 201 | y = ms, |
| 202 | ymin = ms, |
| 203 | ymax = ms + stds, |
| 204 | fill = type ) ) |
| 205 | |
| 206 | # ---------------------------------------------- |
| 207 | # Error Bar Graph Fundamental Variables Assigned |
| 208 | # ---------------------------------------------- |
| 209 | |
| 210 | print( "Generating fundamental graph data for the error bar graph." ) |
| 211 | |
| 212 | title <- labs( title = chartTitle, subtitle = lastUpdatedLabel() ) |
| 213 | |
| 214 | fundamentalGraphData <- mainPlot + |
| 215 | xScaleConfig + |
| 216 | xLabel + |
| 217 | yLabel + |
| 218 | fillLabel + |
| 219 | theme + |
| 220 | title + |
| 221 | wrapLegend |
| 222 | |
| 223 | # ------------------------------------------- |
| 224 | # Error Bar Graph Generating Bar Graph Format |
| 225 | # ------------------------------------------- |
| 226 | |
| 227 | print( "Generating bar graph with error bars." ) |
| 228 | |
| 229 | barGraphFormat <- geom_bar( stat = "identity", |
| 230 | position = position_dodge(), |
| 231 | width = barWidth ) |
| 232 | |
| 233 | errorBarFormat <- geom_errorbar( width = barWidth, |
| 234 | position = position_dodge(), |
| 235 | color = webColor( "darkerGray" ) ) |
| 236 | |
| 237 | values <- geom_text( aes( x = dataFrame$scale, |
| 238 | y = dataFrame$ms + 0.02 * max( dataFrame$ms ), |
| 239 | label = format( dataFrame$ms, |
| 240 | digits = 3, |
| 241 | big.mark = ",", |
| 242 | scientific = FALSE ) ), |
| 243 | size = 7.0, |
| 244 | fontface = "bold", |
| 245 | position = position_dodge( 0.9 ) ) |
| 246 | |
| 247 | result <- fundamentalGraphData + |
| 248 | barGraphFormat + |
| 249 | barColors + |
| 250 | errorBarFormat + |
| 251 | values |
| 252 | |
| 253 | # --------------------------------------- |
| 254 | # Error Bar Graph Exporting Graph to File |
| 255 | # --------------------------------------- |
| 256 | |
| 257 | saveGraph( errBarOutputFile ) |
| 258 | |
| 259 | # ------------------------------------------------ |
| 260 | # Stacked Bar Graph Fundamental Variables Assigned |
| 261 | # ------------------------------------------------ |
| 262 | |
| 263 | print( "Generating fundamental graph data for the stacked bar graph." ) |
| 264 | |
| 265 | title <- labs( title = chartTitle, subtitle = lastUpdatedLabel() ) |
| 266 | |
| 267 | fundamentalGraphData <- mainPlot + |
| 268 | xScaleConfig + |
| 269 | xLabel + |
| 270 | yLabel + |
| 271 | fillLabel + |
| 272 | theme + |
| 273 | title + |
| 274 | wrapLegend |
| 275 | |
| 276 | # --------------------------------------------- |
| 277 | # Stacked Bar Graph Generating Bar Graph Format |
| 278 | # --------------------------------------------- |
| 279 | |
| 280 | print( "Generating stacked bar chart." ) |
| 281 | stackedBarFormat <- geom_bar( stat = "identity", |
| 282 | width = barWidth ) |
| 283 | |
| 284 | values <- geom_text( aes( x = dataFrame$scale, |
| 285 | y = sum + 0.02 * max( sum ), |
| 286 | label = format( sum, |
| 287 | digits = 3, |
| 288 | big.mark = ",", |
| 289 | scientific = FALSE ) ), |
| 290 | size = 7.0, |
| 291 | fontface = "bold" ) |
| 292 | |
| 293 | result <- fundamentalGraphData + |
| 294 | stackedBarFormat + |
| 295 | barColors + |
| 296 | title + |
| 297 | values |
| 298 | |
| 299 | # ----------------------------------------- |
| 300 | # Stacked Bar Graph Exporting Graph to File |
| 301 | # ----------------------------------------- |
| 302 | |
| 303 | saveGraph( stackedBarOutputFile ) |