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 | # If you have any questions, or if you don't understand R, |
| 21 | # please contact Jeremy Ronquillo: j_ronquillo@u.pacific.edu |
| 22 | |
| 23 | # ********************************************************** |
| 24 | # STEP 1: Data management. |
| 25 | # ********************************************************** |
| 26 | |
| 27 | print( "**********************************************************" ) |
| 28 | print( "STEP 1: Data management." ) |
| 29 | print( "**********************************************************" ) |
| 30 | has_flow_obj = 1 |
| 31 | database_host = 2 |
| 32 | database_port = 3 |
| 33 | database_u_id = 4 |
| 34 | database_pw = 5 |
| 35 | test_name = 6 |
| 36 | branch_name = 7 |
| 37 | old_flow = 8 |
| 38 | save_directory = 9 |
| 39 | |
| 40 | print( "Reading commmand-line args." ) |
| 41 | args <- commandArgs( trailingOnly=TRUE ) |
| 42 | |
| 43 | # ---------------- |
| 44 | # Import Libraries |
| 45 | # ---------------- |
| 46 | |
| 47 | print( "Importing libraries." ) |
| 48 | library( ggplot2 ) |
| 49 | library( reshape2 ) |
| 50 | library( RPostgreSQL ) # For databases |
| 51 | |
| 52 | # ------------------- |
| 53 | # Check CLI Arguments |
| 54 | # ------------------- |
| 55 | |
| 56 | print( "Verifying CLI args." ) |
| 57 | |
| 58 | if ( is.na( args[ save_directory ] ) ){ |
| 59 | print( paste( "Usage: Rscript SCPFInstalledIntentsFlows", |
| 60 | "<has-flowObj>", |
| 61 | "<database-host>", |
| 62 | "<database-port>", |
| 63 | "<database-user-id>", |
| 64 | "<database-password>", |
| 65 | "<test-name>", |
| 66 | "<branch-name>", |
| 67 | "<using-old-flow>", |
| 68 | "<directory-to-save-graphs>", |
| 69 | sep=" " ) ) |
| 70 | |
| 71 | quit( status = 1 ) # basically exit(), but in R |
| 72 | } |
| 73 | |
| 74 | # ----------------- |
| 75 | # Create File Names |
| 76 | # ----------------- |
| 77 | |
| 78 | print( "Creating filenames and title of graph." ) |
| 79 | |
| 80 | fileFlowObjModifier <- "" |
| 81 | sqlFlowObjModifier <- "" |
| 82 | chartTitle <- "Number of Installed Intents & Flows" |
| 83 | |
| 84 | if ( args[ has_flow_obj ] == "y" ){ |
| 85 | fileFlowObjModifier <- "_flowObj" |
| 86 | sqlFlowObjModifier <- "fobj_" |
| 87 | chartTitle <- "Number of Installed Intents & Flows\n with Flow Objectives" |
| 88 | } |
| 89 | fileOldFlowModifier <- "" |
| 90 | if ( args[ old_flow ] == 'y' ){ |
| 91 | fileOldFlowModifier <- "_OldFlow" |
| 92 | chartTitle <- paste( chartTitle, "With Eventually Consistent Flow Rule Store", sep="\n" ) |
| 93 | } |
| 94 | |
| 95 | outputFile <- paste( args[ save_directory ], |
| 96 | args[ test_name ], |
| 97 | fileFlowObjModifier, |
| 98 | fileOldFlowModifier, |
| 99 | "_", |
| 100 | args[ branch_name ], |
| 101 | "_errGraph.jpg", |
| 102 | sep="" ) |
| 103 | |
| 104 | # ------------------ |
| 105 | # SQL Initialization |
| 106 | # ------------------ |
| 107 | |
| 108 | print( "Initializing SQL" ) |
| 109 | |
| 110 | con <- dbConnect( dbDriver( "PostgreSQL" ), |
| 111 | dbname = "onostest", |
| 112 | host = args[ database_host ], |
| 113 | port = strtoi( args[ database_port ] ), |
| 114 | user = args[ database_u_id ], |
| 115 | password = args[ database_pw ] ) |
| 116 | |
| 117 | # ------------------------------- |
| 118 | # Scaling Max Intents SQL Command |
| 119 | # ------------------------------- |
| 120 | |
| 121 | print( "Scaling Max Intents SQL Command" ) |
| 122 | |
| 123 | command <- paste( "SELECT * FROM max_intents_", |
| 124 | sqlFlowObjModifier, |
| 125 | "tests WHERE branch = '", |
| 126 | args[ branch_name ], |
| 127 | "' AND date IN ( SELECT MAX( date ) FROM max_intents_", |
| 128 | sqlFlowObjModifier, |
| 129 | "tests WHERE branch = '", |
| 130 | args[ branch_name ], |
| 131 | "' AND ", |
| 132 | ( if( args[ old_flow ] == 'y' ) "" else "NOT " ), |
| 133 | "is_old_flow", |
| 134 | " ) ", |
| 135 | sep="" ) |
| 136 | |
| 137 | print( "Sending SQL command:" ) |
| 138 | print( command ) |
| 139 | fileData <- dbGetQuery( con, command ) |
| 140 | |
| 141 | # ********************************************************** |
| 142 | # STEP 2: Organize data. |
| 143 | # ********************************************************** |
| 144 | |
| 145 | print( "**********************************************************" ) |
| 146 | print( "STEP 2: Organize Data." ) |
| 147 | print( "**********************************************************" ) |
| 148 | |
| 149 | # ------------ |
| 150 | # Data Sorting |
| 151 | # ------------ |
| 152 | |
| 153 | print( "Sorting data." ) |
| 154 | |
| 155 | requiredColumns <- c( "max_intents_ovs", "max_flows_ovs" ) |
| 156 | |
| 157 | tryCatch( avgs <- c( fileData[ requiredColumns] ), |
| 158 | error = function( e ) { |
| 159 | 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." ) |
| 160 | print( "Required columns: " ) |
| 161 | print( requiredColumns ) |
| 162 | print( "Actual columns: " ) |
| 163 | print( names( fileData ) ) |
| 164 | print( "Error dump:" ) |
| 165 | print( e ) |
| 166 | quit( status = 1 ) |
| 167 | } |
| 168 | ) |
| 169 | |
| 170 | # -------------------- |
| 171 | # Construct Data Frame |
| 172 | # -------------------- |
| 173 | |
| 174 | print( "Constructing Data Frame" ) |
| 175 | |
| 176 | dataFrame <- melt( avgs ) |
| 177 | dataFrame$scale <- fileData$scale |
| 178 | |
| 179 | colnames( dataFrame ) <- c( "ms", "type", "scale" ) |
| 180 | |
| 181 | dataFrame$type <- as.character( dataFrame$type ) |
| 182 | dataFrame$type <- factor( dataFrame$type, levels=unique( dataFrame$type ) ) |
| 183 | |
| 184 | dataFrame <- na.omit( dataFrame ) # Omit any data that doesn't exist |
| 185 | |
| 186 | print( "Data Frame Results:" ) |
| 187 | print( dataFrame ) |
| 188 | |
| 189 | # ********************************************************** |
| 190 | # STEP 3: Generate graphs. |
| 191 | # ********************************************************** |
| 192 | |
| 193 | print( "**********************************************************" ) |
| 194 | print( "STEP 3: Generate Graph." ) |
| 195 | print( "**********************************************************" ) |
| 196 | |
| 197 | # ------------------ |
| 198 | # Generate Main Plot |
| 199 | # ------------------ |
| 200 | |
| 201 | print( "Creating main plot." ) |
| 202 | mainPlot <- ggplot( data = dataFrame, aes( x = scale, |
| 203 | y = ms, |
| 204 | fill = type ) ) |
| 205 | |
| 206 | # ------------------------------ |
| 207 | # Fundamental Variables Assigned |
| 208 | # ------------------------------ |
| 209 | |
| 210 | print( "Generating fundamental graph data." ) |
| 211 | |
| 212 | barWidth <- 1.3 |
| 213 | theme_set( theme_grey( base_size = 22 ) ) # set the default text size of the graph. |
| 214 | xScaleConfig <- scale_x_continuous( breaks=c( 1, 3, 5, 7, 9 ) ) |
| 215 | xLabel <- xlab( "Scale" ) |
| 216 | yLabel <- ylab( "Max Number of Intents/Flow Rules" ) |
| 217 | fillLabel <- labs( fill="Type" ) |
| 218 | imageWidth <- 15 |
| 219 | imageHeight <- 10 |
| 220 | imageDPI <- 200 |
| 221 | |
| 222 | theme <- theme( plot.title = element_text( hjust = 0.5, size = 32, face = 'bold' ), |
| 223 | legend.position = "bottom", |
| 224 | legend.text = element_text( size=22 ), |
| 225 | legend.title = element_blank(), |
| 226 | legend.key.size = unit( 1.5, 'lines' ), |
| 227 | plot.subtitle = element_text( size=16, hjust=1.0 ) ) |
| 228 | |
| 229 | subtitle <- paste( "Last Updated: ", format( Sys.time(), format = "%b %d, %Y at %I:%M %p %Z" ), sep="" ) |
| 230 | |
| 231 | colors <- scale_fill_manual( values = c( "#F77670", |
| 232 | "#619DFA" ) ) |
| 233 | |
| 234 | wrapLegend <- guides( fill = guide_legend( nrow = 1, byrow = TRUE ) ) |
| 235 | |
| 236 | title <- labs( title = chartTitle, subtitle = subtitle ) |
| 237 | |
| 238 | fundamentalGraphData <- mainPlot + |
| 239 | xScaleConfig + |
| 240 | xLabel + |
| 241 | yLabel + |
| 242 | fillLabel + |
| 243 | theme + |
| 244 | wrapLegend + |
| 245 | title + |
| 246 | colors |
| 247 | |
| 248 | # --------------------------- |
| 249 | # Generating Bar Graph Format |
| 250 | # --------------------------- |
| 251 | |
| 252 | print( "Generating bar graph." ) |
| 253 | |
| 254 | barGraphFormat <- geom_bar( stat = "identity", |
| 255 | position = position_dodge(), |
| 256 | width = barWidth ) |
| 257 | |
| 258 | values <- geom_text( aes( x = dataFrame$scale, |
| 259 | y = dataFrame$ms + 0.015 * max( dataFrame$ms ), |
| 260 | label = format( dataFrame$ms, |
| 261 | digits=3, |
| 262 | big.mark = ",", |
| 263 | scientific = FALSE ) ), |
| 264 | size = 5.2, |
| 265 | fontface = "bold", |
| 266 | position = position_dodge( width = 1.25 ) ) |
| 267 | |
| 268 | result <- fundamentalGraphData + |
| 269 | barGraphFormat + |
| 270 | values |
| 271 | |
| 272 | # ----------------------- |
| 273 | # Exporting Graph to File |
| 274 | # ----------------------- |
| 275 | |
| 276 | print( paste( "Saving bar chart to", outputFile ) ) |
| 277 | |
| 278 | tryCatch( ggsave( outputFile, |
| 279 | width = imageWidth, |
| 280 | height = imageHeight, |
| 281 | dpi = imageDPI ), |
| 282 | error = function( e ){ |
| 283 | print( "[ERROR] There was a problem saving the graph due to a graph formatting exception. Error dump:" ) |
| 284 | print( e ) |
| 285 | quit( status = 1 ) |
| 286 | } |
| 287 | ) |
| 288 | |
| 289 | print( paste( "[SUCCESS] Successfully wrote bar chart out to", outputFile ) ) |
| 290 | quit( status = 0 ) |