Artem Titov | 5f15f86 | 2019-09-13 10:26:20 +0200 | [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 15:14:06 +0200 | [diff] [blame] | 27 | import argparse |
Artem Titov | 5f15f86 | 2019-09-13 10:26:20 +0200 | [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(): |
Artem Titov | 0b9e354 | 2020-09-29 15:14:06 +0200 | [diff] [blame] | 42 | 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 Titov | 5f15f86 | 2019-09-13 10:26:20 +0200 | [diff] [blame] | 53 | metrics = [] |
Artem Titov | 0b9e354 | 2020-09-29 15:14:06 +0200 | [diff] [blame] | 54 | for line in fileinput.input('-'): |
Artem Titov | 5f15f86 | 2019-09-13 10:26:20 +0200 | [diff] [blame] | 55 | 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 Titov | 0b9e354 | 2020-09-29 15:14:06 +0200 | [diff] [blame] | 63 | if len(metrics_to_plot) > 0 and metric[GRAPH_NAME] not in metrics_to_plot: |
| 64 | continue |
| 65 | |
Artem Titov | 5f15f86 | 2019-09-13 10:26:20 +0200 | [diff] [blame] | 66 | figure = plt.figure() |
| 67 | figure.canvas.set_window_title(metric[TRACE_NAME]) |
| 68 | |
| 69 | x_values = [] |
| 70 | y_values = [] |
| 71 | start_x = None |
Artem Titov | 0b9e354 | 2020-09-29 15:14:06 +0200 | [diff] [blame] | 72 | samples = metric['samples'] |
| 73 | samples.sort(key=lambda x: x['time']) |
| 74 | for sample in samples: |
Artem Titov | 5f15f86 | 2019-09-13 10:26:20 +0200 | [diff] [blame] | 75 | 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 Titov | 3873927 | 2019-09-13 13:35:32 +0200 | [diff] [blame] | 82 | plt.xlabel('time (s)') |
Artem Titov | 5f15f86 | 2019-09-13 10:26:20 +0200 | [diff] [blame] | 83 | plt.title(metric[GRAPH_NAME]) |
| 84 | plt.plot(x_values, y_values) |
| 85 | |
| 86 | plt.show() |
| 87 | |
| 88 | |
| 89 | if __name__ == '__main__': |
| 90 | main() |