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, |
| 21 | # please contact Jeremy Ronquillo: jeremyr@opennetworking.org |
| 22 | |
| 23 | # ********************************************************** |
| 24 | # STEP 1: File management. |
| 25 | # ********************************************************** |
| 26 | |
| 27 | print( "STEP 1: File management." ) |
| 28 | |
| 29 | # Command line arguments are read. Args usually include the database filename and the output |
| 30 | # directory for the graphs to save to. |
| 31 | # ie: Rscript SCPFgraphGenerator SCPFsampleDataDB.csv ~/tmp/ |
| 32 | print( "Reading commmand-line args." ) |
| 33 | args <- commandArgs( trailingOnly=TRUE ) |
| 34 | |
| 35 | # Import libraries to be used for graphing and organizing data, respectively. |
| 36 | # Find out more about ggplot2: https://github.com/tidyverse/ggplot2 |
| 37 | # reshape2: https://github.com/hadley/reshape |
| 38 | print( "Importing libraries." ) |
| 39 | library( ggplot2 ) |
| 40 | library( reshape2 ) |
| 41 | library( RPostgreSQL ) # For databases |
| 42 | |
| 43 | # Check if sufficient args are provided. |
| 44 | if ( is.na( args[ 9 ] ) ){ |
| 45 | print( "Usage: Rscript SCPFIntentInstallWithdrawRerouteLat.R <isFlowObj> <database-host> <database-port> <database-user-id> <database-password> <test-name> <branch-name> <batch-size> <directory-to-save-graphs>" ) |
| 46 | q() # basically exit(), but in R |
| 47 | } |
| 48 | |
| 49 | flowObjFileModifier <- "" |
| 50 | if ( args[ 1 ] == "y" ){ |
| 51 | flowObjFileModifier <- "fobj_" |
| 52 | } |
| 53 | |
| 54 | # Filenames for output graphs include the testname and the graph type. |
| 55 | # See the examples below. paste() is used to concatenate strings. |
| 56 | |
| 57 | errBarOutputFile <- paste( args[ 9 ], "SCPFIntentInstallWithdrawRerouteLat", sep="" ) |
| 58 | errBarOutputFile <- paste( errBarOutputFile, args[ 7 ], sep="_" ) |
| 59 | if ( args[ 1 ] == "y" ){ |
| 60 | errBarOutputFile <- paste( errBarOutputFile, "_fobj", sep="" ) |
| 61 | } |
| 62 | errBarOutputFile <- paste( errBarOutputFile, "_", sep="" ) |
| 63 | errBarOutputFile <- paste( errBarOutputFile, args[ 8 ], sep="" ) |
| 64 | errBarOutputFile <- paste( errBarOutputFile, "-batchSize", sep="" ) |
| 65 | errBarOutputFile <- paste( errBarOutputFile, "_graph.jpg", sep="" ) |
| 66 | |
| 67 | print( "Reading from databases." ) |
| 68 | |
| 69 | con <- dbConnect( dbDriver( "PostgreSQL" ), dbname="onostest", host=args[ 2 ], port=strtoi( args[ 3 ] ), user=args[ 4 ],password=args[ 5 ] ) |
| 70 | |
| 71 | command1 <- paste( "SELECT * FROM intent_latency_", flowObjFileModifier, sep="" ) |
| 72 | command1 <- paste( command1, "tests WHERE batch_size=", sep="" ) |
| 73 | command1 <- paste( command1, args[ 8 ], sep="" ) |
| 74 | command1 <- paste( command1, " AND branch = '", sep="" ) |
| 75 | command1 <- paste( command1, args[ 7 ], sep="" ) |
| 76 | command1 <- paste( command1, "' AND date IN ( SELECT MAX( date ) FROM intent_latency_", sep="" ) |
| 77 | command1 <- paste( command1, flowObjFileModifier, sep="" ) |
| 78 | command1 <- paste( command1, "tests WHERE branch='", sep="" ) |
| 79 | command1 <- paste( command1, args[ 7 ], sep="" ) |
| 80 | command1 <- paste( command1, "')", sep="" ) |
| 81 | |
| 82 | print( paste( "Sending SQL command:", command1 ) ) |
| 83 | |
| 84 | fileData1 <- dbGetQuery( con, command1 ) |
| 85 | |
| 86 | command2 <- paste( "SELECT * FROM intent_reroute_latency_", flowObjFileModifier, sep="" ) |
| 87 | command2 <- paste( command2, "tests WHERE batch_size=", sep="" ) |
| 88 | command2 <- paste( command2, args[ 8 ], sep="" ) |
| 89 | command2 <- paste( command2, " AND branch = '", sep="" ) |
| 90 | command2 <- paste( command2, args[ 7 ], sep="" ) |
| 91 | command2 <- paste( command2, "' AND date IN ( SELECT MAX( date ) FROM intent_reroute_latency_", sep="" ) |
| 92 | command2 <- paste( command2, flowObjFileModifier, sep="" ) |
| 93 | command2 <- paste( command2, "tests WHERE branch='", sep="" ) |
| 94 | command2 <- paste( command2, args[ 7 ], sep="" ) |
| 95 | command2 <- paste( command2, "')", sep="" ) |
| 96 | |
| 97 | print( paste( "Sending SQL command:", command2 ) ) |
| 98 | |
| 99 | fileData2 <- dbGetQuery( con, command2 ) |
| 100 | |
| 101 | # ********************************************************** |
| 102 | # STEP 2: Organize data. |
| 103 | # ********************************************************** |
| 104 | |
| 105 | print( "STEP 2: Organize data." ) |
| 106 | |
| 107 | # Create lists c() and organize data into their corresponding list. |
| 108 | print( "Sorting data." ) |
| 109 | if ( ncol( fileData2 ) == 0 ){ |
| 110 | avgs <- c( fileData1[ 'install_avg' ], fileData1[ 'withdraw_avg' ] ) |
| 111 | } else{ |
| 112 | colnames( fileData2 ) <- c( "date", "name", "date", "branch", "commit", "scale", "batch_size", "reroute_avg", "reroute_std" ) |
| 113 | avgs <- c( fileData1[ 'install_avg' ], fileData1[ 'withdraw_avg' ], fileData2[ 'reroute_avg' ] ) |
| 114 | } |
| 115 | |
| 116 | # Parse lists into data frames. |
| 117 | dataFrame <- melt( avgs ) # This is where reshape2 comes in. Avgs list is converted to data frame |
| 118 | |
| 119 | if ( ncol( fileData2 ) == 0 ){ |
| 120 | dataFrame$scale <- c( fileData1$scale, fileData1$scale ) # Add node scaling to the data frame. |
| 121 | dataFrame$stds <- c( fileData1$install_std, fileData1$withdraw_std ) |
| 122 | } else{ |
| 123 | dataFrame$scale <- c( fileData1$scale, fileData1$scale, fileData2$scale ) # Add node scaling to the data frame. |
| 124 | dataFrame$stds <- c( fileData1$install_std, fileData1$withdraw_std, fileData2$reroute_std ) |
| 125 | } |
| 126 | colnames( dataFrame ) <- c( "ms", "type", "scale", "stds" ) |
| 127 | |
| 128 | # Format data frame so that the data is in the same order as it appeared in the file. |
| 129 | dataFrame$type <- as.character( dataFrame$type ) |
| 130 | dataFrame$type <- factor( dataFrame$type, levels=unique( dataFrame$type ) ) |
| 131 | |
Jeremy Ronquillo | 2d2649d | 2017-09-14 12:53:06 -0700 | [diff] [blame] | 132 | dataFrame <- na.omit( dataFrame ) # Omit any data that doesn't exist |
| 133 | |
| 134 | print( "Data Frame Results:" ) |
| 135 | print( dataFrame ) |
| 136 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 137 | # ********************************************************** |
| 138 | # STEP 3: Generate graphs. |
| 139 | # ********************************************************** |
| 140 | |
| 141 | print( "STEP 3: Generate graphs." ) |
| 142 | |
| 143 | # 1. Graph fundamental data is generated first. |
| 144 | # These are variables that apply to all of the graphs being generated, regardless of type. |
| 145 | # |
| 146 | # 2. Type specific graph data is generated. |
| 147 | # Data specific for the error bar and stacked bar graphs are generated. |
| 148 | # |
| 149 | # 3. Generate and save the graphs. |
| 150 | # Graphs are saved to the filename above, in the directory provided in command line args |
| 151 | |
| 152 | print( "Generating fundamental graph data." ) |
| 153 | |
Jeremy Ronquillo | 2d2649d | 2017-09-14 12:53:06 -0700 | [diff] [blame] | 154 | theme_set( theme_grey( base_size = 20 ) ) # set the default text size of the graph. |
| 155 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 156 | # Calculate window to display graph, based on the lowest and highest points of the data. |
| 157 | if ( min( dataFrame$ms - dataFrame$stds ) < 0){ |
| 158 | yWindowMin <- min( dataFrame$ms - dataFrame$stds ) * 1.05 |
| 159 | } else { |
| 160 | yWindowMin <- 0 |
| 161 | } |
| 162 | yWindowMax <- max( dataFrame$ms + dataFrame$stds ) |
| 163 | |
| 164 | mainPlot <- ggplot( data = dataFrame, aes( x = scale, y = ms, ymin = ms - stds, ymax = ms + stds,fill = type ) ) |
| 165 | |
| 166 | # Formatting the plot |
| 167 | width <- 1.3 # Width of the bars. |
| 168 | xScaleConfig <- scale_x_continuous( breaks=c( 1, 3, 5, 7, 9) ) |
| 169 | yLimit <- ylim( yWindowMin, yWindowMax ) |
| 170 | xLabel <- xlab( "Scale" ) |
| 171 | yLabel <- ylab( "Latency (ms)" ) |
| 172 | fillLabel <- labs( fill="Type" ) |
| 173 | chartTitle <- "Intent Install, Withdraw, & Reroute Latencies" |
| 174 | if ( args[ 1 ] == "y" ){ |
Jeremy Ronquillo | 2d2649d | 2017-09-14 12:53:06 -0700 | [diff] [blame] | 175 | chartTitle <- paste( chartTitle, "w/ FlowObj" ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 176 | } |
| 177 | chartTitle <- paste( chartTitle, "\nBatch Size =" ) |
| 178 | chartTitle <- paste( chartTitle, fileData1[ 1,'batch_size' ] ) |
| 179 | |
Jeremy Ronquillo | be37f09 | 2017-09-26 13:30:05 -0700 | [diff] [blame] | 180 | theme <- theme( plot.title=element_text( hjust = 0.5, size = 22, face='bold' ) ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 181 | |
| 182 | # Store plot configurations as 1 variable |
| 183 | fundamentalGraphData <- mainPlot + xScaleConfig + yLimit + xLabel + yLabel + fillLabel + theme |
| 184 | |
| 185 | |
| 186 | # Create the bar graph with error bars. |
| 187 | # geom_bar contains: |
| 188 | # - stat: data formatting (usually "identity") |
| 189 | # - width: the width of the bar types (declared above) |
| 190 | # geom_errorbar contains similar arguments as geom_bar. |
| 191 | print( "Generating bar graph with error bars." ) |
| 192 | barGraphFormat <- geom_bar( stat = "identity", width = width, position = "dodge" ) |
| 193 | errorBarFormat <- geom_errorbar( width = width, position = "dodge" ) |
| 194 | title <- ggtitle( chartTitle ) |
| 195 | result <- fundamentalGraphData + barGraphFormat + errorBarFormat + title |
| 196 | |
| 197 | # Save graph to file |
| 198 | print( paste( "Saving bar chart with error bars to", errBarOutputFile ) ) |
| 199 | ggsave( errBarOutputFile, width = 10, height = 6, dpi = 200 ) |
| 200 | print( paste( "Successfully wrote bar chart with error bars out to", errBarOutputFile ) ) |