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