3 ##############################################################################
4 # Copyright (c) 2015 Ericsson AB and others.
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 ##############################################################################
12 ''' yardstick-plot - a command line tool for visualizing results from the
13 output file of yardstick framework.
16 $ yardstick-plot -i /tmp/yardstick.out -o /tmp/plots/
24 import matplotlib.pyplot as plt
25 import matplotlib.lines as mlines
29 ''' Command-line argument and input file parser for yardstick-plot tool'''
38 self.default_input_loc = "/tmp/yardstick.out"
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())
50 help="The input file name. If left unspecified then "
51 "it defaults to %s" % self.default_input_loc
54 '-o', '--output-folder',
55 help="The output folder location. If left unspecified then "
56 "it defaults to <script_directory>/plots/"
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)
68 '''parse command-line arguments'''
69 parser = self._get_parser()
70 self.args = parser.parse_args()
73 def parse_input_file(self):
74 '''parse the input test results file'''
76 input_file = self.args.input
78 print("No input file specified, reading from %s"
79 % self.default_input_loc)
80 input_file = self.default_input_loc
83 with open(input_file) as f:
85 record = json.loads(line)
86 self._add_record(record)
88 print(os.strerror(e.errno))
92 class Plotter(object):
93 '''Graph plotter for scenario-specific results from yardstick framework'''
95 def __init__(self, data, output_folder):
97 self.output_folder = output_folder
99 self.colors = ['g', 'b', 'c', 'm', 'y']
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
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)
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)
126 def _plot_ping(self, records):
127 '''ping test result interpretation and visualization on the graph'''
128 rtts = [r['benchmark']['data'] for r in records]
129 seqs = [r['benchmark']['sequence'] for r in records]
131 for i in range(0, len(rtts)):
135 plt.axvline(seqs[i], color='r')
137 # If there is a single data-point then display a bar-chart
139 plt.bar(1, rtts[0], 0.35, color=self.colors[0])
141 plt.plot(seqs, rtts, self.colors[0]+'-')
143 self._construct_legend(['rtt'])
144 plt.xlabel("sequence number")
145 plt.xticks(seqs, seqs)
146 plt.ylabel("round trip time in milliseconds (rtt)")
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'])
155 for i in range(0, len(sent)):
157 if not sent[i] or not received[i]:
160 plt.axvline(flows[i], color='r')
162 ppm = [1000000.0*(i - j)/i for i, j in zip(sent, received)]
164 # If there is a single data-point then display a bar-chart
166 plt.bar(1, ppm[0], 0.35, color=self.colors[0])
168 plt.plot(flows, ppm, self.colors[0]+'-')
170 self._construct_legend(['ppm'])
171 plt.xlabel("number of flows")
172 plt.ylabel("lost packets per million packets (ppm)")
174 def _plot_iperf3(self, records):
175 '''iperf3 test result interpretation and visualization on the graph'''
178 # If did not fail the SLA
179 if r['benchmark']['data']:
180 intervals.append(r['benchmark']['data']['intervals'])
182 intervals.append(None)
186 for i, val in enumerate(intervals):
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'])
193 # Don't know how long the failed test took, add 1 second
194 # TODO more accurate solution or replace x-axis from seconds
196 seconds.append(seconds[-1] + 1)
197 plt.axvline(seconds[-1], color='r')
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")
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)]
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"
215 if is_r_type or is_rw_type:
218 [r['benchmark']['data']['read_lat'] for r in records]
220 [float(i) for i in data['read_lat']]
223 [r['benchmark']['data']['read_bw'] for r in records]
225 [int(i) for i in data['read_bw']]
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']]
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']]
239 [r['benchmark']['data']['write_bw'] for r in records]
241 [int(i) for i in data['write_bw']]
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']]
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")
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")
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")
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")
264 self._construct_legend(['read', 'write'], obj=axl[0])
265 plt.xlabel("Sequence number")
266 plt.xticks(seqs, seqs)
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'''
272 if len(data[key]) == 1:
273 axl.bar(0.1, data[key], 0.35, color=bar_color)
275 line_style = bar_color + '-'
276 axl.plot(seqs, data[key], line_style)
278 def _construct_legend(self, legend_texts, obj=plt):
279 '''construct legend for the plot or subplot'''
283 for text in legend_texts:
284 line = mlines.Line2D([], [], color=self.colors[ci], label=text)
288 lines.append(mlines.Line2D([], [], color='r', label="SLA failed"))
290 getattr(obj, "legend")(
291 bbox_to_anchor=(0.25, 1.02, 0.75, .102),
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)")
310 if __name__ == '__main__':