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.
12 import perf_result_data_type
15 # Mapping from result type to test output
16 RESULT_TYPES = {perf_result_data_type.UNIMPORTANT: 'RESULT ',
17 perf_result_data_type.DEFAULT: '*RESULT ',
18 perf_result_data_type.INFORMATIONAL: '',
19 perf_result_data_type.UNIMPORTANT_HISTOGRAM: 'HISTOGRAM ',
20 perf_result_data_type.HISTOGRAM: '*HISTOGRAM '}
23 def _EscapePerfResult(s):
24 """Escapes |s| for use in a perf result."""
25 return re.sub('[\:|=/#&,]', '_', s)
28 def FlattenList(values):
29 """Returns a simple list without sub-lists."""
32 if isinstance(entry, list):
33 ret.extend(FlattenList(entry))
39 def GeomMeanAndStdDevFromHistogram(histogram_json):
40 histogram = json.loads(histogram_json)
41 # Handle empty histograms gracefully.
42 if not 'buckets' in histogram:
46 for bucket in histogram['buckets']:
48 bucket['mean'] = (bucket['low'] + bucket['high']) / 2.0
50 bucket['mean'] = bucket['low']
51 if bucket['mean'] > 0:
52 sum_of_logs += math.log(bucket['mean']) * bucket['count']
53 count += bucket['count']
59 geom_mean = math.exp(sum_of_logs / count)
60 for bucket in histogram['buckets']:
61 if bucket['mean'] > 0:
62 sum_of_squares += (bucket['mean'] - geom_mean) ** 2 * bucket['count']
63 return geom_mean, math.sqrt(sum_of_squares / count)
66 def _MeanAndStdDevFromList(values):
71 value = '[%s]' % ','.join([str(v) for v in values])
72 avg = sum([float(v) for v in values]) / len(values)
73 sqdiffs = [(float(v) - avg) ** 2 for v in values]
74 variance = sum(sqdiffs) / (len(values) - 1)
75 sd = math.sqrt(variance)
77 value = ", ".join(values)
83 def PrintPages(page_list):
84 """Prints list of pages to stdout in the format required by perf tests."""
85 print 'Pages: [%s]' % ','.join([_EscapePerfResult(p) for p in page_list])
88 def PrintPerfResult(measurement, trace, values, units,
89 result_type=perf_result_data_type.DEFAULT,
90 print_to_stdout=True):
91 """Prints numerical data to stdout in the format required by perf tests.
93 The string args may be empty but they must not contain any colons (:) or
95 This is parsed by the buildbot using:
96 http://src.chromium.org/viewvc/chrome/trunk/tools/build/scripts/slave/process_log_utils.py
99 measurement: A description of the quantity being measured, e.g. "vm_peak".
100 On the dashboard, this maps to a particular graph. Mandatory.
101 trace: A description of the particular data point, e.g. "reference".
102 On the dashboard, this maps to a particular "line" in the graph.
104 values: A list of numeric measured values. An N-dimensional list will be
105 flattened and treated as a simple list.
106 units: A description of the units of measure, e.g. "bytes".
107 result_type: Accepts values of perf_result_data_type.ALL_TYPES.
108 print_to_stdout: If True, prints the output in stdout instead of returning
109 the output to caller.
112 String of the formated perf result.
114 assert perf_result_data_type.IsValidType(result_type), \
115 'result type: %s is invalid' % result_type
117 trace_name = _EscapePerfResult(trace)
119 if (result_type == perf_result_data_type.UNIMPORTANT or
120 result_type == perf_result_data_type.DEFAULT or
121 result_type == perf_result_data_type.INFORMATIONAL):
122 assert isinstance(values, list)
123 assert '/' not in measurement
124 flattened_values = FlattenList(values)
125 assert len(flattened_values)
126 value, avg, sd = _MeanAndStdDevFromList(flattened_values)
127 output = '%s%s: %s%s%s %s' % (
128 RESULT_TYPES[result_type],
129 _EscapePerfResult(measurement),
131 # Do not show equal sign if the trace is empty. Usually it happens when
132 # measurement is enough clear to describe the result.
133 '= ' if trace_name else '',
137 assert perf_result_data_type.IsHistogram(result_type)
138 assert isinstance(values, list)
139 # The histograms can only be printed individually, there's no computation
140 # across different histograms.
141 assert len(values) == 1
143 output = '%s%s: %s= %s %s' % (
144 RESULT_TYPES[result_type],
145 _EscapePerfResult(measurement),
149 avg, sd = GeomMeanAndStdDevFromHistogram(value)
152 output += '\nAvg %s: %f%s' % (measurement, avg, units)
154 output += '\nSd %s: %f%s' % (measurement, sd, units)