Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import re | 5 import re |
| 6 | 6 |
| 7 import android_commands | 7 import android_commands |
| 8 import json | |
| 8 import math | 9 import math |
| 9 | 10 |
| 10 # Valid values of result type. | 11 # Valid values of result type. |
| 11 RESULT_TYPES = {'unimportant': 'RESULT ', | 12 RESULT_TYPES = {'unimportant': 'RESULT ', |
| 12 'default': '*RESULT ', | 13 'default': '*RESULT ', |
| 13 'informational': ''} | 14 'informational': '', |
| 15 'unimportant-histogram': 'HISTOGRAM ', | |
| 16 'histogram': '*HISTOGRAM '} | |
| 14 | 17 |
| 15 | 18 |
| 16 def _EscapePerfResult(s): | 19 def _EscapePerfResult(s): |
| 17 """Escapes |s| for use in a perf result.""" | 20 """Escapes |s| for use in a perf result.""" |
| 18 # Colons (:) and equal signs (=) are not allowed, and we chose an arbitrary | 21 # Colons (:) and equal signs (=) are not allowed, and we chose an arbitrary |
| 19 # limit of 40 chars. | 22 # limit of 40 chars. |
| 20 return re.sub(':|=', '_', s[:40]) | 23 return re.sub(':|=', '_', s[:40]) |
| 21 | 24 |
| 22 | 25 |
| 26 def GeomMeanAndStdDevFromHistogram(histogram_json): | |
| 27 histogram = json.loads(histogram_json) | |
| 28 count = 0 | |
| 29 sum_of_logs = 0 | |
| 30 for bucket in histogram['buckets']: | |
| 31 if 'high' in bucket: | |
| 32 bucket['mean'] = (bucket['low'] + bucket['high']) / 2.0 | |
| 33 else: | |
| 34 bucket['mean'] = bucket['low'] | |
| 35 if bucket['mean'] > 0: | |
| 36 sum_of_logs += math.log(bucket['mean']) * bucket['count'] | |
| 37 count += bucket['count'] | |
| 38 | |
| 39 if count == 0: | |
| 40 return 0.0, 0.0 | |
| 41 | |
| 42 sum_of_squares = 0 | |
| 43 geom_mean = math.exp(sum_of_logs / count) | |
| 44 for bucket in histogram['buckets']: | |
| 45 if bucket['mean'] > 0: | |
| 46 sum_of_squares += (bucket['mean'] - geom_mean) ** 2 * bucket['count'] | |
|
bulach
2012/10/30 18:46:28
not sure if this would be any clearer, feel free t
marja
2012/10/31 09:15:59
Discussed this offline: This code is copied -> dec
| |
| 47 return geom_mean, math.sqrt(sum_of_squares / count) | |
| 48 | |
| 49 | |
| 23 def PrintPerfResult(measurement, trace, values, units, result_type='default', | 50 def PrintPerfResult(measurement, trace, values, units, result_type='default', |
| 24 print_to_stdout=True): | 51 print_to_stdout=True): |
| 25 """Prints numerical data to stdout in the format required by perf tests. | 52 """Prints numerical data to stdout in the format required by perf tests. |
| 26 | 53 |
| 27 The string args may be empty but they must not contain any colons (:) or | 54 The string args may be empty but they must not contain any colons (:) or |
| 28 equals signs (=). | 55 equals signs (=). |
| 29 | 56 |
| 30 Args: | 57 Args: |
| 31 measurement: A description of the quantity being measured, e.g. "vm_peak". | 58 measurement: A description of the quantity being measured, e.g. "vm_peak". |
| 32 trace: A description of the particular data point, e.g. "reference". | 59 trace: A description of the particular data point, e.g. "reference". |
| 33 values: A list of numeric measured values. | 60 values: A list of numeric measured values. |
| 34 units: A description of the units of measure, e.g. "bytes". | 61 units: A description of the units of measure, e.g. "bytes". |
| 35 result_type: A tri-state that accepts values of ['unimportant', 'default', | 62 result_type: A tri-state that accepts values of ['unimportant', 'default', |
| 36 'informational']. 'unimportant' prints RESULT, 'default' prints *RESULT | 63 'informational']. 'unimportant' prints RESULT, 'default' prints *RESULT |
| 37 and 'informational' prints nothing. | 64 and 'informational' prints nothing. |
| 38 print_to_stdout: If True, prints the output in stdout instead of returning | 65 print_to_stdout: If True, prints the output in stdout instead of returning |
| 39 the output to caller. | 66 the output to caller. |
| 40 | 67 |
| 41 Returns: | 68 Returns: |
| 42 String of the formated perf result. | 69 String of the formated perf result. |
| 43 """ | 70 """ |
| 44 assert result_type in RESULT_TYPES, 'result type: %s is invalid' % result_type | 71 assert result_type in RESULT_TYPES, 'result type: %s is invalid' % result_type |
| 45 | 72 |
| 46 assert isinstance(values, list) | 73 if result_type in ['unimportant', 'default', 'informational']: |
| 47 assert len(values) | 74 assert isinstance(values, list) |
| 48 assert '/' not in measurement | 75 assert len(values) |
| 49 avg = None | 76 assert '/' not in measurement |
| 50 sd = None | 77 avg = None |
| 51 if len(values) > 1: | 78 sd = None |
| 52 try: | 79 if len(values) > 1: |
| 53 value = '[%s]' % ','.join([str(v) for v in values]) | 80 try: |
| 54 avg = sum([float(v) for v in values]) / len(values) | 81 value = '[%s]' % ','.join([str(v) for v in values]) |
| 55 sqdiffs = [(float(v) - avg) ** 2 for v in values] | 82 avg = sum([float(v) for v in values]) / len(values) |
| 56 variance = sum(sqdiffs) / (len(values) - 1) | 83 sqdiffs = [(float(v) - avg) ** 2 for v in values] |
| 57 sd = math.sqrt(variance) | 84 variance = sum(sqdiffs) / (len(values) - 1) |
| 58 except ValueError: | 85 sd = math.sqrt(variance) |
| 59 value = ", ".join(values) | 86 except ValueError: |
| 87 value = ", ".join(values) | |
| 88 else: | |
| 89 value = values[0] | |
|
bulach
2012/10/30 18:46:28
maybe split 74-89 into GeomMeandAndStdDevFromList
marja
2012/10/31 09:15:59
Done.
| |
| 60 else: | 90 else: |
| 61 value = values[0] | 91 value = values[0] |
| 92 # We can't print the units, otherwise parsing the histogram json output | |
| 93 # can't be parsed easily. | |
| 94 units = '' | |
| 95 avg, sd = GeomMeanAndStdDevFromHistogram(value) | |
| 62 | 96 |
| 63 trace_name = _EscapePerfResult(trace) | 97 trace_name = _EscapePerfResult(trace) |
| 64 output = '%s%s: %s%s%s %s' % ( | 98 output = '%s%s: %s%s%s %s' % ( |
| 65 RESULT_TYPES[result_type], | 99 RESULT_TYPES[result_type], |
| 66 _EscapePerfResult(measurement), | 100 _EscapePerfResult(measurement), |
| 67 trace_name, | 101 trace_name, |
| 68 # Do not show equal sign if the trace is empty. Usually it happens when | 102 # Do not show equal sign if the trace is empty. Usually it happens when |
| 69 # measurement is enough clear to describe the result. | 103 # measurement is enough clear to describe the result. |
| 70 '= ' if trace_name else '', | 104 '= ' if trace_name else '', |
| 71 value, | 105 value, |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 111 def TearDown(self): | 145 def TearDown(self): |
| 112 """Tears down performance tests.""" | 146 """Tears down performance tests.""" |
| 113 if self._original_scaling_governor: | 147 if self._original_scaling_governor: |
| 114 self._SetScalingGovernorInternal(self._original_scaling_governor) | 148 self._SetScalingGovernorInternal(self._original_scaling_governor) |
| 115 self._original_scaling_governor = None | 149 self._original_scaling_governor = None |
| 116 | 150 |
| 117 def _SetScalingGovernorInternal(self, value): | 151 def _SetScalingGovernorInternal(self, value): |
| 118 for cpu in range(self._num_cpus): | 152 for cpu in range(self._num_cpus): |
| 119 self._adb.RunShellCommand( | 153 self._adb.RunShellCommand( |
| 120 ('echo %s > ' + PerfTestSetup._SCALING_GOVERNOR) % (value, cpu)) | 154 ('echo %s > ' + PerfTestSetup._SCALING_GOVERNOR) % (value, cpu)) |
| OLD | NEW |