Artem Titov | 5f15f86 | 2019-09-13 08:26:20 | [diff] [blame] | 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 | |
| 11 | Expected format: |
| 12 | PLOTTABLE_DATA: <json data> |
| 13 | Where 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 Titov | 0b9e354 | 2020-09-29 13:14:06 | [diff] [blame] | 27 | import argparse |
Artem Titov | 5f15f86 | 2019-09-13 08:26:20 | [diff] [blame] | 28 | import fileinput |
| 29 | import json |
| 30 | import matplotlib.pyplot as plt |
| 31 | |
| 32 | LINE_PREFIX = 'PLOTTABLE_DATA: ' |
| 33 | |
| 34 | GRAPH_NAME = 'graph_name' |
| 35 | TRACE_NAME = 'trace_name' |
| 36 | UNITS = 'units' |
| 37 | |
| 38 | MICROSECONDS_IN_SECOND = 1e6 |
| 39 | |
| 40 | |
| 41 | def main(): |
Mirko Bonadei | 8cc6695 | 2020-10-30 09:13:45 | [diff] [blame] | 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() |
Artem Titov | 0b9e354 | 2020-09-29 13:14:06 | [diff] [blame] | 52 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 09:13:45 | [diff] [blame] | 53 | metrics_to_plot = set() |
| 54 | if args.metrics: |
| 55 | for metric in args.metrics: |
| 56 | metrics_to_plot.add(metric) |
Artem Titov | 0b9e354 | 2020-09-29 13:14:06 | [diff] [blame] | 57 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 09:13:45 | [diff] [blame] | 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 |
Artem Titov | 5f15f86 | 2019-09-13 08:26:20 | [diff] [blame] | 66 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 09:13:45 | [diff] [blame] | 67 | for metric in metrics: |
| 68 | if len(metrics_to_plot |
| 69 | ) > 0 and metric[GRAPH_NAME] not in metrics_to_plot: |
| 70 | continue |
Artem Titov | 0b9e354 | 2020-09-29 13:14:06 | [diff] [blame] | 71 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 09:13:45 | [diff] [blame] | 72 | figure = plt.figure() |
| 73 | figure.canvas.set_window_title(metric[TRACE_NAME]) |
Artem Titov | 5f15f86 | 2019-09-13 08:26:20 | [diff] [blame] | 74 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 09:13:45 | [diff] [blame] | 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']) |
Artem Titov | 5f15f86 | 2019-09-13 08:26:20 | [diff] [blame] | 87 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 09:13:45 | [diff] [blame] | 88 | plt.ylabel('%s (%s)' % (metric[GRAPH_NAME], metric[UNITS])) |
| 89 | plt.xlabel('time (s)') |
| 90 | plt.title(metric[GRAPH_NAME]) |
Mirko Bonadei | 824eeba | 2021-09-09 18:20:10 | [diff] [blame] | 91 | plt.plot(x_values, y_values, marker='x', markersize=3, |
| 92 | markeredgecolor='red') |
Artem Titov | 5f15f86 | 2019-09-13 08:26:20 | [diff] [blame] | 93 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 09:13:45 | [diff] [blame] | 94 | plt.show() |
Artem Titov | 5f15f86 | 2019-09-13 08:26:20 | [diff] [blame] | 95 | |
| 96 | |
| 97 | if __name__ == '__main__': |
Mirko Bonadei | 8cc6695 | 2020-10-30 09:13:45 | [diff] [blame] | 98 | main() |