xref: /aosp_15_r20/external/webrtc/rtc_tools/metrics_plotter.py (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1#!/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
27import argparse
28import 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():
42    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()
52
53    metrics_to_plot = set()
54    if args.metrics:
55        for metric in args.metrics:
56            metrics_to_plot.add(metric)
57
58    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
66
67    for metric in metrics:
68        if len(metrics_to_plot
69               ) > 0 and metric[GRAPH_NAME] not in metrics_to_plot:
70            continue
71
72        figure = plt.figure()
73        figure.canvas.set_window_title(metric[TRACE_NAME])
74
75        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'])
87
88        plt.ylabel('%s (%s)' % (metric[GRAPH_NAME], metric[UNITS]))
89        plt.xlabel('time (s)')
90        plt.title(metric[GRAPH_NAME])
91        plt.plot(x_values, y_values, marker='x', markersize=3,
92                 markeredgecolor='red')
93
94    plt.show()
95
96
97if __name__ == '__main__':
98    main()
99