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 | |
| 27 | import fileinput |
| 28 | import json |
| 29 | import matplotlib.pyplot as plt |
| 30 | |
| 31 | LINE_PREFIX = 'PLOTTABLE_DATA: ' |
| 32 | |
| 33 | GRAPH_NAME = 'graph_name' |
| 34 | TRACE_NAME = 'trace_name' |
| 35 | UNITS = 'units' |
| 36 | |
| 37 | MICROSECONDS_IN_SECOND = 1e6 |
| 38 | |
| 39 | |
| 40 | def main(): |
| 41 | metrics = [] |
| 42 | for line in fileinput.input(): |
| 43 | line = line.strip() |
| 44 | if line.startswith(LINE_PREFIX): |
| 45 | line = line.replace(LINE_PREFIX, '') |
| 46 | metrics.append(json.loads(line)) |
| 47 | else: |
| 48 | print line |
| 49 | |
| 50 | for metric in metrics: |
| 51 | figure = plt.figure() |
| 52 | figure.canvas.set_window_title(metric[TRACE_NAME]) |
| 53 | |
| 54 | x_values = [] |
| 55 | y_values = [] |
| 56 | start_x = None |
| 57 | for sample in metric['samples']: |
| 58 | if start_x is None: |
| 59 | start_x = sample['time'] |
| 60 | # Time is us, we want to show it in seconds. |
| 61 | x_values.append((sample['time'] - start_x) / MICROSECONDS_IN_SECOND) |
| 62 | y_values.append(sample['value']) |
| 63 | |
| 64 | plt.ylabel('%s (%s)' % (metric[GRAPH_NAME], metric[UNITS])) |
Artem Titov | 3873927 | 2019-09-13 13:35:32 +0200 | [diff] [blame] | 65 | plt.xlabel('time (s)') |
Artem Titov | 5f15f86 | 2019-09-13 10:26:20 +0200 | [diff] [blame] | 66 | plt.title(metric[GRAPH_NAME]) |
| 67 | plt.plot(x_values, y_values) |
| 68 | |
| 69 | plt.show() |
| 70 | |
| 71 | |
| 72 | if __name__ == '__main__': |
| 73 | main() |