Wed, 31 Dec 2014 13:27:57 +0100
Ignore runtime configuration files generated during quality assurance.
michael@0 | 1 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 2 | # Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | # found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | import re |
michael@0 | 6 | |
michael@0 | 7 | import android_commands |
michael@0 | 8 | import math |
michael@0 | 9 | |
michael@0 | 10 | # Valid values of result type. |
michael@0 | 11 | RESULT_TYPES = {'unimportant': 'RESULT ', |
michael@0 | 12 | 'default': '*RESULT ', |
michael@0 | 13 | 'informational': ''} |
michael@0 | 14 | |
michael@0 | 15 | |
michael@0 | 16 | def _EscapePerfResult(s): |
michael@0 | 17 | """Escapes |s| for use in a perf result.""" |
michael@0 | 18 | # Colons (:) and equal signs (=) are not allowed, and we chose an arbitrary |
michael@0 | 19 | # limit of 40 chars. |
michael@0 | 20 | return re.sub(':|=', '_', s[:40]) |
michael@0 | 21 | |
michael@0 | 22 | |
michael@0 | 23 | def PrintPerfResult(measurement, trace, values, units, result_type='default', |
michael@0 | 24 | print_to_stdout=True): |
michael@0 | 25 | """Prints numerical data to stdout in the format required by perf tests. |
michael@0 | 26 | |
michael@0 | 27 | The string args may be empty but they must not contain any colons (:) or |
michael@0 | 28 | equals signs (=). |
michael@0 | 29 | |
michael@0 | 30 | Args: |
michael@0 | 31 | measurement: A description of the quantity being measured, e.g. "vm_peak". |
michael@0 | 32 | trace: A description of the particular data point, e.g. "reference". |
michael@0 | 33 | values: A list of numeric measured values. |
michael@0 | 34 | units: A description of the units of measure, e.g. "bytes". |
michael@0 | 35 | result_type: A tri-state that accepts values of ['unimportant', 'default', |
michael@0 | 36 | 'informational']. 'unimportant' prints RESULT, 'default' prints *RESULT |
michael@0 | 37 | and 'informational' prints nothing. |
michael@0 | 38 | print_to_stdout: If True, prints the output in stdout instead of returning |
michael@0 | 39 | the output to caller. |
michael@0 | 40 | |
michael@0 | 41 | Returns: |
michael@0 | 42 | String of the formated perf result. |
michael@0 | 43 | """ |
michael@0 | 44 | assert result_type in RESULT_TYPES, 'result type: %s is invalid' % result_type |
michael@0 | 45 | |
michael@0 | 46 | assert isinstance(values, list) |
michael@0 | 47 | assert len(values) |
michael@0 | 48 | assert '/' not in measurement |
michael@0 | 49 | avg = None |
michael@0 | 50 | sd = None |
michael@0 | 51 | if len(values) > 1: |
michael@0 | 52 | try: |
michael@0 | 53 | value = '[%s]' % ','.join([str(v) for v in values]) |
michael@0 | 54 | avg = sum([float(v) for v in values]) / len(values) |
michael@0 | 55 | sqdiffs = [(float(v) - avg) ** 2 for v in values] |
michael@0 | 56 | variance = sum(sqdiffs) / (len(values) - 1) |
michael@0 | 57 | sd = math.sqrt(variance) |
michael@0 | 58 | except ValueError: |
michael@0 | 59 | value = ", ".join(values) |
michael@0 | 60 | else: |
michael@0 | 61 | value = values[0] |
michael@0 | 62 | |
michael@0 | 63 | trace_name = _EscapePerfResult(trace) |
michael@0 | 64 | output = '%s%s: %s%s%s %s' % ( |
michael@0 | 65 | RESULT_TYPES[result_type], |
michael@0 | 66 | _EscapePerfResult(measurement), |
michael@0 | 67 | trace_name, |
michael@0 | 68 | # Do not show equal sign if the trace is empty. Usually it happens when |
michael@0 | 69 | # measurement is enough clear to describe the result. |
michael@0 | 70 | '= ' if trace_name else '', |
michael@0 | 71 | value, |
michael@0 | 72 | units) |
michael@0 | 73 | if avg: |
michael@0 | 74 | output += '\nAvg %s: %f%s' % (measurement, avg, units) |
michael@0 | 75 | if sd: |
michael@0 | 76 | output += '\nSd %s: %f%s' % (measurement, sd, units) |
michael@0 | 77 | if print_to_stdout: |
michael@0 | 78 | print output |
michael@0 | 79 | return output |
michael@0 | 80 | |
michael@0 | 81 | |
michael@0 | 82 | class PerfTestSetup(object): |
michael@0 | 83 | """Provides methods for setting up a device for perf testing.""" |
michael@0 | 84 | _DROP_CACHES = '/proc/sys/vm/drop_caches' |
michael@0 | 85 | _SCALING_GOVERNOR = '/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor' |
michael@0 | 86 | |
michael@0 | 87 | def __init__(self, adb): |
michael@0 | 88 | self._adb = adb |
michael@0 | 89 | num_cpus = self._adb.GetFileContents('/sys/devices/system/cpu/online', |
michael@0 | 90 | log_result=False) |
michael@0 | 91 | assert num_cpus, 'Unable to find /sys/devices/system/cpu/online' |
michael@0 | 92 | self._num_cpus = int(num_cpus[0].split('-')[-1]) |
michael@0 | 93 | self._original_scaling_governor = None |
michael@0 | 94 | |
michael@0 | 95 | def DropRamCaches(self): |
michael@0 | 96 | """Drops the filesystem ram caches for performance testing.""" |
michael@0 | 97 | if not self._adb.IsRootEnabled(): |
michael@0 | 98 | self._adb.EnableAdbRoot() |
michael@0 | 99 | self._adb.RunShellCommand('sync') |
michael@0 | 100 | self._adb.RunShellCommand('echo 3 > ' + PerfTestSetup._DROP_CACHES) |
michael@0 | 101 | |
michael@0 | 102 | def SetUp(self): |
michael@0 | 103 | """Sets up performance tests.""" |
michael@0 | 104 | if not self._original_scaling_governor: |
michael@0 | 105 | self._original_scaling_governor = self._adb.GetFileContents( |
michael@0 | 106 | PerfTestSetup._SCALING_GOVERNOR % 0, |
michael@0 | 107 | log_result=False)[0] |
michael@0 | 108 | self._SetScalingGovernorInternal('performance') |
michael@0 | 109 | self.DropRamCaches() |
michael@0 | 110 | |
michael@0 | 111 | def TearDown(self): |
michael@0 | 112 | """Tears down performance tests.""" |
michael@0 | 113 | if self._original_scaling_governor: |
michael@0 | 114 | self._SetScalingGovernorInternal(self._original_scaling_governor) |
michael@0 | 115 | self._original_scaling_governor = None |
michael@0 | 116 | |
michael@0 | 117 | def _SetScalingGovernorInternal(self, value): |
michael@0 | 118 | for cpu in range(self._num_cpus): |
michael@0 | 119 | self._adb.RunShellCommand( |
michael@0 | 120 | ('echo %s > ' + PerfTestSetup._SCALING_GOVERNOR) % (value, cpu)) |