Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 2 | # Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | # found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | |
michael@0 | 6 | """A helper module for parsing JSON objects from perf tests results.""" |
michael@0 | 7 | |
michael@0 | 8 | import json |
michael@0 | 9 | |
michael@0 | 10 | |
michael@0 | 11 | def GetAverageRunInfo(json_data, name): |
michael@0 | 12 | """Summarizes TraceEvent JSON data for performance metrics. |
michael@0 | 13 | |
michael@0 | 14 | Example JSON Inputs (More tags can be added but these are required): |
michael@0 | 15 | Measuring Duration: |
michael@0 | 16 | [ |
michael@0 | 17 | { "cat": "Java", |
michael@0 | 18 | "ts": 10000000000, |
michael@0 | 19 | "ph": "S", |
michael@0 | 20 | "name": "TestTrace" |
michael@0 | 21 | }, |
michael@0 | 22 | { "cat": "Java", |
michael@0 | 23 | "ts": 10000004000, |
michael@0 | 24 | "ph": "F", |
michael@0 | 25 | "name": "TestTrace" |
michael@0 | 26 | }, |
michael@0 | 27 | ... |
michael@0 | 28 | ] |
michael@0 | 29 | |
michael@0 | 30 | Measuring Call Frequency (FPS): |
michael@0 | 31 | [ |
michael@0 | 32 | { "cat": "Java", |
michael@0 | 33 | "ts": 10000000000, |
michael@0 | 34 | "ph": "I", |
michael@0 | 35 | "name": "TestTraceFPS" |
michael@0 | 36 | }, |
michael@0 | 37 | { "cat": "Java", |
michael@0 | 38 | "ts": 10000004000, |
michael@0 | 39 | "ph": "I", |
michael@0 | 40 | "name": "TestTraceFPS" |
michael@0 | 41 | }, |
michael@0 | 42 | ... |
michael@0 | 43 | ] |
michael@0 | 44 | |
michael@0 | 45 | Args: |
michael@0 | 46 | json_data: A list of dictonaries each representing a JSON object. |
michael@0 | 47 | name: The 'name' tag to filter on in the JSON file. |
michael@0 | 48 | |
michael@0 | 49 | Returns: |
michael@0 | 50 | A dictionary of result data with the following tags: |
michael@0 | 51 | min: The minimum value tracked. |
michael@0 | 52 | max: The maximum value tracked. |
michael@0 | 53 | average: The average of all the values tracked. |
michael@0 | 54 | count: The number of times the category/name pair was tracked. |
michael@0 | 55 | type: The type of tracking ('Instant' for instant tags and 'Span' for |
michael@0 | 56 | begin/end tags. |
michael@0 | 57 | category: The passed in category filter. |
michael@0 | 58 | name: The passed in name filter. |
michael@0 | 59 | data_points: A list of all of the times used to generate this data. |
michael@0 | 60 | units: The units for the values being reported. |
michael@0 | 61 | |
michael@0 | 62 | Raises: |
michael@0 | 63 | Exception: if entry contains invalid data. |
michael@0 | 64 | """ |
michael@0 | 65 | |
michael@0 | 66 | def EntryFilter(entry): |
michael@0 | 67 | return entry['cat'] == 'Java' and entry['name'] == name |
michael@0 | 68 | filtered_entries = filter(EntryFilter, json_data) |
michael@0 | 69 | |
michael@0 | 70 | result = {} |
michael@0 | 71 | |
michael@0 | 72 | result['min'] = -1 |
michael@0 | 73 | result['max'] = -1 |
michael@0 | 74 | result['average'] = 0 |
michael@0 | 75 | result['count'] = 0 |
michael@0 | 76 | result['type'] = 'Unknown' |
michael@0 | 77 | result['category'] = 'Java' |
michael@0 | 78 | result['name'] = name |
michael@0 | 79 | result['data_points'] = [] |
michael@0 | 80 | result['units'] = '' |
michael@0 | 81 | |
michael@0 | 82 | total_sum = 0 |
michael@0 | 83 | |
michael@0 | 84 | last_val = 0 |
michael@0 | 85 | val_type = None |
michael@0 | 86 | for entry in filtered_entries: |
michael@0 | 87 | if not val_type: |
michael@0 | 88 | if 'mem' in entry: |
michael@0 | 89 | val_type = 'mem' |
michael@0 | 90 | |
michael@0 | 91 | def GetVal(entry): |
michael@0 | 92 | return entry['mem'] |
michael@0 | 93 | |
michael@0 | 94 | result['units'] = 'kb' |
michael@0 | 95 | elif 'ts' in entry: |
michael@0 | 96 | val_type = 'ts' |
michael@0 | 97 | |
michael@0 | 98 | def GetVal(entry): |
michael@0 | 99 | return float(entry['ts']) / 1000.0 |
michael@0 | 100 | |
michael@0 | 101 | result['units'] = 'ms' |
michael@0 | 102 | else: |
michael@0 | 103 | raise Exception('Entry did not contain valid value info: %s' % entry) |
michael@0 | 104 | |
michael@0 | 105 | if not val_type in entry: |
michael@0 | 106 | raise Exception('Entry did not contain expected value type "%s" ' |
michael@0 | 107 | 'information: %s' % (val_type, entry)) |
michael@0 | 108 | val = GetVal(entry) |
michael@0 | 109 | if (entry['ph'] == 'S' and |
michael@0 | 110 | (result['type'] == 'Unknown' or result['type'] == 'Span')): |
michael@0 | 111 | result['type'] = 'Span' |
michael@0 | 112 | last_val = val |
michael@0 | 113 | elif ((entry['ph'] == 'F' and result['type'] == 'Span') or |
michael@0 | 114 | (entry['ph'] == 'I' and (result['type'] == 'Unknown' or |
michael@0 | 115 | result['type'] == 'Instant'))): |
michael@0 | 116 | if last_val > 0: |
michael@0 | 117 | delta = val - last_val |
michael@0 | 118 | if result['min'] == -1 or result['min'] > delta: |
michael@0 | 119 | result['min'] = delta |
michael@0 | 120 | if result['max'] == -1 or result['max'] < delta: |
michael@0 | 121 | result['max'] = delta |
michael@0 | 122 | total_sum += delta |
michael@0 | 123 | result['count'] += 1 |
michael@0 | 124 | result['data_points'].append(delta) |
michael@0 | 125 | if entry['ph'] == 'I': |
michael@0 | 126 | result['type'] = 'Instant' |
michael@0 | 127 | last_val = val |
michael@0 | 128 | if result['count'] > 0: result['average'] = total_sum / result['count'] |
michael@0 | 129 | |
michael@0 | 130 | return result |
michael@0 | 131 | |
michael@0 | 132 | |
michael@0 | 133 | def GetAverageRunInfoFromJSONString(json_string, name): |
michael@0 | 134 | """Returns the results from GetAverageRunInfo using a JSON string. |
michael@0 | 135 | |
michael@0 | 136 | Args: |
michael@0 | 137 | json_string: The string containing JSON. |
michael@0 | 138 | name: The 'name' tag to filter on in the JSON file. |
michael@0 | 139 | |
michael@0 | 140 | Returns: |
michael@0 | 141 | See GetAverageRunInfo Returns section. |
michael@0 | 142 | """ |
michael@0 | 143 | return GetAverageRunInfo(json.loads(json_string), name) |
michael@0 | 144 | |
michael@0 | 145 | |
michael@0 | 146 | def GetAverageRunInfoFromFile(json_file, name): |
michael@0 | 147 | """Returns the results from GetAverageRunInfo using a JSON file. |
michael@0 | 148 | |
michael@0 | 149 | Args: |
michael@0 | 150 | json_file: The path to a JSON file. |
michael@0 | 151 | name: The 'name' tag to filter on in the JSON file. |
michael@0 | 152 | |
michael@0 | 153 | Returns: |
michael@0 | 154 | See GetAverageRunInfo Returns section. |
michael@0 | 155 | """ |
michael@0 | 156 | with open(json_file, 'r') as f: |
michael@0 | 157 | data = f.read() |
michael@0 | 158 | perf = json.loads(data) |
michael@0 | 159 | |
michael@0 | 160 | return GetAverageRunInfo(perf, name) |