media/webrtc/trunk/build/android/pylib/perf_tests_helper.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/webrtc/trunk/build/android/pylib/perf_tests_helper.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,120 @@
     1.4 +# Copyright (c) 2012 The Chromium Authors. All rights reserved.
     1.5 +# Use of this source code is governed by a BSD-style license that can be
     1.6 +# found in the LICENSE file.
     1.7 +
     1.8 +import re
     1.9 +
    1.10 +import android_commands
    1.11 +import math
    1.12 +
    1.13 +# Valid values of result type.
    1.14 +RESULT_TYPES = {'unimportant': 'RESULT ',
    1.15 +                'default': '*RESULT ',
    1.16 +                'informational': ''}
    1.17 +
    1.18 +
    1.19 +def _EscapePerfResult(s):
    1.20 +  """Escapes |s| for use in a perf result."""
    1.21 +  # Colons (:) and equal signs (=) are not allowed, and we chose an arbitrary
    1.22 +  # limit of 40 chars.
    1.23 +  return re.sub(':|=', '_', s[:40])
    1.24 +
    1.25 +
    1.26 +def PrintPerfResult(measurement, trace, values, units, result_type='default',
    1.27 +                    print_to_stdout=True):
    1.28 +  """Prints numerical data to stdout in the format required by perf tests.
    1.29 +
    1.30 +  The string args may be empty but they must not contain any colons (:) or
    1.31 +  equals signs (=).
    1.32 +
    1.33 +  Args:
    1.34 +    measurement: A description of the quantity being measured, e.g. "vm_peak".
    1.35 +    trace: A description of the particular data point, e.g. "reference".
    1.36 +    values: A list of numeric measured values.
    1.37 +    units: A description of the units of measure, e.g. "bytes".
    1.38 +    result_type: A  tri-state that accepts values of ['unimportant', 'default',
    1.39 +        'informational']. 'unimportant' prints RESULT, 'default' prints *RESULT
    1.40 +        and 'informational' prints nothing.
    1.41 +    print_to_stdout: If True, prints the output in stdout instead of returning
    1.42 +        the output to caller.
    1.43 +
    1.44 +    Returns:
    1.45 +      String of the formated perf result.
    1.46 +  """
    1.47 +  assert result_type in RESULT_TYPES, 'result type: %s is invalid' % result_type
    1.48 +
    1.49 +  assert isinstance(values, list)
    1.50 +  assert len(values)
    1.51 +  assert '/' not in measurement
    1.52 +  avg = None
    1.53 +  sd = None
    1.54 +  if len(values) > 1:
    1.55 +    try:
    1.56 +      value = '[%s]' % ','.join([str(v) for v in values])
    1.57 +      avg = sum([float(v) for v in values]) / len(values)
    1.58 +      sqdiffs = [(float(v) - avg) ** 2 for v in values]
    1.59 +      variance = sum(sqdiffs) / (len(values) - 1)
    1.60 +      sd = math.sqrt(variance)
    1.61 +    except ValueError:
    1.62 +      value = ", ".join(values)
    1.63 +  else:
    1.64 +    value = values[0]
    1.65 +
    1.66 +  trace_name = _EscapePerfResult(trace)
    1.67 +  output = '%s%s: %s%s%s %s' % (
    1.68 +    RESULT_TYPES[result_type],
    1.69 +    _EscapePerfResult(measurement),
    1.70 +    trace_name,
    1.71 +    # Do not show equal sign if the trace is empty. Usually it happens when
    1.72 +    # measurement is enough clear to describe the result.
    1.73 +    '= ' if trace_name else '',
    1.74 +    value,
    1.75 +    units)
    1.76 +  if avg:
    1.77 +    output += '\nAvg %s: %f%s' % (measurement, avg, units)
    1.78 +  if sd:
    1.79 +    output += '\nSd  %s: %f%s' % (measurement, sd, units)
    1.80 +  if print_to_stdout:
    1.81 +    print output
    1.82 +  return output
    1.83 +
    1.84 +
    1.85 +class PerfTestSetup(object):
    1.86 +  """Provides methods for setting up a device for perf testing."""
    1.87 +  _DROP_CACHES = '/proc/sys/vm/drop_caches'
    1.88 +  _SCALING_GOVERNOR = '/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor'
    1.89 +
    1.90 +  def __init__(self, adb):
    1.91 +    self._adb = adb
    1.92 +    num_cpus = self._adb.GetFileContents('/sys/devices/system/cpu/online',
    1.93 +                                         log_result=False)
    1.94 +    assert num_cpus, 'Unable to find /sys/devices/system/cpu/online'
    1.95 +    self._num_cpus = int(num_cpus[0].split('-')[-1])
    1.96 +    self._original_scaling_governor = None
    1.97 +
    1.98 +  def DropRamCaches(self):
    1.99 +    """Drops the filesystem ram caches for performance testing."""
   1.100 +    if not self._adb.IsRootEnabled():
   1.101 +      self._adb.EnableAdbRoot()
   1.102 +    self._adb.RunShellCommand('sync')
   1.103 +    self._adb.RunShellCommand('echo 3 > ' + PerfTestSetup._DROP_CACHES)
   1.104 +
   1.105 +  def SetUp(self):
   1.106 +    """Sets up performance tests."""
   1.107 +    if not self._original_scaling_governor:
   1.108 +      self._original_scaling_governor = self._adb.GetFileContents(
   1.109 +          PerfTestSetup._SCALING_GOVERNOR % 0,
   1.110 +          log_result=False)[0]
   1.111 +      self._SetScalingGovernorInternal('performance')
   1.112 +    self.DropRamCaches()
   1.113 +
   1.114 +  def TearDown(self):
   1.115 +    """Tears down performance tests."""
   1.116 +    if self._original_scaling_governor:
   1.117 +      self._SetScalingGovernorInternal(self._original_scaling_governor)
   1.118 +    self._original_scaling_governor = None
   1.119 +
   1.120 +  def _SetScalingGovernorInternal(self, value):
   1.121 +    for cpu in range(self._num_cpus):
   1.122 +      self._adb.RunShellCommand(
   1.123 +          ('echo %s > ' + PerfTestSetup._SCALING_GOVERNOR) % (value, cpu))

mercurial