Create yardstick nova flavor for CI testing
[yardstick.git] / yardstick / plot / plotter.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2015 Ericsson AB and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11
12 ''' yardstick-plot - a command line tool for visualizing results from the
13     output file of yardstick framework.
14
15     Example invocation:
16     $ yardstick-plot -i /tmp/yardstick.out -o /tmp/plots/
17 '''
18
19 import argparse
20 import json
21 import os
22 import sys
23 import time
24 import matplotlib.pyplot as plt
25 import matplotlib.lines as mlines
26
27
28 class Parser(object):
29     ''' Command-line argument and input file parser for yardstick-plot tool'''
30
31     def __init__(self):
32         self.data = {
33             'ping': [],
34             'pktgen': [],
35             'iperf3': [],
36             'fio': []
37         }
38         self.default_input_loc = "/tmp/yardstick.out"
39
40     def _get_parser(self):
41         '''get a command-line parser'''
42         parser = argparse.ArgumentParser(
43             prog='yardstick-plot',
44             description="A tool for visualizing results from yardstick. "
45                         "Currently supports plotting graphs for output files "
46                         "from tests: " + str(self.data.keys())
47         )
48         parser.add_argument(
49             '-i', '--input',
50             help="The input file name. If left unspecified then "
51                  "it defaults to %s" % self.default_input_loc
52         )
53         parser.add_argument(
54             '-o', '--output-folder',
55             help="The output folder location. If left unspecified then "
56                  "it defaults to <script_directory>/plots/"
57         )
58         return parser
59
60     def _add_record(self, record):
61         '''add record to the relevant scenario'''
62         runner_object = record['sargs']['runner']['object']
63         for test_type in self.data.keys():
64             if test_type in runner_object:
65                 self.data[test_type].append(record)
66
67     def parse_args(self):
68         '''parse command-line arguments'''
69         parser = self._get_parser()
70         self.args = parser.parse_args()
71         return self.args
72
73     def parse_input_file(self):
74         '''parse the input test results file'''
75         if self.args.input:
76             input_file = self.args.input
77         else:
78             print("No input file specified, reading from %s"
79                   % self.default_input_loc)
80             input_file = self.default_input_loc
81
82         try:
83             with open(input_file) as f:
84                 for line in f:
85                     record = json.loads(line)
86                     self._add_record(record)
87         except IOError as e:
88             print(os.strerror(e.errno))
89             sys.exit(1)
90
91
92 class Plotter(object):
93     '''Graph plotter for scenario-specific results from yardstick framework'''
94
95     def __init__(self, data, output_folder):
96         self.data = data
97         self.output_folder = output_folder
98         self.fig_counter = 1
99         self.colors = ['g', 'b', 'c', 'm', 'y']
100
101     def plot(self):
102         '''plot the graph(s)'''
103         for test_type in self.data.keys():
104             if self.data[test_type]:
105                 plt.figure(self.fig_counter)
106                 self.fig_counter += 1
107
108                 plt.title(test_type, loc="left")
109                 method_name = "_plot_" + test_type
110                 getattr(self, method_name)(self.data[test_type])
111                 self._save_plot(test_type)
112
113     def _save_plot(self, test_type):
114         '''save the graph to output folder'''
115         timestr = time.strftime("%Y%m%d-%H%M%S")
116         file_name = test_type + "_" + timestr + ".png"
117         if not self.output_folder:
118             curr_path = os.path.dirname(os.path.abspath(__file__))
119             self.output_folder = os.path.join(curr_path, "plots")
120         if not os.path.isdir(self.output_folder):
121             os.makedirs(self.output_folder)
122         new_file = os.path.join(self.output_folder, file_name)
123         plt.savefig(new_file)
124         print("Saved graph to " + new_file)
125
126     def _plot_ping(self, records):
127         '''ping test result interpretation and visualization on the graph'''
128         rtts = [r['benchmark']['data']['rtt'] for r in records]
129         seqs = [r['benchmark']['sequence'] for r in records]
130
131         for i in range(0, len(rtts)):
132             # If SLA failed
133             if not rtts[i]:
134                 rtts[i] = 0.0
135                 plt.axvline(seqs[i], color='r')
136
137         # If there is a single data-point then display a bar-chart
138         if len(rtts) == 1:
139             plt.bar(1, rtts[0], 0.35, color=self.colors[0])
140         else:
141             plt.plot(seqs, rtts, self.colors[0]+'-')
142
143         self._construct_legend(['rtt'])
144         plt.xlabel("sequence number")
145         plt.xticks(seqs, seqs)
146         plt.ylabel("round trip time in milliseconds (rtt)")
147
148     def _plot_pktgen(self, records):
149         '''pktgen test result interpretation and visualization on the graph'''
150         flows = [r['benchmark']['data']['flows'] for r in records]
151         sent = [r['benchmark']['data']['packets_sent'] for r in records]
152         received = [int(r['benchmark']['data']['packets_received'])
153                     for r in records]
154
155         for i in range(0, len(sent)):
156             # If SLA failed
157             if not sent[i] or not received[i]:
158                 sent[i] = 0.0
159                 received[i] = 0.0
160                 plt.axvline(flows[i], color='r')
161
162         ppm = [1000000.0*(i - j)/i for i, j in zip(sent, received)]
163
164         # If there is a single data-point then display a bar-chart
165         if len(ppm) == 1:
166             plt.bar(1, ppm[0], 0.35, color=self.colors[0])
167         else:
168             plt.plot(flows, ppm, self.colors[0]+'-')
169
170         self._construct_legend(['ppm'])
171         plt.xlabel("number of flows")
172         plt.ylabel("lost packets per million packets (ppm)")
173
174     def _plot_iperf3(self, records):
175         '''iperf3 test result interpretation and visualization on the graph'''
176         intervals = []
177         for r in records:
178             #  If did not fail the SLA
179             if r['benchmark']['data']:
180                 intervals.append(r['benchmark']['data']['intervals'])
181             else:
182                 intervals.append(None)
183
184         kbps = [0]
185         seconds = [0]
186         for i, val in enumerate(intervals):
187             if val:
188                 for j, _ in enumerate(intervals):
189                     kbps.append(val[j]['sum']['bits_per_second']/1000)
190                     seconds.append(seconds[-1] + val[j]['sum']['seconds'])
191             else:
192                 kbps.append(0.0)
193                 # Don't know how long the failed test took, add 1 second
194                 # TODO more accurate solution or replace x-axis from seconds
195                 # to measurement nr
196                 seconds.append(seconds[-1] + 1)
197                 plt.axvline(seconds[-1], color='r')
198
199         self._construct_legend(['bandwidth'])
200         plt.plot(seconds[1:], kbps[1:], self.colors[0]+'-')
201         plt.xlabel("time in seconds")
202         plt.ylabel("bandwidth in Kb/s")
203
204     def _plot_fio(self, records):
205         '''fio test result interpretation and visualization on the graph'''
206         rw_types = [r['sargs']['options']['rw'] for r in records]
207         seqs = [x for x in range(1, len(records) + 1)]
208         data = {}
209
210         for i in range(0, len(records)):
211             is_r_type = rw_types[i] == "read" or rw_types[i] == "randread"
212             is_w_type = rw_types[i] == "write" or rw_types[i] == "randwrite"
213             is_rw_type = rw_types[i] == "rw" or rw_types[i] == "randrw"
214
215             if is_r_type or is_rw_type:
216                 # Convert to float
217                 data['read_lat'] = \
218                     [r['benchmark']['data']['read_lat'] for r in records]
219                 data['read_lat'] = \
220                     [float(i) for i in data['read_lat']]
221                 # Convert to int
222                 data['read_bw'] = \
223                     [r['benchmark']['data']['read_bw'] for r in records]
224                 data['read_bw'] =  \
225                     [int(i) for i in data['read_bw']]
226                 # Convert to int
227                 data['read_iops'] = \
228                     [r['benchmark']['data']['read_iops'] for r in records]
229                 data['read_iops'] = \
230                     [int(i) for i in data['read_iops']]
231
232             if is_w_type or is_rw_type:
233                 data['write_lat'] = \
234                     [r['benchmark']['data']['write_lat'] for r in records]
235                 data['write_lat'] = \
236                     [float(i) for i in data['write_lat']]
237
238                 data['write_bw'] = \
239                     [r['benchmark']['data']['write_bw'] for r in records]
240                 data['write_bw'] = \
241                     [int(i) for i in data['write_bw']]
242
243                 data['write_iops'] = \
244                     [r['benchmark']['data']['write_iops'] for r in records]
245                 data['write_iops'] = \
246                     [int(i) for i in data['write_iops']]
247
248         # Divide the area into 3 subplots, sharing a common x-axis
249         fig, axl = plt.subplots(3, sharex=True)
250         axl[0].set_title("fio", loc="left")
251
252         self._plot_fio_helper(data, seqs, 'read_bw', self.colors[0], axl[0])
253         self._plot_fio_helper(data, seqs, 'write_bw', self.colors[1], axl[0])
254         axl[0].set_ylabel("Bandwidth in KB/s")
255
256         self._plot_fio_helper(data, seqs, 'read_iops', self.colors[0], axl[1])
257         self._plot_fio_helper(data, seqs, 'write_iops', self.colors[1], axl[1])
258         axl[1].set_ylabel("IOPS")
259
260         self._plot_fio_helper(data, seqs, 'read_lat', self.colors[0], axl[2])
261         self._plot_fio_helper(data, seqs, 'write_lat', self.colors[1], axl[2])
262         axl[2].set_ylabel("Latency in " + u"\u00B5s")
263
264         self._construct_legend(['read', 'write'], obj=axl[0])
265         plt.xlabel("Sequence number")
266         plt.xticks(seqs, seqs)
267
268     def _plot_fio_helper(self, data, seqs, key, bar_color, axl):
269         '''check if measurements exist for a key and then plot the
270            data to a given subplot'''
271         if key in data:
272             if len(data[key]) == 1:
273                 axl.bar(0.1, data[key], 0.35, color=bar_color)
274             else:
275                 line_style = bar_color + '-'
276                 axl.plot(seqs, data[key], line_style)
277
278     def _construct_legend(self, legend_texts, obj=plt):
279         '''construct legend for the plot or subplot'''
280         ci = 0
281         lines = []
282
283         for text in legend_texts:
284             line = mlines.Line2D([], [], color=self.colors[ci], label=text)
285             lines.append(line)
286             ci += 1
287
288         lines.append(mlines.Line2D([], [], color='r', label="SLA failed"))
289
290         getattr(obj, "legend")(
291             bbox_to_anchor=(0.25, 1.02, 0.75, .102),
292             loc=3,
293             borderaxespad=0.0,
294             ncol=len(lines),
295             mode="expand",
296             handles=lines
297         )
298
299
300 def main():
301     parser = Parser()
302     args = parser.parse_args()
303     print("Parsing input file")
304     parser.parse_input_file()
305     print("Initializing plotter")
306     plotter = Plotter(parser.data, args.output_folder)
307     print("Plotting graph(s)")
308     plotter.plot()
309
310 if __name__ == '__main__':
311     main()