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. |
Devin Lim | 0e96716 | 2017-11-03 15:59:53 -0700 | [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 | # ------------------- |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 53 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 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 SCPFhostLat", |
| 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 | errBarOutputFile <- 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 | "_errGraph.jpg", |
| 82 | sep="" ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 83 | |
| 84 | chartTitle <- "Host Latency" |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 85 | # ------------------ |
| 86 | # SQL Initialization |
| 87 | # ------------------ |
| 88 | |
| 89 | print( "Initializing SQL" ) |
| 90 | |
| 91 | con <- dbConnect( dbDriver( "PostgreSQL" ), |
| 92 | dbname = "onostest", |
Devin Lim | 0e96716 | 2017-11-03 15:59:53 -0700 | [diff] [blame] | 93 | host = args[ database_host ], |
| 94 | port = strtoi( args[ database_port ] ), |
| 95 | user = args[ database_u_id ], |
| 96 | password = args[ database_pw ] ) |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 97 | |
| 98 | # ------------------------ |
| 99 | # Host Latency SQL Command |
| 100 | # ------------------------ |
| 101 | |
| 102 | print( "Generating Host Latency SQL Command" ) |
| 103 | |
| 104 | command <- paste( "SELECT * FROM host_latency_tests WHERE branch = '", |
Devin Lim | 0e96716 | 2017-11-03 15:59:53 -0700 | [diff] [blame] | 105 | args[ branch_name ], |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 106 | "' AND date IN ( SELECT MAX( date ) FROM host_latency_tests WHERE branch = '", |
Devin Lim | 0e96716 | 2017-11-03 15:59:53 -0700 | [diff] [blame] | 107 | args[ branch_name ], |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 108 | "' ) ", |
| 109 | sep = "" ) |
| 110 | |
| 111 | print( "Sending SQL command:" ) |
| 112 | print( command ) |
| 113 | |
| 114 | fileData <- dbGetQuery( con, command ) |
| 115 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 116 | |
| 117 | # ********************************************************** |
| 118 | # STEP 2: Organize data. |
| 119 | # ********************************************************** |
| 120 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 121 | print( "**********************************************************" ) |
| 122 | print( "STEP 2: Organize Data." ) |
| 123 | print( "**********************************************************" ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 124 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 125 | # ------------ |
| 126 | # Data Sorting |
| 127 | # ------------ |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 128 | |
| 129 | print( "Sorting data." ) |
Jeremy Ronquillo | 9ea85d0 | 2017-11-27 10:21:03 -0800 | [diff] [blame] | 130 | |
| 131 | requiredColumns <- c( "avg" ) |
| 132 | |
| 133 | tryCatch( avgs <- c( fileData[ requiredColumns] ), |
| 134 | error = function( e ) { |
| 135 | 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." ) |
| 136 | print( "Required columns: " ) |
| 137 | print( requiredColumns ) |
| 138 | print( "Actual columns: " ) |
| 139 | print( names( fileData ) ) |
| 140 | print( "Error dump:" ) |
| 141 | print( e ) |
| 142 | quit( status = 1 ) |
| 143 | } |
| 144 | ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 145 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 146 | # -------------------- |
| 147 | # Construct Data Frame |
| 148 | # -------------------- |
| 149 | |
| 150 | print( "Constructing Data Frame" ) |
| 151 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 152 | dataFrame <- melt( avgs ) |
| 153 | dataFrame$scale <- fileData$scale |
| 154 | dataFrame$std <- fileData$std |
| 155 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 156 | colnames( dataFrame ) <- c( "ms", |
| 157 | "type", |
| 158 | "scale", |
| 159 | "std" ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 160 | |
Jeremy Ronquillo | 2d2649d | 2017-09-14 12:53:06 -0700 | [diff] [blame] | 161 | dataFrame <- na.omit( dataFrame ) # Omit any data that doesn't exist |
| 162 | |
| 163 | print( "Data Frame Results:" ) |
| 164 | print( dataFrame ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 165 | |
| 166 | # ********************************************************** |
| 167 | # STEP 3: Generate graphs. |
| 168 | # ********************************************************** |
| 169 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 170 | print( "**********************************************************" ) |
| 171 | print( "STEP 3: Generate Graph." ) |
| 172 | print( "**********************************************************" ) |
| 173 | |
| 174 | # ------------------ |
| 175 | # Generate Main Plot |
| 176 | # ------------------ |
| 177 | |
| 178 | print( "Creating main plot." ) |
| 179 | |
| 180 | mainPlot <- ggplot( data = dataFrame, aes( x = scale, |
| 181 | y = ms, |
| 182 | ymin = ms, |
| 183 | ymax = ms + std ) ) |
| 184 | |
| 185 | # ------------------------------ |
| 186 | # Fundamental Variables Assigned |
| 187 | # ------------------------------ |
| 188 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 189 | print( "Generating fundamental graph data." ) |
| 190 | |
Jeremy Ronquillo | 0e27b91 | 2017-10-21 14:34:24 -0700 | [diff] [blame] | 191 | theme_set( theme_grey( base_size = 22 ) ) # set the default text size of the graph. |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 192 | xScaleConfig <- scale_x_continuous( breaks=c( 1, 3, 5, 7, 9 ) ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 193 | xLabel <- xlab( "Scale" ) |
| 194 | yLabel <- ylab( "Latency (ms)" ) |
| 195 | fillLabel <- labs( fill="Type" ) |
Jeremy Ronquillo | 94f99dd | 2018-01-05 11:11:27 -0800 | [diff] [blame] | 196 | theme <- theme( plot.title = element_text( hjust = 0.5, size = 32, face ='bold' ), |
| 197 | plot.subtitle = element_text( size=16, hjust=1.0 ) ) |
| 198 | |
| 199 | subtitle <- paste( "Last Updated: ", format( Sys.time(), format = "%b %d, %Y at %I:%M %p %Z" ), sep="" ) |
| 200 | |
| 201 | title <- labs( title = chartTitle, subtitle = subtitle ) |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 202 | errorBarColor <- rgb( 140, 140, 140, maxColorValue = 255 ) |
| 203 | imageWidth <- 15 |
| 204 | imageHeight <- 10 |
| 205 | imageDPI <- 200 |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 206 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 207 | fundamentalGraphData <- mainPlot + |
| 208 | xScaleConfig + |
| 209 | xLabel + |
| 210 | yLabel + |
| 211 | fillLabel + |
| 212 | theme + |
| 213 | title |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 214 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 215 | # --------------------------- |
| 216 | # Generating Bar Graph Format |
| 217 | # --------------------------- |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 218 | |
| 219 | print( "Generating bar graph with error bars." ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 220 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 221 | barWidth <- 0.9 |
| 222 | barGraphFormat <- geom_bar( stat = "identity", |
| 223 | position = position_dodge(), |
| 224 | width = barWidth, |
| 225 | fill = "#A700EF" ) |
| 226 | |
| 227 | errorBarFormat <- geom_errorbar( position = position_dodge(), |
| 228 | width = barWidth, |
| 229 | color = errorBarColor ) |
| 230 | |
| 231 | values <- geom_text( aes( x=dataFrame$scale, |
| 232 | y=dataFrame$ms + 0.06 * max( dataFrame$ms ), |
| 233 | label = format( dataFrame$ms, |
| 234 | digits=3, |
| 235 | big.mark = ",", |
| 236 | scientific = FALSE ) ), |
| 237 | size = 7.0, |
| 238 | fontface = "bold" ) |
| 239 | |
| 240 | result <- fundamentalGraphData + |
| 241 | barGraphFormat + |
| 242 | errorBarFormat + |
| 243 | values |
| 244 | |
| 245 | # ----------------------- |
| 246 | # Exporting Graph to File |
| 247 | # ----------------------- |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 248 | |
| 249 | print( paste( "Saving bar chart with error bars to", errBarOutputFile ) ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 250 | |
Jeremy Ronquillo | 9ea85d0 | 2017-11-27 10:21:03 -0800 | [diff] [blame] | 251 | tryCatch( ggsave( errBarOutputFile, |
| 252 | width = imageWidth, |
| 253 | height = imageHeight, |
| 254 | dpi = imageDPI ), |
| 255 | error = function( e ){ |
| 256 | print( "[ERROR] There was a problem saving the graph due to a graph formatting exception. Error dump:" ) |
| 257 | print( e ) |
| 258 | quit( status = 1 ) |
| 259 | } |
| 260 | ) |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 261 | |
Devin Lim | 0e96716 | 2017-11-03 15:59:53 -0700 | [diff] [blame] | 262 | print( paste( "[SUCCESS] Successfully wrote bar chart out to", errBarOutputFile ) ) |
Jeremy Ronquillo | 9ea85d0 | 2017-11-27 10:21:03 -0800 | [diff] [blame] | 263 | quit( status = 0 ) |