blob: 37a9d63546ee2bc2e2b884f0dfb15c232e2d8b34 [file] [log] [blame]
Artem Titov5f15f862019-09-13 10:26:20 +02001#!/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 15:14:06 +020027import argparse
Artem Titov5f15f862019-09-13 10:26:20 +020028import 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():
Artem Titov0b9e3542020-09-29 15:14:06 +020042 parser = argparse.ArgumentParser(
43 description='Plots metrics exported from WebRTC perf tests')
44 parser.add_argument('-m', '--metrics', type=str, nargs='*',
45 help='Metrics to plot. If nothing specified then will plot all available')
46 args = parser.parse_args()
47
48 metrics_to_plot = set()
49 if args.metrics:
50 for metric in args.metrics:
51 metrics_to_plot.add(metric)
52
Artem Titov5f15f862019-09-13 10:26:20 +020053 metrics = []
Artem Titov0b9e3542020-09-29 15:14:06 +020054 for line in fileinput.input('-'):
Artem Titov5f15f862019-09-13 10:26:20 +020055 line = line.strip()
56 if line.startswith(LINE_PREFIX):
57 line = line.replace(LINE_PREFIX, '')
58 metrics.append(json.loads(line))
59 else:
60 print line
61
62 for metric in metrics:
Artem Titov0b9e3542020-09-29 15:14:06 +020063 if len(metrics_to_plot) > 0 and metric[GRAPH_NAME] not in metrics_to_plot:
64 continue
65
Artem Titov5f15f862019-09-13 10:26:20 +020066 figure = plt.figure()
67 figure.canvas.set_window_title(metric[TRACE_NAME])
68
69 x_values = []
70 y_values = []
71 start_x = None
Artem Titov0b9e3542020-09-29 15:14:06 +020072 samples = metric['samples']
73 samples.sort(key=lambda x: x['time'])
74 for sample in samples:
Artem Titov5f15f862019-09-13 10:26:20 +020075 if start_x is None:
76 start_x = sample['time']
77 # Time is us, we want to show it in seconds.
78 x_values.append((sample['time'] - start_x) / MICROSECONDS_IN_SECOND)
79 y_values.append(sample['value'])
80
81 plt.ylabel('%s (%s)' % (metric[GRAPH_NAME], metric[UNITS]))
Artem Titov38739272019-09-13 13:35:32 +020082 plt.xlabel('time (s)')
Artem Titov5f15f862019-09-13 10:26:20 +020083 plt.title(metric[GRAPH_NAME])
84 plt.plot(x_values, y_values)
85
86 plt.show()
87
88
89if __name__ == '__main__':
90 main()