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[ 7 ] ) ){ |
| 46 | print( "Usage: Rscript SCPFmastershipFailoverLat <database-host> <database-port> <database-user-id> <database-password> <test-name> <branch-name> <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[ 7 ], args[ 5 ], sep="" ) |
| 54 | errBarOutputFile <- paste( errBarOutputFile, args[ 6 ], sep="_" ) |
| 55 | errBarOutputFile <- paste( errBarOutputFile, "_errGraph.jpg", sep="" ) |
| 56 | |
| 57 | stackedBarOutputFile <- paste( args[ 7 ], args[ 5 ], sep="" ) |
| 58 | stackedBarOutputFile <- paste( stackedBarOutputFile, args[ 6 ], sep="_" ) |
| 59 | stackedBarOutputFile <- paste( stackedBarOutputFile, "_stackedGraph.jpg", sep="" ) |
| 60 | |
| 61 | print( "Reading from databases." ) |
| 62 | |
| 63 | con <- dbConnect( dbDriver( "PostgreSQL" ), dbname="onostest", host=args[ 1 ], port=strtoi( args[ 2 ] ), user=args[ 3 ],password=args[ 4 ] ) |
| 64 | |
| 65 | command <- paste( "SELECT * FROM mastership_failover_tests WHERE branch = '", args[ 6 ], sep = "" ) |
| 66 | command <- paste( command, "' AND date IN ( SELECT MAX( date ) FROM mastership_failover_tests WHERE branch = '", sep = "" ) |
| 67 | command <- paste( command, args[ 6 ], sep = "" ) |
| 68 | command <- paste( command, "' ) ", sep="" ) |
| 69 | |
| 70 | print( paste( "Sending SQL command:", command ) ) |
| 71 | |
| 72 | fileData <- dbGetQuery( con, command ) |
| 73 | |
| 74 | chartTitle <- "Mastership Failover Latency" |
| 75 | |
| 76 | |
| 77 | # ********************************************************** |
| 78 | # STEP 2: Organize data. |
| 79 | # ********************************************************** |
| 80 | |
| 81 | fileDataNames <- names( fileData ) |
| 82 | |
| 83 | avgs <- c() |
| 84 | stds <- c() |
| 85 | |
| 86 | |
| 87 | print( "Sorting data." ) |
| 88 | for ( name in fileDataNames ){ |
| 89 | nameLen <- nchar( name ) |
| 90 | if ( nameLen > 2 ){ |
| 91 | if ( substring( name, nameLen - 2, nameLen ) == "avg" ){ |
| 92 | avgs <- c( avgs, fileData[ name ] ) |
| 93 | } |
| 94 | if ( substring( name, nameLen - 2, nameLen ) == "std" ){ |
| 95 | stds <- c( stds, fileData[ name ] ) |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | avgData <- melt( avgs ) |
| 101 | avgData$scale <- fileData$scale |
| 102 | colnames( avgData ) <- c( "ms", "type", "scale" ) |
| 103 | |
| 104 | stdData <- melt( stds ) |
| 105 | colnames( stdData ) <- c( "ms", "type" ) |
| 106 | |
Jeremy Ronquillo | 2d2649d | 2017-09-14 12:53:06 -0700 | [diff] [blame] | 107 | dataFrame <- na.omit( avgData ) # Omit any data that doesn't exist |
| 108 | |
| 109 | print( "Data Frame Results:" ) |
| 110 | print( avgData ) |
| 111 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 112 | |
| 113 | # ********************************************************** |
| 114 | # STEP 3: Generate graphs. |
| 115 | # ********************************************************** |
| 116 | |
| 117 | print( "Generating fundamental graph data." ) |
Jeremy Ronquillo | 2d2649d | 2017-09-14 12:53:06 -0700 | [diff] [blame] | 118 | |
| 119 | theme_set( theme_grey( base_size = 20 ) ) # set the default text size of the graph. |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 120 | |
| 121 | mainPlot <- ggplot( data = avgData, aes( x = scale, y = ms, ymin = ms - stdData$ms, ymax = ms + stdData$ms,fill = type ) ) |
| 122 | xScaleConfig <- scale_x_continuous( breaks=c( 1, 3, 5, 7, 9) ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 123 | yLimit <- ylim( yMin, yMax ) |
| 124 | xLabel <- xlab( "Scale" ) |
| 125 | yLabel <- ylab( "Latency (ms)" ) |
| 126 | fillLabel <- labs( fill="Type" ) |
Jeremy Ronquillo | 2d2649d | 2017-09-14 12:53:06 -0700 | [diff] [blame] | 127 | 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] | 128 | |
| 129 | fundamentalGraphData <- mainPlot + xScaleConfig + yLimit + xLabel + yLabel + fillLabel + theme |
| 130 | |
| 131 | |
| 132 | print( "Generating bar graph with error bars." ) |
| 133 | width <- 0.9 |
| 134 | barGraphFormat <- geom_bar( stat="identity", position=position_dodge( ), width = width ) |
| 135 | errorBarFormat <- geom_errorbar( position=position_dodge( ), width = width ) |
| 136 | title <- ggtitle( paste( chartTitle, "with Standard Error Bars" ) ) |
| 137 | result <- fundamentalGraphData + barGraphFormat + errorBarFormat + title |
| 138 | |
| 139 | |
| 140 | print( paste( "Saving bar chart with error bars to", errBarOutputFile ) ) |
| 141 | ggsave( errBarOutputFile, width = 10, height = 6, dpi = 200 ) |
| 142 | |
| 143 | |
| 144 | print( paste( "Successfully wrote bar chart with error bars out to", errBarOutputFile ) ) |
| 145 | |
| 146 | |
| 147 | print( "Generating stacked bar chart." ) |
| 148 | stackedBarFormat <- geom_bar( stat="identity", width=width ) |
| 149 | title <- ggtitle( paste( chartTitle, "Total Latency" ) ) |
| 150 | result <- fundamentalGraphData + stackedBarFormat + title |
| 151 | |
| 152 | |
| 153 | print( paste( "Saving stacked bar chart to", stackedBarOutputFile ) ) |
| 154 | ggsave( stackedBarOutputFile, width = 10, height = 6, dpi = 200 ) |
| 155 | |
| 156 | |
| 157 | print( paste( "Successfully wrote stacked bar chart out to", stackedBarOutputFile ) ) |