1# Copyright 2016 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4import argparse 5import json 6import os 7import sys 8 9from ggplot import * 10import pandas 11 12 13def _ConvertToSimplifiedFormat(values_list): 14 data = {} 15 for trace_name, result_data in values_list: 16 result_values = result_data['pairs']['values'] 17 18 values = {} 19 for current_value in result_values: 20 grouping_key = current_value['name'] 21 22 if current_value['type'] == 'numeric': 23 numeric_value = current_value['numeric'] 24 if numeric_value['type'] == 'scalar': 25 values[grouping_key] = [current_value['numeric']['value']] 26 elif numeric_value['type'] == 'numeric': 27 # Let's just skip histograms for now. 28 histogram_values = [] 29 for bin in numeric_value['centralBins']: 30 histo_mid = (bin['max'] - bin['min']) * 0.5 31 histogram_values += [histo_mid for _ in xrange(bin['count'])] 32 values[grouping_key] = histogram_values 33 34 data[trace_name] = values 35 return data 36 37 38def _ReadValuesOutput(file_name, metric_names): 39 with open(file_name, 'r') as f: 40 results_list = f.read() 41 42 try: 43 # Try metrics format first, which is a dict of results. 44 results_together = json.loads(results_list).iteritems() 45 except ValueError: 46 # Try flume pipeline output format, which is a 1 result per line. 47 r = [json.loads(s) for s in results_list.splitlines()] 48 results_together = dict([(str(i), r[i]) for i in xrange(len(r))]) 49 50 simplified_data = _ConvertToSimplifiedFormat(results_together) 51 52 return _ConvertValuesToPD(simplified_data, metric_names) 53 54 55def _ConvertValuesToPD(all_values_data, metric_names): 56 pd_dict = {} 57 58 for trace_name, values in all_values_data.iteritems(): 59 for metric_name, metric_value in values.iteritems(): 60 if not metric_name in metric_names: 61 continue 62 if not metric_name in pd_dict: 63 pd_dict[metric_name] = [] 64 pd_dict[metric_name].extend(metric_value) 65 return pandas.DataFrame(pd_dict) 66 67 68def _DoGGPlot(graph_type, data, x, y): 69 if graph_type == 'histogram': 70 print ggplot(aes(x=x), data=data) + geom_histogram() 71 elif graph_type == 'point': 72 print (ggplot(aes(x=x, y=y), data=data) + geom_point() + 73 stat_smooth(color='black', se=True)) 74 75 76def Main(): 77 # Parse options. 78 parser = argparse.ArgumentParser() 79 parser.add_argument('--x', help='X-Axis parameter.') 80 parser.add_argument('--y', help='X-Axis parameter.') 81 parser.add_argument('--graph-type', 82 choices=['histogram', 'point'], 83 help='Type of graph.') 84 parser.add_argument('--source', 85 help='Path to file containing results of metrics run.') 86 args = parser.parse_args() 87 88 if not args.source: 89 parser.error('Source file not specified. Use --source to specify ' 90 'path to source file.') 91 92 if not args.graph_type: 93 parser.error('Graph type not specified. Use --graph-type.') 94 95 metric_names = [] 96 if args.x: 97 metric_names.append(args.x) 98 if args.y: 99 metric_names.append(args.y) 100 101 data = _ReadValuesOutput(os.path.abspath(args.source), metric_names) 102 103 _DoGGPlot(args.graph_type, data, args.x, args.y) 104