Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(464)

Side by Side Diff: build/util/lib/common/perf_tests_helper.py

Issue 23578019: Copy perf_tests_helper.py into build/util/lib (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 # Copyright (c) 2012 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.
4
5 import re
6 import sys
7
8 import json
9 import logging
10 import math
11
12 import perf_result_data_type
13
14
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 '}
21
bulach 2013/09/13 10:31:18 nit: \n
22 def _EscapePerfResult(s):
23 """Escapes |s| for use in a perf result."""
24 return re.sub('[\:|=/#&,]', '_', s)
25
26
27 def _Flatten(values):
28 """Returns a simple list without sub-lists."""
29 ret = []
30 for entry in values:
31 if isinstance(entry, list):
32 ret.extend(_Flatten(entry))
33 else:
34 ret.append(entry)
35 return ret
36
37
38 def GeomMeanAndStdDevFromHistogram(histogram_json):
39 histogram = json.loads(histogram_json)
40 # Handle empty histograms gracefully.
41 if not 'buckets' in histogram:
42 return 0.0, 0.0
43 count = 0
44 sum_of_logs = 0
45 for bucket in histogram['buckets']:
46 if 'high' in bucket:
47 bucket['mean'] = (bucket['low'] + bucket['high']) / 2.0
48 else:
49 bucket['mean'] = bucket['low']
50 if bucket['mean'] > 0:
51 sum_of_logs += math.log(bucket['mean']) * bucket['count']
52 count += bucket['count']
53
54 if count == 0:
55 return 0.0, 0.0
56
57 sum_of_squares = 0
58 geom_mean = math.exp(sum_of_logs / count)
59 for bucket in histogram['buckets']:
60 if bucket['mean'] > 0:
61 sum_of_squares += (bucket['mean'] - geom_mean) ** 2 * bucket['count']
62 return geom_mean, math.sqrt(sum_of_squares / count)
63
64
65 def _MeanAndStdDevFromList(values):
66 avg = None
67 sd = None
68 if len(values) > 1:
69 try:
70 value = '[%s]' % ','.join([str(v) for v in values])
71 avg = sum([float(v) for v in values]) / len(values)
72 sqdiffs = [(float(v) - avg) ** 2 for v in values]
73 variance = sum(sqdiffs) / (len(values) - 1)
74 sd = math.sqrt(variance)
75 except ValueError:
76 value = ", ".join(values)
77 else:
78 value = values[0]
79 return value, avg, sd
80
81
82 def PrintPages(page_list):
83 """Prints list of pages to stdout in the format required by perf tests."""
84 print 'Pages: [%s]' % ','.join([_EscapePerfResult(p) for p in page_list])
85
86
87 def PrintPerfResult(measurement, trace, values, units,
88 result_type=perf_result_data_type.DEFAULT,
89 print_to_stdout=True):
90 """Prints numerical data to stdout in the format required by perf tests.
91
92 The string args may be empty but they must not contain any colons (:) or
93 equals signs (=).
94
95 Args:
96 measurement: A description of the quantity being measured, e.g. "vm_peak".
97 trace: A description of the particular data point, e.g. "reference".
98 values: A list of numeric measured values. An N-dimensional list will be
99 flattened and treated as a simple list.
100 units: A description of the units of measure, e.g. "bytes".
101 result_type: Accepts values of perf_result_data_type.ALL_TYPES.
102 print_to_stdout: If True, prints the output in stdout instead of returning
103 the output to caller.
104
105 Returns:
106 String of the formated perf result.
107 """
108 assert (perf_result_data_type.IsValidType(result_type),
109 'result type: %s is invalid' % result_type)
110
111 trace_name = _EscapePerfResult(trace)
112
113 if (result_type == perf_result_data_type.UNIMPORTANT or
114 result_type == perf_result_data_type.DEFAULT or
115 result_type == perf_result_data_type.INFORMATIONAL):
116 assert isinstance(values, list)
117 assert len(values)
118 assert '/' not in measurement
119 value, avg, sd = _MeanAndStdDevFromList(_Flatten(values))
120 output = '%s%s: %s%s%s %s' % (
121 RESULT_TYPES[result_type],
122 _EscapePerfResult(measurement),
123 trace_name,
124 # Do not show equal sign if the trace is empty. Usually it happens when
125 # measurement is enough clear to describe the result.
126 '= ' if trace_name else '',
127 value,
128 units)
129 else:
130 assert perf_result_data_type.IsHistogram(result_type)
131 assert isinstance(values, list)
132 # The histograms can only be printed individually, there's no computation
133 # across different histograms.
134 assert len(values) == 1
135 value = values[0]
136 output = '%s%s: %s= %s' % (
137 RESULT_TYPES[result_type],
138 _EscapePerfResult(measurement),
139 trace_name,
140 value)
141 avg, sd = GeomMeanAndStdDevFromHistogram(value)
142
143 if avg:
144 output += '\nAvg %s: %f%s' % (measurement, avg, units)
145 if sd:
146 output += '\nSd %s: %f%s' % (measurement, sd, units)
147 if print_to_stdout:
148 print output
149 sys.stdout.flush()
150 return output
bulach 2013/09/13 10:31:18 nit: I think this file would be clearer as perf_te
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698