| 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 math |
| 8 | 9 |
| 9 # Valid values of result type. | 10 # Valid values of result type. |
| 10 RESULT_TYPES = {'unimportant': 'RESULT ', | 11 RESULT_TYPES = {'unimportant': 'RESULT ', |
| 11 'default': '*RESULT ', | 12 'default': '*RESULT ', |
| 12 'informational': ''} | 13 'informational': ''} |
| 13 | 14 |
| 14 | 15 |
| 15 def _EscapePerfResult(s): | 16 def _EscapePerfResult(s): |
| 16 """Escapes |s| for use in a perf result.""" | 17 """Escapes |s| for use in a perf result.""" |
| 17 # Colons (:) and equal signs (=) are not allowed, and we chose an arbitrary | 18 # Colons (:) and equal signs (=) are not allowed, and we chose an arbitrary |
| (...skipping 21 matching lines...) Expand all Loading... |
| 39 | 40 |
| 40 Returns: | 41 Returns: |
| 41 String of the formated perf result. | 42 String of the formated perf result. |
| 42 """ | 43 """ |
| 43 assert result_type in RESULT_TYPES, 'result type: %s is invalid' % result_type | 44 assert result_type in RESULT_TYPES, 'result type: %s is invalid' % result_type |
| 44 | 45 |
| 45 assert isinstance(values, list) | 46 assert isinstance(values, list) |
| 46 assert len(values) | 47 assert len(values) |
| 47 assert '/' not in measurement | 48 assert '/' not in measurement |
| 48 avg = None | 49 avg = None |
| 50 sd = None |
| 49 if len(values) > 1: | 51 if len(values) > 1: |
| 50 try: | 52 try: |
| 51 value = '[%s]' % ','.join([str(v) for v in values]) | 53 value = '[%s]' % ','.join([str(v) for v in values]) |
| 52 avg = sum([float(v) for v in values]) / len(values) | 54 avg = sum([float(v) for v in values]) / len(values) |
| 55 sqdiffs = [(float(v) - avg) ** 2 for v in values] |
| 56 variance = sum(sqdiffs) / (len(values) - 1) |
| 57 sd = math.sqrt(variance) |
| 53 except ValueError: | 58 except ValueError: |
| 54 value = ", ".join(values) | 59 value = ", ".join(values) |
| 55 else: | 60 else: |
| 56 value = values[0] | 61 value = values[0] |
| 57 | 62 |
| 58 trace_name = _EscapePerfResult(trace) | 63 trace_name = _EscapePerfResult(trace) |
| 59 output = '%s%s: %s%s%s %s' % ( | 64 output = '%s%s: %s%s%s %s' % ( |
| 60 RESULT_TYPES[result_type], | 65 RESULT_TYPES[result_type], |
| 61 _EscapePerfResult(measurement), | 66 _EscapePerfResult(measurement), |
| 62 trace_name, | 67 trace_name, |
| 63 # Do not show equal sign if the trace is empty. Usually it happens when | 68 # Do not show equal sign if the trace is empty. Usually it happens when |
| 64 # measurement is enough clear to describe the result. | 69 # measurement is enough clear to describe the result. |
| 65 '= ' if trace_name else '', | 70 '= ' if trace_name else '', |
| 66 value, | 71 value, |
| 67 units) | 72 units) |
| 68 if avg: | 73 if avg: |
| 69 output += '\nAvg %s: %f%s' % (measurement, avg, units) | 74 output += '\nAvg %s: %f%s' % (measurement, avg, units) |
| 75 if sd: |
| 76 output += '\nSd %s: %f%s' % (measurement, sd, units) |
| 70 if print_to_stdout: | 77 if print_to_stdout: |
| 71 print output | 78 print output |
| 72 return output | 79 return output |
| 73 | 80 |
| 74 | 81 |
| 75 class PerfTestSetup(object): | 82 class PerfTestSetup(object): |
| 76 """Provides methods for setting up a device for perf testing.""" | 83 """Provides methods for setting up a device for perf testing.""" |
| 77 _DROP_CACHES = '/proc/sys/vm/drop_caches' | 84 _DROP_CACHES = '/proc/sys/vm/drop_caches' |
| 78 _SCALING_GOVERNOR = '/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor' | 85 _SCALING_GOVERNOR = '/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor' |
| 79 | 86 |
| 80 def __init__(self, adb): | 87 def __init__(self, adb): |
| 81 self._adb = adb | 88 self._adb = adb |
| 82 num_cpus = self._adb.GetFileContents('/sys/devices/system/cpu/online', | 89 num_cpus = self._adb.GetFileContents('/sys/devices/system/cpu/online', |
| 83 log_result=False) | 90 log_result=False) |
| 84 assert num_cpus, 'Unable to find /sys/devices/system/cpu/online' | 91 assert num_cpus, 'Unable to find /sys/devices/system/cpu/online' |
| 85 self._num_cpus = int(num_cpus[0].split('-')[-1]) | 92 self._num_cpus = int(num_cpus[0].split('-')[-1]) |
| 86 self._original_scaling_governor = None | 93 self._original_scaling_governor = None |
| 87 | 94 |
| 88 def DropRamCaches(self): | 95 def DropRamCaches(self): |
| 89 """Drops the filesystem ram caches for performance testing.""" | 96 """Drops the filesystem ram caches for performance testing.""" |
| 97 if not self._adb.IsRootEnabled(): |
| 98 self._adb.EnableAdbRoot() |
| 99 self._adb.RunShellCommand('sync') |
| 90 self._adb.RunShellCommand('echo 3 > ' + PerfTestSetup._DROP_CACHES) | 100 self._adb.RunShellCommand('echo 3 > ' + PerfTestSetup._DROP_CACHES) |
| 91 | 101 |
| 92 def SetUp(self): | 102 def SetUp(self): |
| 93 """Sets up performance tests.""" | 103 """Sets up performance tests.""" |
| 94 if not self._original_scaling_governor: | 104 if not self._original_scaling_governor: |
| 95 self._original_scaling_governor = self._adb.GetFileContents( | 105 self._original_scaling_governor = self._adb.GetFileContents( |
| 96 PerfTestSetup._SCALING_GOVERNOR % 0, | 106 PerfTestSetup._SCALING_GOVERNOR % 0, |
| 97 log_result=False)[0] | 107 log_result=False)[0] |
| 98 self._SetScalingGovernorInternal('performance') | 108 self._SetScalingGovernorInternal('performance') |
| 99 self.DropRamCaches() | 109 self.DropRamCaches() |
| 100 | 110 |
| 101 def TearDown(self): | 111 def TearDown(self): |
| 102 """Tears down performance tests.""" | 112 """Tears down performance tests.""" |
| 103 if self._original_scaling_governor: | 113 if self._original_scaling_governor: |
| 104 self._SetScalingGovernorInternal(self._original_scaling_governor) | 114 self._SetScalingGovernorInternal(self._original_scaling_governor) |
| 105 self._original_scaling_governor = None | 115 self._original_scaling_governor = None |
| 106 | 116 |
| 107 def _SetScalingGovernorInternal(self, value): | 117 def _SetScalingGovernorInternal(self, value): |
| 108 for cpu in range(self._num_cpus): | 118 for cpu in range(self._num_cpus): |
| 109 self._adb.RunShellCommand( | 119 self._adb.RunShellCommand( |
| 110 ('echo %s > ' + PerfTestSetup._SCALING_GOVERNOR) % (value, cpu)) | 120 ('echo %s > ' + PerfTestSetup._SCALING_GOVERNOR) % (value, cpu)) |
| OLD | NEW |