blob: ddffd8040989d6a0cb6f859f986e989062fffd5a [file] [log] [blame]
Simran Basi833814b2013-01-29 13:13:43 -08001# 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
5import logging
6import os
Dennis Jeffreyc42fd302013-04-17 11:57:51 -07007import pprint
Simran Basi833814b2013-01-29 13:13:43 -08008import re
9import StringIO
10
11import common
12from autotest_lib.client.common_lib import error, utils
13from autotest_lib.client.common_lib.cros import dev_server
14
15
Dave Tu6a404e62013-11-05 15:54:48 -080016TELEMETRY_RUN_BENCHMARKS_SCRIPT = 'tools/perf/run_benchmark'
Simran Basi833814b2013-01-29 13:13:43 -080017TELEMETRY_RUN_TESTS_SCRIPT = 'tools/telemetry/run_tests'
Simran Basi1dbfc132013-05-02 10:11:02 -070018TELEMETRY_RUN_CROS_TESTS_SCRIPT = 'chrome/test/telemetry/run_cros_tests'
Simran Basiee9e8602013-03-19 11:52:18 -070019TELEMETRY_TIMEOUT_MINS = 60
Simran Basi833814b2013-01-29 13:13:43 -080020
21# Result Statuses
22SUCCESS_STATUS = 'SUCCESS'
23WARNING_STATUS = 'WARNING'
24FAILED_STATUS = 'FAILED'
25
Dennis Jeffreyc42fd302013-04-17 11:57:51 -070026# Regex for the RESULT output lines understood by chrome buildbot.
27# Keep in sync with chromium/tools/build/scripts/slave/process_log_utils.py.
28RESULTS_REGEX = re.compile(r'(?P<IMPORTANT>\*)?RESULT '
29 '(?P<GRAPH>[^:]*): (?P<TRACE>[^=]*)= '
30 '(?P<VALUE>[\{\[]?[-\d\., ]+[\}\]]?)('
31 ' ?(?P<UNITS>.+))?')
32
33# Constants pertaining to perf keys generated from Telemetry test results.
34PERF_KEY_TELEMETRY_PREFIX = 'TELEMETRY'
35PERF_KEY_DELIMITER = '--'
36
Simran Basi833814b2013-01-29 13:13:43 -080037
38class TelemetryResult(object):
39 """Class to represent the results of a telemetry run.
40
41 This class represents the results of a telemetry run, whether it ran
42 successful, failed or had warnings.
43 """
44
45
46 def __init__(self, exit_code=0, stdout='', stderr=''):
47 """Initializes this TelemetryResultObject instance.
48
49 @param status: Status of the telemtry run.
50 @param stdout: Stdout of the telemetry run.
51 @param stderr: Stderr of the telemetry run.
52 """
53 if exit_code == 0:
54 self.status = SUCCESS_STATUS
55 else:
56 self.status = FAILED_STATUS
57
58 self.perf_keyvals = {}
59 self._stdout = stdout
60 self._stderr = stderr
61 self.output = '\n'.join([stdout, stderr])
62
63
Dennis Jeffreyc42fd302013-04-17 11:57:51 -070064 def _cleanup_perf_string(self, str):
65 """Clean up a perf-related string by removing illegal characters.
Simran Basi833814b2013-01-29 13:13:43 -080066
Dennis Jeffreyc42fd302013-04-17 11:57:51 -070067 Perf keys stored in the chromeOS database may contain only letters,
68 numbers, underscores, periods, and dashes. Transform an inputted
69 string so that any illegal characters are replaced by underscores.
70
71 @param str: The perf string to clean up.
72
73 @return The cleaned-up perf string.
74 """
75 return re.sub(r'[^\w.-]', '_', str)
76
77
78 def _cleanup_units_string(self, units):
79 """Cleanup a units string.
80
81 Given a string representing units for a perf measurement, clean it up
82 by replacing certain illegal characters with meaningful alternatives.
83 Any other illegal characters should then be replaced with underscores.
Simran Basi833814b2013-01-29 13:13:43 -080084
85 Examples:
Dennis Jeffreyc42fd302013-04-17 11:57:51 -070086 count/time -> count_per_time
87 % -> percent
88 units! --> units_
89 score (bigger is better) -> score__bigger_is_better_
90 score (runs/s) -> score__runs_per_s_
Simran Basi833814b2013-01-29 13:13:43 -080091
Dennis Jeffreyc42fd302013-04-17 11:57:51 -070092 @param units: The units string to clean up.
Simran Basi833814b2013-01-29 13:13:43 -080093
Dennis Jeffreyc42fd302013-04-17 11:57:51 -070094 @return The cleaned-up units string.
Simran Basi833814b2013-01-29 13:13:43 -080095 """
Dennis Jeffreyc42fd302013-04-17 11:57:51 -070096 if '%' in units:
97 units = units.replace('%', 'percent')
Simran Basi833814b2013-01-29 13:13:43 -080098 if '/' in units:
99 units = units.replace('/','_per_')
Dennis Jeffreyc42fd302013-04-17 11:57:51 -0700100 return self._cleanup_perf_string(units)
Simran Basi833814b2013-01-29 13:13:43 -0800101
102
103 def parse_benchmark_results(self):
104 """Parse the results of a telemetry benchmark run.
105
Dave Tu6a404e62013-11-05 15:54:48 -0800106 Stdout has the output in RESULT block format below.
Simran Basi833814b2013-01-29 13:13:43 -0800107
Dennis Jeffreyc42fd302013-04-17 11:57:51 -0700108 The lines of interest start with the substring "RESULT". These are
109 specially-formatted perf data lines that are interpreted by chrome
110 builbot (when the Telemetry tests run for chrome desktop) and are
111 parsed to extract perf data that can then be displayed on a perf
112 dashboard. This format is documented in the docstring of class
113 GraphingLogProcessor in this file in the chrome tree:
Simran Basi833814b2013-01-29 13:13:43 -0800114
Dennis Jeffreyc42fd302013-04-17 11:57:51 -0700115 chromium/tools/build/scripts/slave/process_log_utils.py
Simran Basi833814b2013-01-29 13:13:43 -0800116
Dennis Jeffreyc42fd302013-04-17 11:57:51 -0700117 Example RESULT output lines:
118 RESULT average_commit_time_by_url: http___www.ebay.com= 8.86528 ms
119 RESULT CodeLoad: CodeLoad= 6343 score (bigger is better)
120 RESULT ai-astar: ai-astar= [614,527,523,471,530,523,577,625,614,538] ms
121
122 Currently for chromeOS, we can only associate a single perf key (string)
123 with a perf value. That string can only contain letters, numbers,
124 dashes, periods, and underscores, as defined by write_keyval() in:
125
126 chromeos/src/third_party/autotest/files/client/common_lib/
127 base_utils.py
128
129 We therefore parse each RESULT line, clean up the strings to remove any
130 illegal characters not accepted by chromeOS, and construct a perf key
131 string based on the parsed components of the RESULT line (with each
132 component separated by a special delimiter). We prefix the perf key
133 with the substring "TELEMETRY" to identify it as a telemetry-formatted
134 perf key.
Simran Basi833814b2013-01-29 13:13:43 -0800135
136 Stderr has the format of Warnings/Tracebacks. There is always a default
Dennis Jeffreyc42fd302013-04-17 11:57:51 -0700137 warning of the display enviornment setting, followed by warnings of
Simran Basi833814b2013-01-29 13:13:43 -0800138 page timeouts or a traceback.
139
140 If there are any other warnings we flag the test as warning. If there
141 is a traceback we consider this test a failure.
Simran Basi833814b2013-01-29 13:13:43 -0800142 """
Simran Basi833814b2013-01-29 13:13:43 -0800143 if not self._stdout:
144 # Nothing in stdout implies a test failure.
145 logging.error('No stdout, test failed.')
146 self.status = FAILED_STATUS
147 return
148
149 stdout_lines = self._stdout.splitlines()
Simran Basi833814b2013-01-29 13:13:43 -0800150 for line in stdout_lines:
Dennis Jeffreyc42fd302013-04-17 11:57:51 -0700151 results_match = RESULTS_REGEX.search(line)
152 if not results_match:
Simran Basi833814b2013-01-29 13:13:43 -0800153 continue
Dennis Jeffreyc42fd302013-04-17 11:57:51 -0700154
155 match_dict = results_match.groupdict()
156 graph_name = self._cleanup_perf_string(match_dict['GRAPH'].strip())
157 trace_name = self._cleanup_perf_string(match_dict['TRACE'].strip())
158 units = self._cleanup_units_string(
159 (match_dict['UNITS'] or 'units').strip())
160 value = match_dict['VALUE'].strip()
161 unused_important = match_dict['IMPORTANT'] or False # Unused now.
162
163 if value.startswith('['):
164 # A list of values, e.g., "[12,15,8,7,16]". Extract just the
165 # numbers, compute the average and use that. In this example,
166 # we'd get 12+15+8+7+16 / 5 --> 11.6.
167 value_list = [float(x) for x in value.strip('[],').split(',')]
168 value = float(sum(value_list)) / len(value_list)
169 elif value.startswith('{'):
170 # A single value along with a standard deviation, e.g.,
171 # "{34.2,2.15}". Extract just the value itself and use that.
172 # In this example, we'd get 34.2.
173 value_list = [float(x) for x in value.strip('{},').split(',')]
174 value = value_list[0] # Position 0 is the value.
175
176 perf_key = PERF_KEY_DELIMITER.join(
177 [PERF_KEY_TELEMETRY_PREFIX, graph_name, trace_name, units])
178 self.perf_keyvals[perf_key] = str(value)
179
180 pp = pprint.PrettyPrinter(indent=2)
181 logging.debug('Perf Keyvals: %s', pp.pformat(self.perf_keyvals))
Simran Basi833814b2013-01-29 13:13:43 -0800182
183 if self.status is SUCCESS_STATUS:
184 return
185
186 # Otherwise check if simply a Warning occurred or a Failure,
187 # i.e. a Traceback is listed.
188 self.status = WARNING_STATUS
189 for line in self._stderr.splitlines():
190 if line.startswith('Traceback'):
191 self.status = FAILED_STATUS
192
193
194class TelemetryRunner(object):
195 """Class responsible for telemetry for a given build.
196
197 This class will extract and install telemetry on the devserver and is
198 responsible for executing the telemetry benchmarks and returning their
199 output to the caller.
200 """
201
202 def __init__(self, host):
203 """Initializes this telemetry runner instance.
204
205 If telemetry is not installed for this build, it will be.
206 """
207 self._host = host
208 logging.debug('Grabbing build from AFE.')
209
210 build = host.get_build()
211 if not build:
212 logging.error('Unable to locate build label for host: %s.',
213 self._host.hostname)
214 raise error.AutotestError('Failed to grab build for host %s.' %
215 self._host.hostname)
216
217 logging.debug('Setting up telemetry for build: %s', build)
218
219 self._devserver = dev_server.ImageServer.resolve(build)
220 self._telemetry_path = self._devserver.setup_telemetry(build=build)
221 logging.debug('Telemetry Path: %s',self._telemetry_path)
222
223
224 def _run_telemetry(self, script, test_or_benchmark):
225 """Runs telemetry on a dut.
226
227 @param script: Telemetry script we want to run. For example:
228 [path_to_telemetry_src]/src/tools/telemetry/run_tests
229 @param test_or_benchmark: Name of the test or benchmark we want to run,
230 with the page_set (if required) as part of the
231 string.
232
233 @returns A TelemetryResult Instance with the results of this telemetry
234 execution.
235 """
236 devserver_hostname = self._devserver.url().split(
237 'http://')[1].split(':')[0]
Simran Basi1dbfc132013-05-02 10:11:02 -0700238 # TODO (sbasi crbug.com/239933) add support for incognito mode.
Simran Basi833814b2013-01-29 13:13:43 -0800239 telemetry_args = ['ssh',
240 devserver_hostname,
241 'python',
242 script,
243 '--browser=cros-chrome',
244 '--remote=%s' % self._host.hostname,
245 test_or_benchmark]
246
247 logging.debug('Running Telemetry: %s', ' '.join(telemetry_args))
248 output = StringIO.StringIO()
249 error_output = StringIO.StringIO()
250 exit_code = 0
251 try:
252 result = utils.run(' '.join(telemetry_args), stdout_tee=output,
253 stderr_tee=error_output,
254 timeout=TELEMETRY_TIMEOUT_MINS*60)
255 exit_code = result.exit_status
256 except error.CmdError as e:
257 # Telemetry returned a return code of not 0; for benchmarks this
258 # can be due to a timeout on one of the pages of the page set and
259 # we may still have data on the rest. For a test however this
260 # indicates failure.
261 logging.debug('Error occurred executing telemetry.')
262 exit_code = e.result_obj.exit_status
263
264 stdout = output.getvalue()
265 stderr = error_output.getvalue()
266 logging.debug('Telemetry completed with exit code: %d.\nstdout:%s\n'
267 'stderr:%s', exit_code, stdout, stderr)
268
269 return TelemetryResult(exit_code=exit_code, stdout=stdout,
270 stderr=stderr)
271
272
Simran Basi1dbfc132013-05-02 10:11:02 -0700273 def _run_test(self, script, test):
274 """Runs a telemetry test on a dut.
275
276 @param script: Which telemetry test script we want to run. Can be
277 telemetry's base test script or the Chrome OS specific
278 test script.
279 @param test: Telemetry test we want to run.
280
281 @returns A TelemetryResult Instance with the results of this telemetry
282 execution.
283 """
284 logging.debug('Running telemetry test: %s', test)
285 telemetry_script = os.path.join(self._telemetry_path, script)
286 result = self._run_telemetry(telemetry_script, test)
287 if result.status is FAILED_STATUS:
288 raise error.TestFail('Telemetry test: %s failed.',
289 test)
290 return result
291
292
Simran Basi833814b2013-01-29 13:13:43 -0800293 def run_telemetry_test(self, test):
294 """Runs a telemetry test on a dut.
295
296 @param test: Telemetry test we want to run.
297
298 @returns A TelemetryResult Instance with the results of this telemetry
299 execution.
300 """
Simran Basi1dbfc132013-05-02 10:11:02 -0700301 return self._run_test(TELEMETRY_RUN_TESTS_SCRIPT, test)
302
303
304 def run_cros_telemetry_test(self, test):
305 """Runs a cros specific telemetry test on a dut.
306
307 @param test: Telemetry test we want to run.
308
309 @returns A TelemetryResult instance with the results of this telemetry
310 execution.
311 """
312 return self._run_test(TELEMETRY_RUN_CROS_TESTS_SCRIPT, test)
Simran Basi833814b2013-01-29 13:13:43 -0800313
314
Dave Tu6a404e62013-11-05 15:54:48 -0800315 def run_telemetry_benchmark(self, benchmark, keyval_writer=None):
Simran Basi833814b2013-01-29 13:13:43 -0800316 """Runs a telemetry benchmark on a dut.
317
318 @param benchmark: Benchmark we want to run.
Simran Basi833814b2013-01-29 13:13:43 -0800319 @param keyval_writer: Should be a instance with the function
320 write_perf_keyval(), if None, no keyvals will be
321 written. Typically this will be the job object
322 from a autotest test.
323
324 @returns A TelemetryResult Instance with the results of this telemetry
325 execution.
326 """
Dave Tu6a404e62013-11-05 15:54:48 -0800327 logging.debug('Running telemetry benchmark: %s', benchmark)
Simran Basi833814b2013-01-29 13:13:43 -0800328 telemetry_script = os.path.join(self._telemetry_path,
329 TELEMETRY_RUN_BENCHMARKS_SCRIPT)
Dave Tu6a404e62013-11-05 15:54:48 -0800330 result = self._run_telemetry(telemetry_script, benchmark)
Simran Basi833814b2013-01-29 13:13:43 -0800331 result.parse_benchmark_results()
332
333 if keyval_writer:
334 keyval_writer.write_perf_keyval(result.perf_keyvals)
335
336 if result.status is WARNING_STATUS:
Dave Tu6a404e62013-11-05 15:54:48 -0800337 raise error.TestWarn('Telemetry Benchmark: %s'
338 ' exited with Warnings.' % benchmark)
Simran Basi833814b2013-01-29 13:13:43 -0800339 if result.status is FAILED_STATUS:
Dave Tu6a404e62013-11-05 15:54:48 -0800340 raise error.TestFail('Telemetry Benchmark: %s'
341 ' failed to run.' % benchmark)
Simran Basi833814b2013-01-29 13:13:43 -0800342
343 return result