1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/webrtc/trunk/build/android/pylib/json_perf_parser.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,160 @@ 1.4 +# Copyright (c) 2012 The Chromium Authors. All rights reserved. 1.5 +# Use of this source code is governed by a BSD-style license that can be 1.6 +# found in the LICENSE file. 1.7 + 1.8 + 1.9 +"""A helper module for parsing JSON objects from perf tests results.""" 1.10 + 1.11 +import json 1.12 + 1.13 + 1.14 +def GetAverageRunInfo(json_data, name): 1.15 + """Summarizes TraceEvent JSON data for performance metrics. 1.16 + 1.17 + Example JSON Inputs (More tags can be added but these are required): 1.18 + Measuring Duration: 1.19 + [ 1.20 + { "cat": "Java", 1.21 + "ts": 10000000000, 1.22 + "ph": "S", 1.23 + "name": "TestTrace" 1.24 + }, 1.25 + { "cat": "Java", 1.26 + "ts": 10000004000, 1.27 + "ph": "F", 1.28 + "name": "TestTrace" 1.29 + }, 1.30 + ... 1.31 + ] 1.32 + 1.33 + Measuring Call Frequency (FPS): 1.34 + [ 1.35 + { "cat": "Java", 1.36 + "ts": 10000000000, 1.37 + "ph": "I", 1.38 + "name": "TestTraceFPS" 1.39 + }, 1.40 + { "cat": "Java", 1.41 + "ts": 10000004000, 1.42 + "ph": "I", 1.43 + "name": "TestTraceFPS" 1.44 + }, 1.45 + ... 1.46 + ] 1.47 + 1.48 + Args: 1.49 + json_data: A list of dictonaries each representing a JSON object. 1.50 + name: The 'name' tag to filter on in the JSON file. 1.51 + 1.52 + Returns: 1.53 + A dictionary of result data with the following tags: 1.54 + min: The minimum value tracked. 1.55 + max: The maximum value tracked. 1.56 + average: The average of all the values tracked. 1.57 + count: The number of times the category/name pair was tracked. 1.58 + type: The type of tracking ('Instant' for instant tags and 'Span' for 1.59 + begin/end tags. 1.60 + category: The passed in category filter. 1.61 + name: The passed in name filter. 1.62 + data_points: A list of all of the times used to generate this data. 1.63 + units: The units for the values being reported. 1.64 + 1.65 + Raises: 1.66 + Exception: if entry contains invalid data. 1.67 + """ 1.68 + 1.69 + def EntryFilter(entry): 1.70 + return entry['cat'] == 'Java' and entry['name'] == name 1.71 + filtered_entries = filter(EntryFilter, json_data) 1.72 + 1.73 + result = {} 1.74 + 1.75 + result['min'] = -1 1.76 + result['max'] = -1 1.77 + result['average'] = 0 1.78 + result['count'] = 0 1.79 + result['type'] = 'Unknown' 1.80 + result['category'] = 'Java' 1.81 + result['name'] = name 1.82 + result['data_points'] = [] 1.83 + result['units'] = '' 1.84 + 1.85 + total_sum = 0 1.86 + 1.87 + last_val = 0 1.88 + val_type = None 1.89 + for entry in filtered_entries: 1.90 + if not val_type: 1.91 + if 'mem' in entry: 1.92 + val_type = 'mem' 1.93 + 1.94 + def GetVal(entry): 1.95 + return entry['mem'] 1.96 + 1.97 + result['units'] = 'kb' 1.98 + elif 'ts' in entry: 1.99 + val_type = 'ts' 1.100 + 1.101 + def GetVal(entry): 1.102 + return float(entry['ts']) / 1000.0 1.103 + 1.104 + result['units'] = 'ms' 1.105 + else: 1.106 + raise Exception('Entry did not contain valid value info: %s' % entry) 1.107 + 1.108 + if not val_type in entry: 1.109 + raise Exception('Entry did not contain expected value type "%s" ' 1.110 + 'information: %s' % (val_type, entry)) 1.111 + val = GetVal(entry) 1.112 + if (entry['ph'] == 'S' and 1.113 + (result['type'] == 'Unknown' or result['type'] == 'Span')): 1.114 + result['type'] = 'Span' 1.115 + last_val = val 1.116 + elif ((entry['ph'] == 'F' and result['type'] == 'Span') or 1.117 + (entry['ph'] == 'I' and (result['type'] == 'Unknown' or 1.118 + result['type'] == 'Instant'))): 1.119 + if last_val > 0: 1.120 + delta = val - last_val 1.121 + if result['min'] == -1 or result['min'] > delta: 1.122 + result['min'] = delta 1.123 + if result['max'] == -1 or result['max'] < delta: 1.124 + result['max'] = delta 1.125 + total_sum += delta 1.126 + result['count'] += 1 1.127 + result['data_points'].append(delta) 1.128 + if entry['ph'] == 'I': 1.129 + result['type'] = 'Instant' 1.130 + last_val = val 1.131 + if result['count'] > 0: result['average'] = total_sum / result['count'] 1.132 + 1.133 + return result 1.134 + 1.135 + 1.136 +def GetAverageRunInfoFromJSONString(json_string, name): 1.137 + """Returns the results from GetAverageRunInfo using a JSON string. 1.138 + 1.139 + Args: 1.140 + json_string: The string containing JSON. 1.141 + name: The 'name' tag to filter on in the JSON file. 1.142 + 1.143 + Returns: 1.144 + See GetAverageRunInfo Returns section. 1.145 + """ 1.146 + return GetAverageRunInfo(json.loads(json_string), name) 1.147 + 1.148 + 1.149 +def GetAverageRunInfoFromFile(json_file, name): 1.150 + """Returns the results from GetAverageRunInfo using a JSON file. 1.151 + 1.152 + Args: 1.153 + json_file: The path to a JSON file. 1.154 + name: The 'name' tag to filter on in the JSON file. 1.155 + 1.156 + Returns: 1.157 + See GetAverageRunInfo Returns section. 1.158 + """ 1.159 + with open(json_file, 'r') as f: 1.160 + data = f.read() 1.161 + perf = json.loads(data) 1.162 + 1.163 + return GetAverageRunInfo(perf, name)