blob: 9956ec8ec725fecb44768fe1e9baa6fd4155df0f [file] [log] [blame]
Jeremy Ronquillo6df87812017-08-28 16:17:36 +00001# 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
27print( "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/
32print( "Reading commmand-line args." )
33args <- 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
38print( "Importing libraries." )
39library( ggplot2 )
40library( reshape2 )
41library( RPostgreSQL ) # For databases
42
43# Check if sufficient args are provided.
44if ( 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
52outputFile <- paste( args[ 7 ], args[ 5 ], sep="" )
53outputFile <- paste( outputFile, args[ 6 ], sep="_" )
54outputFile <- paste( outputFile, "_graph.jpg", sep="" )
55
56print( "Reading from databases." )
57
58con <- dbConnect( dbDriver( "PostgreSQL" ), dbname="onostest", host=args[ 1 ], port=strtoi( args[ 2 ] ), user=args[ 3 ],password=args[ 4 ] )
59
60command <- paste( "SELECT * FROM scale_topo_latency_details WHERE branch = '", args[ 6 ], sep = "" )
61command <- paste( command, "' AND date IN ( SELECT MAX( date ) FROM scale_topo_latency_details WHERE branch = '", sep = "" )
62command <- paste( command, args[ 6 ], sep = "" )
63command <- paste( command, "' ) ", sep="" )
64
65print( paste( "Sending SQL command:", command ) )
66
67fileData <- dbGetQuery( con, command )
68
69title <- paste( args[ 5 ], args[ 6 ], sep="_" )
70
71# **********************************************************
72# STEP 2: Organize data.
73# **********************************************************
74
75print( "STEP 2: Organize data." )
76
77# Create lists c() and organize data into their corresponding list.
78print( "Sorting data." )
79avgs <- 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.
82dataFrame <- melt( avgs ) # This is where reshape2 comes in. Avgs list is converted to data frame
83dataFrame$scale <- fileData$scale # Add node scaling to the data frame.
84colnames( 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.
88dataFrame$type <- as.character( dataFrame$type )
89dataFrame$type <- factor( dataFrame$type, levels=unique( dataFrame$type ) )
90dataFrame$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.
93avgsSum <- fileData$total_time
94
95# **********************************************************
96# STEP 3: Generate graphs.
97# **********************************************************
98
99print( "STEP 3: Generate graphs." )
100
101# 1. Graph fundamental data is generated first.
102# These are variables that apply to all of the graphs being generated, regardless of type.
103#
104# 2. Type specific graph data is generated.
105# Data specific for the error bar and stacked bar graphs are generated.
106#
107# 3. Generate and save the graphs.
108# Graphs are saved to the filename above, in the directory provided in command line args
109
110print( "Generating fundamental graph data." )
111
112# Calculate window to display graph, based on the lowest and highest points of the data.
113if ( min( avgsSum ) < 0){
114 yWindowMin <- min( avgsSum ) * 1.05
115} else {
116 yWindowMin <- 0
117}
118yWindowMax <- max( avgsSum )
119
120# Create the primary plot here.
121# ggplot contains the following arguments:
122# - data: the data frame that the graph will be based off of
123# - aes: the asthetics of the graph which require:
124# - x: x-axis values (usually node scaling)
125# - y: y-axis values (usually time in milliseconds)
126# - fill: the category of the colored side-by-side bars (usually type)
127mainPlot <- ggplot( data = dataFrame, aes( x = iterative, y = ms, fill = type ) )
128
129# Formatting the plot
130width <- 0.6 # Width of the bars.
131xScaleConfig <- scale_x_continuous( breaks = dataFrame$iterative, label = dataFrame$scale )
132yLimit <- ylim( yWindowMin, yWindowMax )
133xLabel <- xlab( "Scale" )
134yLabel <- ylab( "Latency (ms)" )
135fillLabel <- labs( fill="Type" )
136chartTitle <- paste( "Topology Scaling Operation Latency" )
137theme <- theme( plot.title=element_text( hjust = 0.5, size = 18, face='bold' ) )
138
139# Store plot configurations as 1 variable
140fundamentalGraphData <- mainPlot + xScaleConfig + yLimit + xLabel + yLabel + fillLabel + theme
141
142# Create the stacked bar graph with error bars.
143# geom_bar contains:
144# - stat: data formatting (usually "identity")
145# - width: the width of the bar types (declared above)
146# geom_errorbar contains similar arguments as geom_bar.
147print( "Generating bar graph with error bars." )
148barGraphFormat <- geom_bar( stat = "identity", width = width )
149title <- ggtitle( paste( chartTitle, "" ) )
150result <- fundamentalGraphData + barGraphFormat + title
151
152# Save graph to file
153print( paste( "Saving bar chart with error bars to", outputFile ) )
154ggsave( outputFile, width = 10, height = 6, dpi = 200 )
155print( paste( "Successfully wrote bar chart with error bars out to", outputFile ) )