blob: 804bc8fd71996808e8e097728dae07af05d6d1d5 [file] [log] [blame]
Artem Titov5f15f862019-09-13 08:26:201#!/usr/bin/env python
2# Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
3#
4# Use of this source code is governed by a BSD-style license
5# that can be found in the LICENSE file in the root of the source
6# tree. An additional intellectual property rights grant can be found
7# in the file PATENTS. All contributing project authors may
8# be found in the AUTHORS file in the root of the source tree.
9"""Plots metrics from stdin.
10
11Expected format:
12PLOTTABLE_DATA: <json data>
13Where json data has the following format:
14{
15 "graph_name": "<graph name>",
16 "trace_name": "<test suite name>",
17 "units": "<units>",
18 "mean": <mean value>,
19 "std": <standard deviation value>,
20 "samples": [
21 { "time": <sample time in us>, "value": <sample value> },
22 ...
23 ]
24}
25"""
26
Artem Titov0b9e3542020-09-29 13:14:0627import argparse
Artem Titov5f15f862019-09-13 08:26:2028import fileinput
29import json
30import matplotlib.pyplot as plt
31
32LINE_PREFIX = 'PLOTTABLE_DATA: '
33
34GRAPH_NAME = 'graph_name'
35TRACE_NAME = 'trace_name'
36UNITS = 'units'
37
38MICROSECONDS_IN_SECOND = 1e6
39
40
41def main():
Mirko Bonadei8cc66952020-10-30 09:13:4542 parser = argparse.ArgumentParser(
43 description='Plots metrics exported from WebRTC perf tests')
44 parser.add_argument(
45 '-m',
46 '--metrics',
47 type=str,
48 nargs='*',
49 help=
50 'Metrics to plot. If nothing specified then will plot all available')
51 args = parser.parse_args()
Artem Titov0b9e3542020-09-29 13:14:0652
Mirko Bonadei8cc66952020-10-30 09:13:4553 metrics_to_plot = set()
54 if args.metrics:
55 for metric in args.metrics:
56 metrics_to_plot.add(metric)
Artem Titov0b9e3542020-09-29 13:14:0657
Mirko Bonadei8cc66952020-10-30 09:13:4558 metrics = []
59 for line in fileinput.input('-'):
60 line = line.strip()
61 if line.startswith(LINE_PREFIX):
62 line = line.replace(LINE_PREFIX, '')
63 metrics.append(json.loads(line))
64 else:
65 print line
Artem Titov5f15f862019-09-13 08:26:2066
Mirko Bonadei8cc66952020-10-30 09:13:4567 for metric in metrics:
68 if len(metrics_to_plot
69 ) > 0 and metric[GRAPH_NAME] not in metrics_to_plot:
70 continue
Artem Titov0b9e3542020-09-29 13:14:0671
Mirko Bonadei8cc66952020-10-30 09:13:4572 figure = plt.figure()
73 figure.canvas.set_window_title(metric[TRACE_NAME])
Artem Titov5f15f862019-09-13 08:26:2074
Mirko Bonadei8cc66952020-10-30 09:13:4575 x_values = []
76 y_values = []
77 start_x = None
78 samples = metric['samples']
79 samples.sort(key=lambda x: x['time'])
80 for sample in samples:
81 if start_x is None:
82 start_x = sample['time']
83 # Time is us, we want to show it in seconds.
84 x_values.append(
85 (sample['time'] - start_x) / MICROSECONDS_IN_SECOND)
86 y_values.append(sample['value'])
Artem Titov5f15f862019-09-13 08:26:2087
Mirko Bonadei8cc66952020-10-30 09:13:4588 plt.ylabel('%s (%s)' % (metric[GRAPH_NAME], metric[UNITS]))
89 plt.xlabel('time (s)')
90 plt.title(metric[GRAPH_NAME])
Mirko Bonadei824eeba2021-09-09 18:20:1091 plt.plot(x_values, y_values, marker='x', markersize=3,
92 markeredgecolor='red')
Artem Titov5f15f862019-09-13 08:26:2093
Mirko Bonadei8cc66952020-10-30 09:13:4594 plt.show()
Artem Titov5f15f862019-09-13 08:26:2095
96
97if __name__ == '__main__':
Mirko Bonadei8cc66952020-10-30 09:13:4598 main()