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[ 7 ] ) ){ |
| 45 | print( "Usage: Rscript SCPFgraphGenerator <database-host> <database-port> <database-user-id> <database-password> <test-name> <branch-name> <directory-to-save-graphs>" ) |
| 46 | q() # basically exit(), but in R |
| 47 | } |
| 48 | |
| 49 | # Filenames for output graphs include the testname and the graph type. |
| 50 | # See the examples below. paste() is used to concatenate strings. |
| 51 | |
| 52 | outputFile <- paste( args[ 7 ], args[ 5 ], sep="" ) |
| 53 | outputFile <- paste( outputFile, args[ 6 ], sep="_" ) |
| 54 | outputFile <- paste( outputFile, "_graph.jpg", sep="" ) |
| 55 | |
| 56 | print( "Reading from databases." ) |
| 57 | |
| 58 | con <- dbConnect( dbDriver( "PostgreSQL" ), dbname="onostest", host=args[ 1 ], port=strtoi( args[ 2 ] ), user=args[ 3 ],password=args[ 4 ] ) |
| 59 | |
| 60 | command <- paste( "SELECT * FROM scale_topo_latency_details WHERE branch = '", args[ 6 ], sep = "" ) |
| 61 | command <- paste( command, "' AND date IN ( SELECT MAX( date ) FROM scale_topo_latency_details WHERE branch = '", sep = "" ) |
| 62 | command <- paste( command, args[ 6 ], sep = "" ) |
| 63 | command <- paste( command, "' ) ", sep="" ) |
| 64 | |
| 65 | print( paste( "Sending SQL command:", command ) ) |
| 66 | |
| 67 | fileData <- dbGetQuery( con, command ) |
| 68 | |
| 69 | title <- paste( args[ 5 ], args[ 6 ], sep="_" ) |
| 70 | |
| 71 | # ********************************************************** |
| 72 | # STEP 2: Organize data. |
| 73 | # ********************************************************** |
| 74 | |
| 75 | print( "STEP 2: Organize data." ) |
| 76 | |
| 77 | # Create lists c() and organize data into their corresponding list. |
| 78 | print( "Sorting data." ) |
| 79 | avgs <- c( fileData[ 'last_role_request_to_last_topology' ], fileData[ 'last_connection_to_last_role_request' ], fileData[ 'first_connection_to_last_connection' ] ) |
| 80 | |
| 81 | # Parse lists into data frames. |
| 82 | dataFrame <- melt( avgs ) # This is where reshape2 comes in. Avgs list is converted to data frame |
| 83 | dataFrame$scale <- fileData$scale # Add node scaling to the data frame. |
| 84 | colnames( dataFrame ) <- c( "ms", "type", "scale") |
| 85 | |
| 86 | |
| 87 | # Format data frame so that the data is in the same order as it appeared in the file. |
| 88 | dataFrame$type <- as.character( dataFrame$type ) |
| 89 | dataFrame$type <- factor( dataFrame$type, levels=unique( dataFrame$type ) ) |
| 90 | dataFrame$iterative <- seq( 1, nrow( fileData ), by = 1 ) |
| 91 | |
| 92 | # Obtain the sum of the averages for the plot size and center of standard deviation bars. |
| 93 | avgsSum <- fileData$total_time |
| 94 | |
Jeremy Ronquillo | 2d2649d | 2017-09-14 12:53:06 -0700 | [diff] [blame] | 95 | dataFrame <- na.omit( dataFrame ) # Omit any data that doesn't exist |
| 96 | |
| 97 | print( "Data Frame Results:" ) |
| 98 | print( dataFrame ) |
| 99 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 100 | # ********************************************************** |
| 101 | # STEP 3: Generate graphs. |
| 102 | # ********************************************************** |
| 103 | |
| 104 | print( "STEP 3: Generate graphs." ) |
| 105 | |
| 106 | # 1. Graph fundamental data is generated first. |
| 107 | # These are variables that apply to all of the graphs being generated, regardless of type. |
| 108 | # |
| 109 | # 2. Type specific graph data is generated. |
| 110 | # Data specific for the error bar and stacked bar graphs are generated. |
| 111 | # |
| 112 | # 3. Generate and save the graphs. |
| 113 | # Graphs are saved to the filename above, in the directory provided in command line args |
| 114 | |
| 115 | print( "Generating fundamental graph data." ) |
| 116 | |
| 117 | # Calculate window to display graph, based on the lowest and highest points of the data. |
| 118 | if ( min( avgsSum ) < 0){ |
| 119 | yWindowMin <- min( avgsSum ) * 1.05 |
| 120 | } else { |
| 121 | yWindowMin <- 0 |
| 122 | } |
| 123 | yWindowMax <- max( avgsSum ) |
| 124 | |
Jeremy Ronquillo | 2d2649d | 2017-09-14 12:53:06 -0700 | [diff] [blame] | 125 | theme_set( theme_grey( base_size = 20 ) ) # set the default text size of the graph. |
| 126 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 127 | # Create the primary plot here. |
| 128 | # ggplot contains the following arguments: |
| 129 | # - data: the data frame that the graph will be based off of |
| 130 | # - aes: the asthetics of the graph which require: |
| 131 | # - x: x-axis values (usually node scaling) |
| 132 | # - y: y-axis values (usually time in milliseconds) |
| 133 | # - fill: the category of the colored side-by-side bars (usually type) |
| 134 | mainPlot <- ggplot( data = dataFrame, aes( x = iterative, y = ms, fill = type ) ) |
| 135 | |
| 136 | # Formatting the plot |
| 137 | width <- 0.6 # Width of the bars. |
| 138 | xScaleConfig <- scale_x_continuous( breaks = dataFrame$iterative, label = dataFrame$scale ) |
| 139 | yLimit <- ylim( yWindowMin, yWindowMax ) |
| 140 | xLabel <- xlab( "Scale" ) |
| 141 | yLabel <- ylab( "Latency (ms)" ) |
| 142 | fillLabel <- labs( fill="Type" ) |
Jeremy Ronquillo | 2d2649d | 2017-09-14 12:53:06 -0700 | [diff] [blame] | 143 | chartTitle <- paste( "Scale Topology Latency Test" ) |
| 144 | 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] | 145 | |
| 146 | # Store plot configurations as 1 variable |
| 147 | fundamentalGraphData <- mainPlot + xScaleConfig + yLimit + xLabel + yLabel + fillLabel + theme |
| 148 | |
| 149 | # Create the stacked bar graph with error bars. |
| 150 | # geom_bar contains: |
| 151 | # - stat: data formatting (usually "identity") |
| 152 | # - width: the width of the bar types (declared above) |
| 153 | # geom_errorbar contains similar arguments as geom_bar. |
| 154 | print( "Generating bar graph with error bars." ) |
| 155 | barGraphFormat <- geom_bar( stat = "identity", width = width ) |
| 156 | title <- ggtitle( paste( chartTitle, "" ) ) |
| 157 | result <- fundamentalGraphData + barGraphFormat + title |
| 158 | |
| 159 | # Save graph to file |
| 160 | print( paste( "Saving bar chart with error bars to", outputFile ) ) |
| 161 | ggsave( outputFile, width = 10, height = 6, dpi = 200 ) |
| 162 | print( paste( "Successfully wrote bar chart with error bars out to", outputFile ) ) |