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 import sys | 6 import sys |
7 | 7 |
8 import android_commands | 8 import android_commands |
9 import json | 9 import json |
10 import math | 10 import math |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 if avg: | 121 if avg: |
122 output += '\nAvg %s: %f%s' % (measurement, avg, units) | 122 output += '\nAvg %s: %f%s' % (measurement, avg, units) |
123 if sd: | 123 if sd: |
124 output += '\nSd %s: %f%s' % (measurement, sd, units) | 124 output += '\nSd %s: %f%s' % (measurement, sd, units) |
125 if print_to_stdout: | 125 if print_to_stdout: |
126 print output | 126 print output |
127 sys.stdout.flush() | 127 sys.stdout.flush() |
128 return output | 128 return output |
129 | 129 |
130 | 130 |
131 class PerfTestSetup(object): | 131 class CacheControl(object): |
132 """Provides methods for setting up a device for perf testing.""" | |
133 _DROP_CACHES = '/proc/sys/vm/drop_caches' | 132 _DROP_CACHES = '/proc/sys/vm/drop_caches' |
| 133 |
| 134 def __init__(self, adb): |
| 135 self._adb = adb |
| 136 |
| 137 def DropRamCaches(self): |
| 138 """Drops the filesystem ram caches for performance testing.""" |
| 139 self._adb.RunShellCommand('su -c sync') |
| 140 self._adb.SetProtectedFileContents(CacheControl._DROP_CACHES, '3') |
| 141 |
| 142 |
| 143 class PerfControl(object): |
| 144 """Provides methods for setting the performance mode of a device.""" |
134 _SCALING_GOVERNOR_FMT = ( | 145 _SCALING_GOVERNOR_FMT = ( |
135 '/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor') | 146 '/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor') |
136 | 147 |
137 def __init__(self, adb): | 148 def __init__(self, adb): |
138 self._adb = adb | 149 self._adb = adb |
139 kernel_max = self._adb.GetFileContents('/sys/devices/system/cpu/kernel_max', | 150 kernel_max = self._adb.GetFileContents('/sys/devices/system/cpu/kernel_max', |
140 log_result=False) | 151 log_result=False) |
141 assert kernel_max, 'Unable to find /sys/devices/system/cpu/kernel_max' | 152 assert kernel_max, 'Unable to find /sys/devices/system/cpu/kernel_max' |
142 self._kernel_max = int(kernel_max[0]) | 153 self._kernel_max = int(kernel_max[0]) |
143 self._original_scaling_governor = None | 154 self._original_scaling_governor = self._adb.GetFileContents( |
| 155 PerfControl._SCALING_GOVERNOR_FMT % 0, |
| 156 log_result=False)[0] |
| 157 |
| 158 def SetHighPerfMode(self): |
| 159 """Sets the highest possible performance mode for the device.""" |
| 160 self._SetScalingGovernorInternal('performance') |
| 161 |
| 162 def SetDefaultPerfMode(self): |
| 163 """Sets the performance mode for the device to its default mode.""" |
| 164 product_model = self._adb.GetProductModel() |
| 165 governor_mode = { |
| 166 "GT-I9300" : 'pegasusq', |
| 167 "Galaxy Nexus" : 'interactive', |
| 168 "Nexus 4" : 'ondemand', |
| 169 "Nexus 7" : 'interactive', |
| 170 "Nexus 10": 'interactive' |
| 171 }.get(product_model, 'ondemand') |
| 172 self._SetScalingGovernorInternal(governor_mode) |
| 173 |
| 174 def RestoreOriginalPerfMode(self): |
| 175 """Resets the original performance mode of the device.""" |
| 176 self._SetScalingGovernorInternal(self._original_scaling_governor) |
| 177 |
| 178 def _SetScalingGovernorInternal(self, value): |
| 179 for cpu in range(self._kernel_max + 1): |
| 180 scaling_governor_file = PerfControl._SCALING_GOVERNOR_FMT % cpu |
| 181 if self._adb.FileExistsOnDevice(scaling_governor_file): |
| 182 self._adb.SetProtectedFileContents(scaling_governor_file, value) |
| 183 |
| 184 |
| 185 class PerfTestSetup(PerfControl): |
| 186 """Provides methods for setting up a device for perf testing. |
| 187 |
| 188 TODO(aberent): remove once all tests have been moved to the new classes |
| 189 """ |
144 | 190 |
145 def DropRamCaches(self): | 191 def DropRamCaches(self): |
146 """Drops the filesystem ram caches for performance testing.""" | 192 CacheControl(self._adb).DropRamCaches() |
147 self._adb.RunShellCommand('su -c sync') | |
148 self._adb.SetProtectedFileContents(PerfTestSetup._DROP_CACHES, '3') | |
149 | 193 |
150 def SetUp(self): | 194 def SetUp(self): |
151 """Sets up performance tests.""" | 195 self.SetHighPerfMode() |
152 if not self._original_scaling_governor: | |
153 self._original_scaling_governor = self._adb.GetFileContents( | |
154 PerfTestSetup._SCALING_GOVERNOR_FMT % 0, | |
155 log_result=False)[0] | |
156 self._SetScalingGovernorInternal('performance') | |
157 self.DropRamCaches() | 196 self.DropRamCaches() |
158 | 197 |
159 def TearDown(self): | 198 def TearDown(self): |
160 """Tears down performance tests.""" | 199 self.ResetOriginalPerfMode() |
161 if self._original_scaling_governor: | |
162 self._SetScalingGovernorInternal(self._original_scaling_governor) | |
163 self._original_scaling_governor = None | |
164 | |
165 def _SetScalingGovernorInternal(self, value): | |
166 for cpu in range(self._kernel_max + 1): | |
167 scaling_governor_file = PerfTestSetup._SCALING_GOVERNOR_FMT % cpu | |
168 if self._adb.FileExistsOnDevice(scaling_governor_file): | |
169 self._adb.SetProtectedFileContents(scaling_governor_file, value) | |
OLD | NEW |