Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 1 | # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | import logging |
| 6 | import os |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 7 | import StringIO |
| 8 | |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 9 | from autotest_lib.client.common_lib import error, utils |
| 10 | from autotest_lib.client.common_lib.cros import dev_server |
Simran Basi | 5ace6f2 | 2016-01-06 17:30:44 -0800 | [diff] [blame] | 11 | from autotest_lib.server import afe_utils |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 12 | |
| 13 | |
Dave Tu | 6a404e6 | 2013-11-05 15:54:48 -0800 | [diff] [blame] | 14 | TELEMETRY_RUN_BENCHMARKS_SCRIPT = 'tools/perf/run_benchmark' |
Ilja H. Friedel | 086bc3f | 2014-02-27 22:17:55 -0800 | [diff] [blame] | 15 | TELEMETRY_RUN_TESTS_SCRIPT = 'tools/telemetry/run_tests' |
Achuith Bhandarkar | 124e473 | 2014-01-21 15:27:54 -0800 | [diff] [blame] | 16 | TELEMETRY_TIMEOUT_MINS = 120 |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 17 | |
| 18 | # Result Statuses |
| 19 | SUCCESS_STATUS = 'SUCCESS' |
| 20 | WARNING_STATUS = 'WARNING' |
| 21 | FAILED_STATUS = 'FAILED' |
| 22 | |
| 23 | |
| 24 | class TelemetryResult(object): |
| 25 | """Class to represent the results of a telemetry run. |
| 26 | |
| 27 | This class represents the results of a telemetry run, whether it ran |
| 28 | successful, failed or had warnings. |
| 29 | """ |
| 30 | |
| 31 | |
| 32 | def __init__(self, exit_code=0, stdout='', stderr=''): |
| 33 | """Initializes this TelemetryResultObject instance. |
| 34 | |
| 35 | @param status: Status of the telemtry run. |
| 36 | @param stdout: Stdout of the telemetry run. |
| 37 | @param stderr: Stderr of the telemetry run. |
| 38 | """ |
| 39 | if exit_code == 0: |
| 40 | self.status = SUCCESS_STATUS |
| 41 | else: |
| 42 | self.status = FAILED_STATUS |
| 43 | |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 44 | self._stdout = stdout |
| 45 | self._stderr = stderr |
| 46 | self.output = '\n'.join([stdout, stderr]) |
| 47 | |
| 48 | |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 49 | class TelemetryRunner(object): |
| 50 | """Class responsible for telemetry for a given build. |
| 51 | |
| 52 | This class will extract and install telemetry on the devserver and is |
| 53 | responsible for executing the telemetry benchmarks and returning their |
| 54 | output to the caller. |
| 55 | """ |
| 56 | |
Luis Lozano | 23ae319 | 2013-11-08 16:22:46 -0800 | [diff] [blame] | 57 | def __init__(self, host, local=False): |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 58 | """Initializes this telemetry runner instance. |
| 59 | |
| 60 | If telemetry is not installed for this build, it will be. |
Luis Lozano | 23ae319 | 2013-11-08 16:22:46 -0800 | [diff] [blame] | 61 | |
| 62 | @param host: Host where the test will be run. |
| 63 | @param local: If set, no devserver will be used, test will be run |
| 64 | locally. |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 65 | """ |
| 66 | self._host = host |
Ilja H. Friedel | c7bf310 | 2014-05-13 17:31:25 -0700 | [diff] [blame] | 67 | self._devserver = None |
| 68 | self._telemetry_path = None |
Luis Lozano | 23ae319 | 2013-11-08 16:22:46 -0800 | [diff] [blame] | 69 | # TODO (llozano crbug.com/324964). Remove conditional code. |
| 70 | # Use a class hierarchy instead. |
| 71 | if local: |
| 72 | self._setup_local_telemetry() |
| 73 | else: |
| 74 | self._setup_devserver_telemetry() |
| 75 | |
| 76 | logging.debug('Telemetry Path: %s', self._telemetry_path) |
| 77 | |
| 78 | |
| 79 | def _setup_devserver_telemetry(self): |
| 80 | """Setup Telemetry to use the devserver.""" |
| 81 | logging.debug('Setting up telemetry for devserver testing') |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 82 | logging.debug('Grabbing build from AFE.') |
| 83 | |
Simran Basi | 5ace6f2 | 2016-01-06 17:30:44 -0800 | [diff] [blame] | 84 | build = afe_utils.get_build(self._host) |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 85 | if not build: |
| 86 | logging.error('Unable to locate build label for host: %s.', |
| 87 | self._host.hostname) |
| 88 | raise error.AutotestError('Failed to grab build for host %s.' % |
| 89 | self._host.hostname) |
| 90 | |
| 91 | logging.debug('Setting up telemetry for build: %s', build) |
| 92 | |
| 93 | self._devserver = dev_server.ImageServer.resolve(build) |
Simran Basi | cf81f68 | 2014-12-03 16:31:39 -0800 | [diff] [blame] | 94 | self._devserver.stage_artifacts(build, ['autotest_packages']) |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 95 | self._telemetry_path = self._devserver.setup_telemetry(build=build) |
Luis Lozano | 23ae319 | 2013-11-08 16:22:46 -0800 | [diff] [blame] | 96 | |
| 97 | |
| 98 | def _setup_local_telemetry(self): |
| 99 | """Setup Telemetry to use local path to its sources. |
| 100 | |
| 101 | First look for chrome source root, either externally mounted, or inside |
| 102 | the chroot. Prefer chrome-src-internal source tree to chrome-src. |
| 103 | """ |
| 104 | TELEMETRY_DIR = 'src' |
| 105 | CHROME_LOCAL_SRC = '/var/cache/chromeos-cache/distfiles/target/' |
Josh Triplett | 05208c9 | 2014-07-17 13:21:29 -0700 | [diff] [blame] | 106 | CHROME_EXTERNAL_SRC = os.path.expanduser('~/chrome_root/') |
Luis Lozano | 23ae319 | 2013-11-08 16:22:46 -0800 | [diff] [blame] | 107 | |
| 108 | logging.debug('Setting up telemetry for local testing') |
| 109 | |
| 110 | sources_list = ('chrome-src-internal', 'chrome-src') |
Josh Triplett | 05208c9 | 2014-07-17 13:21:29 -0700 | [diff] [blame] | 111 | dir_list = [CHROME_EXTERNAL_SRC] |
Luis Lozano | 23ae319 | 2013-11-08 16:22:46 -0800 | [diff] [blame] | 112 | dir_list.extend( |
| 113 | [os.path.join(CHROME_LOCAL_SRC, x) for x in sources_list]) |
| 114 | if 'CHROME_ROOT' in os.environ: |
| 115 | dir_list.insert(0, os.environ['CHROME_ROOT']) |
| 116 | |
| 117 | telemetry_src = '' |
| 118 | for dir in dir_list: |
| 119 | if os.path.exists(dir): |
| 120 | telemetry_src = os.path.join(dir, TELEMETRY_DIR) |
| 121 | break |
| 122 | else: |
| 123 | raise error.TestError('Telemetry source directory not found.') |
| 124 | |
| 125 | self._devserver = None |
| 126 | self._telemetry_path = telemetry_src |
| 127 | |
| 128 | |
Luis Lozano | 814c718 | 2015-09-08 11:20:47 -0700 | [diff] [blame] | 129 | def _get_telemetry_cmd(self, script, test_or_benchmark, *args): |
Luis Lozano | 23ae319 | 2013-11-08 16:22:46 -0800 | [diff] [blame] | 130 | """Build command to execute telemetry based on script and benchmark. |
| 131 | |
| 132 | @param script: Telemetry script we want to run. For example: |
| 133 | [path_to_telemetry_src]/src/tools/telemetry/run_tests. |
| 134 | @param test_or_benchmark: Name of the test or benchmark we want to run, |
| 135 | with the page_set (if required) as part of |
| 136 | the string. |
Luis Lozano | 814c718 | 2015-09-08 11:20:47 -0700 | [diff] [blame] | 137 | @param args: additional list of arguments to pass to the script. |
| 138 | |
Luis Lozano | 23ae319 | 2013-11-08 16:22:46 -0800 | [diff] [blame] | 139 | @returns Full telemetry command to execute the script. |
| 140 | """ |
| 141 | telemetry_cmd = [] |
| 142 | if self._devserver: |
Keith Haddow | 1e5c701 | 2016-03-09 16:05:37 -0800 | [diff] [blame] | 143 | devserver_hostname = dev_server.DevServer.get_server_name( |
| 144 | self._devserver.url()) |
Luis Lozano | 23ae319 | 2013-11-08 16:22:46 -0800 | [diff] [blame] | 145 | telemetry_cmd.extend(['ssh', devserver_hostname]) |
| 146 | |
| 147 | telemetry_cmd.extend( |
| 148 | ['python', |
| 149 | script, |
Ilja H. Friedel | 6965bd8 | 2014-05-20 18:29:15 -0700 | [diff] [blame] | 150 | '--verbose', |
Luis Lozano | 23ae319 | 2013-11-08 16:22:46 -0800 | [diff] [blame] | 151 | '--browser=cros-chrome', |
Keith Haddow | 1e5c701 | 2016-03-09 16:05:37 -0800 | [diff] [blame] | 152 | '--output-format=chartjson', |
| 153 | '--output-dir=%s' % self._telemetry_path, |
Luis Lozano | 814c718 | 2015-09-08 11:20:47 -0700 | [diff] [blame] | 154 | '--remote=%s' % self._host.hostname]) |
| 155 | telemetry_cmd.extend(args) |
| 156 | telemetry_cmd.append(test_or_benchmark) |
| 157 | |
Keith Haddow | 1e5c701 | 2016-03-09 16:05:37 -0800 | [diff] [blame] | 158 | return ' '.join(telemetry_cmd) |
| 159 | |
| 160 | |
| 161 | def _scp_telemetry_results_cmd(self, perf_results_dir): |
| 162 | """Build command to copy the telemetry results from the devserver. |
| 163 | |
| 164 | @param perf_results_dir: directory path where test output is to be |
| 165 | collected. |
| 166 | @returns SCP command to copy the results json to the specified directory. |
| 167 | """ |
| 168 | scp_cmd = [] |
Ricky Liang | d186f3e | 2016-03-15 16:50:55 +0800 | [diff] [blame] | 169 | devserver_hostname = '' |
| 170 | if perf_results_dir: |
| 171 | if self._devserver: |
| 172 | devserver_hostname = dev_server.DevServer.get_server_name( |
| 173 | self._devserver.url()) + ':' |
Keith Haddow | 1e5c701 | 2016-03-09 16:05:37 -0800 | [diff] [blame] | 174 | scp_cmd.extend(['scp', |
Ricky Liang | d186f3e | 2016-03-15 16:50:55 +0800 | [diff] [blame] | 175 | '%s%s/results-chart.json' % ( |
Keith Haddow | 1e5c701 | 2016-03-09 16:05:37 -0800 | [diff] [blame] | 176 | devserver_hostname, self._telemetry_path), |
| 177 | perf_results_dir]) |
| 178 | |
| 179 | return ' '.join(scp_cmd) |
| 180 | |
| 181 | |
| 182 | def _run_cmd(self, cmd): |
| 183 | """Execute an command in a external shell and capture the output. |
| 184 | |
| 185 | @param cmd: String of is a valid shell command. |
| 186 | |
| 187 | @returns The standard out, standard error and the integer exit code of |
| 188 | the executed command. |
| 189 | """ |
| 190 | logging.debug('Running: %s', cmd) |
| 191 | |
| 192 | output = StringIO.StringIO() |
| 193 | error_output = StringIO.StringIO() |
| 194 | exit_code = 0 |
| 195 | try: |
| 196 | result = utils.run(cmd, stdout_tee=output, |
| 197 | stderr_tee=error_output, |
| 198 | timeout=TELEMETRY_TIMEOUT_MINS*60) |
| 199 | exit_code = result.exit_status |
| 200 | except error.CmdError as e: |
| 201 | logging.debug('Error occurred executing.') |
| 202 | exit_code = e.result_obj.exit_status |
| 203 | |
| 204 | stdout = output.getvalue() |
| 205 | stderr = error_output.getvalue() |
| 206 | logging.debug('Completed with exit code: %d.\nstdout:%s\n' |
| 207 | 'stderr:%s', exit_code, stdout, stderr) |
| 208 | return stdout, stderr, exit_code |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 209 | |
| 210 | |
Luis Lozano | 814c718 | 2015-09-08 11:20:47 -0700 | [diff] [blame] | 211 | def _run_telemetry(self, script, test_or_benchmark, *args): |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 212 | """Runs telemetry on a dut. |
| 213 | |
| 214 | @param script: Telemetry script we want to run. For example: |
Luis Lozano | 23ae319 | 2013-11-08 16:22:46 -0800 | [diff] [blame] | 215 | [path_to_telemetry_src]/src/tools/telemetry/run_tests. |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 216 | @param test_or_benchmark: Name of the test or benchmark we want to run, |
| 217 | with the page_set (if required) as part of the |
| 218 | string. |
Luis Lozano | 814c718 | 2015-09-08 11:20:47 -0700 | [diff] [blame] | 219 | @param args: additional list of arguments to pass to the script. |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 220 | |
| 221 | @returns A TelemetryResult Instance with the results of this telemetry |
| 222 | execution. |
| 223 | """ |
Simran Basi | 1dbfc13 | 2013-05-02 10:11:02 -0700 | [diff] [blame] | 224 | # TODO (sbasi crbug.com/239933) add support for incognito mode. |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 225 | |
Luis Lozano | 814c718 | 2015-09-08 11:20:47 -0700 | [diff] [blame] | 226 | telemetry_cmd = self._get_telemetry_cmd(script, |
| 227 | test_or_benchmark, |
| 228 | *args) |
Keith Haddow | 1e5c701 | 2016-03-09 16:05:37 -0800 | [diff] [blame] | 229 | logging.debug('Running Telemetry: %s', telemetry_cmd) |
Luis Lozano | 23ae319 | 2013-11-08 16:22:46 -0800 | [diff] [blame] | 230 | |
Keith Haddow | 1e5c701 | 2016-03-09 16:05:37 -0800 | [diff] [blame] | 231 | stdout, stderr, exit_code = self._run_cmd(telemetry_cmd) |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 232 | |
| 233 | return TelemetryResult(exit_code=exit_code, stdout=stdout, |
| 234 | stderr=stderr) |
| 235 | |
| 236 | |
Keith Haddow | 1e5c701 | 2016-03-09 16:05:37 -0800 | [diff] [blame] | 237 | def _run_scp(self, perf_results_dir): |
| 238 | """Runs telemetry on a dut. |
| 239 | |
| 240 | @param perf_results_dir: The local directory that results are being |
| 241 | collected. |
| 242 | """ |
| 243 | scp_cmd = self._scp_telemetry_results_cmd(perf_results_dir) |
| 244 | logging.debug('Retrieving Results: %s', scp_cmd) |
| 245 | |
| 246 | self._run_cmd(scp_cmd) |
| 247 | |
| 248 | |
Luis Lozano | 814c718 | 2015-09-08 11:20:47 -0700 | [diff] [blame] | 249 | def _run_test(self, script, test, *args): |
Simran Basi | 1dbfc13 | 2013-05-02 10:11:02 -0700 | [diff] [blame] | 250 | """Runs a telemetry test on a dut. |
| 251 | |
| 252 | @param script: Which telemetry test script we want to run. Can be |
| 253 | telemetry's base test script or the Chrome OS specific |
| 254 | test script. |
| 255 | @param test: Telemetry test we want to run. |
Luis Lozano | 814c718 | 2015-09-08 11:20:47 -0700 | [diff] [blame] | 256 | @param args: additional list of arguments to pass to the script. |
Simran Basi | 1dbfc13 | 2013-05-02 10:11:02 -0700 | [diff] [blame] | 257 | |
| 258 | @returns A TelemetryResult Instance with the results of this telemetry |
| 259 | execution. |
| 260 | """ |
| 261 | logging.debug('Running telemetry test: %s', test) |
| 262 | telemetry_script = os.path.join(self._telemetry_path, script) |
Luis Lozano | 814c718 | 2015-09-08 11:20:47 -0700 | [diff] [blame] | 263 | result = self._run_telemetry(telemetry_script, test, *args) |
Simran Basi | 1dbfc13 | 2013-05-02 10:11:02 -0700 | [diff] [blame] | 264 | if result.status is FAILED_STATUS: |
Ilja H. Friedel | c7bf310 | 2014-05-13 17:31:25 -0700 | [diff] [blame] | 265 | raise error.TestFail('Telemetry test %s failed.' % test) |
Simran Basi | 1dbfc13 | 2013-05-02 10:11:02 -0700 | [diff] [blame] | 266 | return result |
| 267 | |
| 268 | |
Luis Lozano | 814c718 | 2015-09-08 11:20:47 -0700 | [diff] [blame] | 269 | def run_telemetry_test(self, test, *args): |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 270 | """Runs a telemetry test on a dut. |
| 271 | |
| 272 | @param test: Telemetry test we want to run. |
Luis Lozano | 814c718 | 2015-09-08 11:20:47 -0700 | [diff] [blame] | 273 | @param args: additional list of arguments to pass to the telemetry |
| 274 | execution script. |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 275 | |
| 276 | @returns A TelemetryResult Instance with the results of this telemetry |
| 277 | execution. |
| 278 | """ |
Luis Lozano | 814c718 | 2015-09-08 11:20:47 -0700 | [diff] [blame] | 279 | return self._run_test(TELEMETRY_RUN_TESTS_SCRIPT, test, *args) |
Simran Basi | 1dbfc13 | 2013-05-02 10:11:02 -0700 | [diff] [blame] | 280 | |
| 281 | |
Luis Lozano | 814c718 | 2015-09-08 11:20:47 -0700 | [diff] [blame] | 282 | def run_telemetry_benchmark(self, benchmark, perf_value_writer=None, |
| 283 | *args): |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 284 | """Runs a telemetry benchmark on a dut. |
| 285 | |
| 286 | @param benchmark: Benchmark we want to run. |
Fang Deng | e689e71 | 2013-11-13 18:27:06 -0800 | [diff] [blame] | 287 | @param perf_value_writer: Should be an instance with the function |
| 288 | output_perf_value(), if None, no perf value |
| 289 | will be written. Typically this will be the |
| 290 | job object from an autotest test. |
Luis Lozano | 814c718 | 2015-09-08 11:20:47 -0700 | [diff] [blame] | 291 | @param args: additional list of arguments to pass to the telemetry |
| 292 | execution script. |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 293 | |
| 294 | @returns A TelemetryResult Instance with the results of this telemetry |
| 295 | execution. |
| 296 | """ |
Dave Tu | 6a404e6 | 2013-11-05 15:54:48 -0800 | [diff] [blame] | 297 | logging.debug('Running telemetry benchmark: %s', benchmark) |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 298 | telemetry_script = os.path.join(self._telemetry_path, |
| 299 | TELEMETRY_RUN_BENCHMARKS_SCRIPT) |
Luis Lozano | 814c718 | 2015-09-08 11:20:47 -0700 | [diff] [blame] | 300 | result = self._run_telemetry(telemetry_script, benchmark, *args) |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 301 | |
| 302 | if result.status is WARNING_STATUS: |
Dave Tu | 6a404e6 | 2013-11-05 15:54:48 -0800 | [diff] [blame] | 303 | raise error.TestWarn('Telemetry Benchmark: %s' |
| 304 | ' exited with Warnings.' % benchmark) |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 305 | if result.status is FAILED_STATUS: |
Dave Tu | 6a404e6 | 2013-11-05 15:54:48 -0800 | [diff] [blame] | 306 | raise error.TestFail('Telemetry Benchmark: %s' |
| 307 | ' failed to run.' % benchmark) |
Keith Haddow | 1e5c701 | 2016-03-09 16:05:37 -0800 | [diff] [blame] | 308 | if perf_value_writer: |
| 309 | self._run_scp(perf_value_writer.resultsdir) |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 310 | return result |