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 | # Normal usage |
| 44 | # Check if sufficient args are provided. |
| 45 | if ( is.na( args[ 9 ] ) ){ |
| 46 | print( "Usage: Rscript SCPFflowTp1g.R <has-flow-obj> <database-host> <database-port> <database-user-id> <database-password> <test-name> <branch-name> <has-neighbors> <directory-to-save-graphs>" ) |
| 47 | q() # basically exit(), but in R |
| 48 | } |
| 49 | |
| 50 | # Filenames for output graphs include the testname and the graph type. |
| 51 | # See the examples below. paste() is used to concatenate strings. |
| 52 | |
| 53 | errBarOutputFile <- paste( args[ 9 ], args[ 6 ], sep="" ) |
| 54 | errBarOutputFile <- paste( errBarOutputFile, args[ 7 ], sep="_" ) |
| 55 | if ( args[ 8 ] == 'y' ){ |
| 56 | errBarOutputFile <- paste( errBarOutputFile, "all-neighbors", sep="_" ) |
| 57 | } else { |
| 58 | errBarOutputFile <- paste( errBarOutputFile, "no-neighbors", sep="_" ) |
| 59 | } |
| 60 | if ( args[ 1 ] == 'y' ){ |
| 61 | errBarOutputFile <- paste( errBarOutputFile, "flowObj", sep="_") |
| 62 | } |
| 63 | errBarOutputFile <- paste( errBarOutputFile, "_graph.jpg", sep="" ) |
| 64 | |
| 65 | print( "Reading from databases." ) |
| 66 | con <- dbConnect( dbDriver( "PostgreSQL" ), dbname="onostest", host=args[ 2 ], port=strtoi( args[ 3 ] ), user=args[ 4 ],password=args[ 5 ] ) |
| 67 | |
| 68 | commandNeighborModifier <- "" |
| 69 | flowObjModifier <- "" |
| 70 | if ( args[ 1 ] == 'y' ){ |
| 71 | flowObjModifier <- "_fobj" |
| 72 | } |
| 73 | if ( args[ 8 ] == 'y' ){ |
Jeremy Ronquillo | 2d2649d | 2017-09-14 12:53:06 -0700 | [diff] [blame] | 74 | commandNeighborModifier <- "scale=1 OR NOT " |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | command <- paste( "SELECT scale, avg( avg ), avg( std ) FROM flow_tp", flowObjModifier, sep="" ) |
Jeremy Ronquillo | 2d2649d | 2017-09-14 12:53:06 -0700 | [diff] [blame] | 78 | command <- paste( command, "_tests WHERE (", sep="" ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 79 | command <- paste( command, commandNeighborModifier, sep="" ) |
Jeremy Ronquillo | 2d2649d | 2017-09-14 12:53:06 -0700 | [diff] [blame] | 80 | command <- paste( command, "neighbors = 0 ) AND branch = '", sep="" ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 81 | command <- paste( command, args[ 7 ], sep="" ) |
| 82 | command <- paste( command, "' AND date IN ( SELECT max( date ) FROM flow_tp", sep="" ) |
| 83 | command <- paste( command, flowObjModifier, sep="" ) |
| 84 | command <- paste( command, "_tests WHERE branch='", sep="" ) |
| 85 | command <- paste( command, args[ 7 ], sep="" ) |
| 86 | command <- paste( command, "' ) GROUP BY scale ORDER BY scale", sep="" ) |
| 87 | |
| 88 | print( paste( "Sending SQL command:", command ) ) |
| 89 | |
| 90 | fileData <- dbGetQuery( con, command ) |
| 91 | |
| 92 | title <- paste( args[ 6 ], args[ 7 ], sep="_" ) |
| 93 | |
| 94 | # ********************************************************** |
| 95 | # STEP 2: Organize data. |
| 96 | # ********************************************************** |
| 97 | |
| 98 | print( "STEP 2: Organize data." ) |
| 99 | |
| 100 | # Create lists c() and organize data into their corresponding list. |
| 101 | print( "Sorting data." ) |
| 102 | colnames( fileData ) <- c( "scale", "avg", "std" ) |
| 103 | avgs <- c( fileData[ 'avg' ] ) |
| 104 | |
| 105 | # Parse lists into data frames. |
| 106 | dataFrame <- melt( avgs ) # This is where reshape2 comes in. Avgs list is converted to data frame |
| 107 | dataFrame$scale <- fileData$scale # Add node scaling to the data frame. |
| 108 | dataFrame$std <- fileData$std |
| 109 | |
| 110 | colnames( dataFrame ) <- c( "throughput", "type", "scale", "std" ) |
| 111 | |
Jeremy Ronquillo | 2d2649d | 2017-09-14 12:53:06 -0700 | [diff] [blame] | 112 | dataFrame <- na.omit( dataFrame ) # Omit any data that doesn't exist |
| 113 | |
| 114 | print( "Data Frame Results:" ) |
| 115 | print( dataFrame ) |
| 116 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 117 | # ********************************************************** |
| 118 | # STEP 3: Generate graphs. |
| 119 | # ********************************************************** |
| 120 | |
| 121 | print( "STEP 3: Generate graphs." ) |
| 122 | |
| 123 | # 1. Graph fundamental data is generated first. |
| 124 | # These are variables that apply to all of the graphs being generated, regardless of type. |
| 125 | # |
| 126 | # 2. Type specific graph data is generated. |
| 127 | # Data specific for the error bar and stacked bar graphs are generated. |
| 128 | # |
| 129 | # 3. Generate and save the graphs. |
| 130 | # Graphs are saved to the filename above, in the directory provided in command line args |
| 131 | |
| 132 | print( "Generating fundamental graph data." ) |
| 133 | |
| 134 | # Create the primary plot here. |
| 135 | # ggplot contains the following arguments: |
| 136 | # - data: the data frame that the graph will be based off of |
| 137 | # - aes: the asthetics of the graph which require: |
| 138 | # - x: x-axis values (usually node scaling) |
| 139 | # - y: y-axis values (usually time in milliseconds) |
| 140 | # - fill: the category of the colored side-by-side bars (usually type) |
Jeremy Ronquillo | 2d2649d | 2017-09-14 12:53:06 -0700 | [diff] [blame] | 141 | |
| 142 | theme_set( theme_grey( base_size = 20 ) ) # set the default text size of the graph. |
| 143 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 144 | mainPlot <- ggplot( data = dataFrame, aes( x = scale, y = throughput, ymin = throughput - std, ymax = throughput + std, fill = type ) ) |
| 145 | |
| 146 | # Formatting the plot |
| 147 | width <- 0.7 # Width of the bars. |
| 148 | xScaleConfig <- scale_x_continuous( breaks = dataFrame$scale, label = dataFrame$scale ) |
| 149 | xLabel <- xlab( "Scale" ) |
Jeremy Ronquillo | 1bdaae5 | 2017-09-22 11:39:48 -0700 | [diff] [blame] | 150 | yLabel <- ylab( "Throughput (,000 Flows/sec)" ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 151 | fillLabel <- labs( fill="Type" ) |
| 152 | chartTitle <- "Flow Throughput Test" |
| 153 | if ( args[ 1 ] == 'y' ){ |
| 154 | chartTitle <- paste( chartTitle, " with Flow Objectives", sep="" ) |
| 155 | } |
| 156 | chartTitle <- paste( chartTitle, "\nNeighbors =", sep="" ) |
| 157 | if ( args[ 8 ] == 'y' ){ |
| 158 | chartTitle <- paste( chartTitle, "Cluster Size - 1" ) |
| 159 | } else { |
| 160 | chartTitle <- paste( chartTitle, "0" ) |
| 161 | } |
| 162 | |
Jeremy Ronquillo | 2d2649d | 2017-09-14 12:53:06 -0700 | [diff] [blame] | 163 | theme <- theme( plot.title=element_text( hjust = 0.5, size = 28, face='bold' ) ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 164 | |
| 165 | # Store plot configurations as 1 variable |
| 166 | fundamentalGraphData <- mainPlot + xScaleConfig + xLabel + yLabel + fillLabel + theme |
| 167 | |
| 168 | |
| 169 | # Create the stacked bar graph with error bars. |
| 170 | # geom_bar contains: |
| 171 | # - stat: data formatting (usually "identity") |
| 172 | # - width: the width of the bar types (declared above) |
| 173 | # geom_errorbar contains similar arguments as geom_bar. |
| 174 | print( "Generating bar graph with error bars." ) |
| 175 | barGraphFormat <- geom_bar( stat = "identity", width = width, fill="#FFA94F" ) |
| 176 | errorBarFormat <- geom_errorbar( position=position_dodge( ), width = width ) |
| 177 | title <- ggtitle( paste( chartTitle, "" ) ) |
| 178 | result <- fundamentalGraphData + barGraphFormat + errorBarFormat + title |
| 179 | |
| 180 | # Save graph to file |
| 181 | print( paste( "Saving bar chart with error bars to", errBarOutputFile ) ) |
| 182 | ggsave( errBarOutputFile, width = 10, height = 6, dpi = 200 ) |
| 183 | print( paste( "Successfully wrote bar chart with error bars out to", errBarOutputFile ) ) |