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 fileinput
28import json
29import matplotlib.pyplot as plt
30
31LINE_PREFIX = 'PLOTTABLE_DATA: '
32
33GRAPH_NAME = 'graph_name'
34TRACE_NAME = 'trace_name'
35UNITS = 'units'
36
37MICROSECONDS_IN_SECOND = 1e6
38
39
40def 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]))
65    plt.xlabel('time (s)')
66    plt.title(metric[GRAPH_NAME])
67    plt.plot(x_values, y_values)
68
69  plt.show()
70
71
72if __name__ == '__main__':
73  main()
74