1 # Copyright 2013 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.
6 """A helper module for parsing JSON objects from perf tests results."""
11 def GetAverageRunInfo(json_data
, name
):
12 """Summarizes TraceEvent JSON data for performance metrics.
14 Example JSON Inputs (More tags can be added but these are required):
30 Measuring Call Frequency (FPS):
35 "name": "TestTraceFPS"
40 "name": "TestTraceFPS"
46 json_data: A list of dictonaries each representing a JSON object.
47 name: The 'name' tag to filter on in the JSON file.
50 A dictionary of result data with the following tags:
51 min: The minimum value tracked.
52 max: The maximum value tracked.
53 average: The average of all the values tracked.
54 count: The number of times the category/name pair was tracked.
55 type: The type of tracking ('Instant' for instant tags and 'Span' for
57 category: The passed in category filter.
58 name: The passed in name filter.
59 data_points: A list of all of the times used to generate this data.
60 units: The units for the values being reported.
63 Exception: if entry contains invalid data.
66 def EntryFilter(entry
):
67 return entry
['cat'] == 'Java' and entry
['name'] == name
68 filtered_entries
= [j
for j
in json_data
if EntryFilter(j
)]
76 result
['type'] = 'Unknown'
77 result
['category'] = 'Java'
79 result
['data_points'] = []
86 for entry
in filtered_entries
:
94 result
['units'] = 'kb'
99 return float(entry
['ts']) / 1000.0
101 result
['units'] = 'ms'
103 raise Exception('Entry did not contain valid value info: %s' % entry
)
105 if not val_type
in entry
:
106 raise Exception('Entry did not contain expected value type "%s" '
107 'information: %s' % (val_type
, entry
))
109 if (entry
['ph'] == 'S' and
110 (result
['type'] == 'Unknown' or result
['type'] == 'Span')):
111 result
['type'] = 'Span'
113 elif ((entry
['ph'] == 'F' and result
['type'] == 'Span') or
114 (entry
['ph'] == 'I' and (result
['type'] == 'Unknown' or
115 result
['type'] == 'Instant'))):
117 delta
= val
- last_val
118 if result
['min'] == -1 or result
['min'] > delta
:
119 result
['min'] = delta
120 if result
['max'] == -1 or result
['max'] < delta
:
121 result
['max'] = delta
124 result
['data_points'].append(delta
)
125 if entry
['ph'] == 'I':
126 result
['type'] = 'Instant'
128 if result
['count'] > 0:
129 result
['average'] = total_sum
/ result
['count']
134 def GetAverageRunInfoFromJSONString(json_string
, name
):
135 """Returns the results from GetAverageRunInfo using a JSON string.
138 json_string: The string containing JSON.
139 name: The 'name' tag to filter on in the JSON file.
142 See GetAverageRunInfo Returns section.
144 return GetAverageRunInfo(json
.loads(json_string
), name
)
147 def GetAverageRunInfoFromFile(json_file
, name
):
148 """Returns the results from GetAverageRunInfo using a JSON file.
151 json_file: The path to a JSON file.
152 name: The 'name' tag to filter on in the JSON file.
155 See GetAverageRunInfo Returns section.
157 with
open(json_file
, 'r') as f
:
159 perf
= json
.loads(data
)
161 return GetAverageRunInfo(perf
, name
)