Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | # Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | """Evaluate ChromeOS autotest. |
| 7 | |
| 8 | Note that by default 'test_that' will install dependency packages of autotest |
| 9 | only once. For example, if you overwrote chrome's unittest binary, your new |
Kuang-che Wu | 927231f | 2018-07-24 14:21:56 +0800 | [diff] [blame] | 10 | binary will be persistent across autotest runs. Add --reinstall if you want |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 11 | clean autotest install. |
| 12 | """ |
| 13 | from __future__ import print_function |
| 14 | import argparse |
| 15 | import json |
| 16 | import logging |
| 17 | import os |
| 18 | import re |
| 19 | import subprocess |
| 20 | import sys |
| 21 | |
| 22 | from bisect_kit import cli |
| 23 | from bisect_kit import common |
| 24 | from bisect_kit import configure |
| 25 | from bisect_kit import cros_util |
| 26 | from bisect_kit import util |
| 27 | |
| 28 | logger = logging.getLogger(__name__) |
| 29 | |
| 30 | OLD = 'old' |
| 31 | NEW = 'new' |
| 32 | SKIP = 'skip' |
| 33 | FATAL = 'fatal' |
| 34 | |
| 35 | EXIT_CODE_MAP = { |
| 36 | OLD: 0, |
| 37 | NEW: 1, |
| 38 | SKIP: 125, |
| 39 | FATAL: 126, |
| 40 | } |
| 41 | |
| 42 | |
| 43 | def create_argument_parser(): |
| 44 | parser = argparse.ArgumentParser(description=__doc__) |
| 45 | common.add_common_arguments(parser) |
| 46 | parser.add_argument( |
| 47 | 'dut', |
| 48 | nargs='?', |
| 49 | type=cli.argtype_notempty, |
| 50 | metavar='DUT', |
| 51 | default=configure.get('DUT', '')) |
| 52 | parser.add_argument( |
| 53 | '--chromeos_root', |
| 54 | type=cli.argtype_dir_path, |
| 55 | metavar='CHROMEOS_ROOT', |
| 56 | default=configure.get('CHROMEOS_ROOT', ''), |
| 57 | help='ChromeOS tree root') |
| 58 | parser.add_argument( |
Kuang-che Wu | d4603d7 | 2018-11-29 17:51:21 +0800 | [diff] [blame] | 59 | '--chrome_root', |
| 60 | metavar='CHROME_ROOT', |
| 61 | type=cli.argtype_dir_path, |
| 62 | default=configure.get('CHROME_ROOT'), |
| 63 | help='Chrome tree root; necessary for telemetry tests') |
| 64 | parser.add_argument( |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 65 | '--prebuilt', |
| 66 | action='store_true', |
| 67 | help='Run autotest using existing prebuilt package if specified; ' |
| 68 | 'otherwise use the default one') |
| 69 | parser.add_argument( |
| 70 | '--reinstall', |
| 71 | action='store_true', |
| 72 | help='Remove existing autotest folder on the DUT first') |
Kuang-che Wu | 85c613c | 2019-01-09 15:46:11 +0800 | [diff] [blame] | 73 | |
| 74 | group = parser.add_argument_group(title='Options for normal autotest tests') |
| 75 | group.add_argument( |
| 76 | '--test_name', help='Test name, like "video_VideoDecodeAccelerator.h264"') |
| 77 | group.add_argument( |
| 78 | '--metric', |
| 79 | help= |
| 80 | 'Metric name of performance test; example: "cheets_SystemRawImageSize"') |
| 81 | group.add_argument( |
| 82 | '--old_value', |
| 83 | type=float, |
| 84 | help='For performance test, old value of given metric') |
| 85 | group.add_argument( |
| 86 | '--new_value', |
| 87 | type=float, |
| 88 | help='For performance test, new value of given metric') |
| 89 | group.add_argument( |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 90 | '--args', |
| 91 | help='Extra args passed to "test_that --args"; Overrides the default') |
| 92 | |
Kuang-che Wu | 85c613c | 2019-01-09 15:46:11 +0800 | [diff] [blame] | 93 | group = parser.add_argument_group(title='Options for CTS/GTS tests') |
| 94 | group.add_argument('--cts_revision', help='CTS revision, like "9.0_r3"') |
| 95 | group.add_argument('--cts_abi', choices=['arm', 'x86']) |
| 96 | group.add_argument( |
| 97 | '--cts_prefix', |
| 98 | help='Prefix of autotest test name, ' |
| 99 | 'like cheets_CTS_N, cheets_CTS_P, cheets_GTS') |
| 100 | group.add_argument( |
| 101 | '--cts_module', help='CTS/GTS module name, like "CtsCameraTestCases"') |
| 102 | group.add_argument( |
| 103 | '--cts_test', |
| 104 | help='CTS/GTS test name, like ' |
| 105 | '"android.hardware.cts.CameraTest#testDisplayOrientation"') |
| 106 | group.add_argument('--cts_timeout', type=float, help='timeout, in seconds') |
| 107 | |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 108 | return parser |
| 109 | |
| 110 | |
| 111 | def parse_test_report_log(result_log, metric): |
| 112 | """Parses autotest result log. |
| 113 | |
| 114 | Args: |
| 115 | result_log: content of test_report.log |
| 116 | metric: what metric to capture if not None |
| 117 | |
| 118 | Returns: |
| 119 | passed, values: |
| 120 | passed: True if test run successfully |
| 121 | values: captured metric values; None if test failed or metric is None |
| 122 | """ |
| 123 | m = re.search(r'Total PASS: (\d+)/(\d+)', result_log) |
Kuang-che Wu | 171dcb6 | 2018-10-25 12:37:05 +0800 | [diff] [blame] | 124 | passed = (m and m.group(1) == m.group(2)) |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 125 | |
| 126 | if not metric: |
Kuang-che Wu | 171dcb6 | 2018-10-25 12:37:05 +0800 | [diff] [blame] | 127 | return passed, None |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 128 | |
| 129 | values = [] |
| 130 | for line in result_log.splitlines(): |
| 131 | m = re.match(r'^(\S+)\s+(\w+)(?:\{\d+\})?\s+(\d+\.\d+)$', line) |
| 132 | if not m: |
| 133 | continue |
| 134 | if m.group(2) == metric: |
| 135 | values.append(float(m.group(3))) |
Kuang-che Wu | 171dcb6 | 2018-10-25 12:37:05 +0800 | [diff] [blame] | 136 | return passed, values |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 137 | |
| 138 | |
| 139 | def parse_test_result_chart(json_path, metric): |
| 140 | data = json.load(open(json_path)) |
Kuang-che Wu | 3331caf | 2018-09-06 19:47:02 +0800 | [diff] [blame] | 141 | |
| 142 | # format 1, telemetry |
| 143 | if 'charts' in data: |
| 144 | summary = data['charts'][metric]['summary'] |
| 145 | |
| 146 | # format 2, autotest without graph |
| 147 | elif metric in data: |
| 148 | summary = data[metric]['summary'] |
| 149 | |
| 150 | # format 3, autotest with graph |
| 151 | elif metric.count('.') == 1: |
| 152 | name, subname = metric.split('.') |
| 153 | summary = data[name][subname] |
| 154 | |
| 155 | else: |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 156 | logger.error('metric "%s" not in %s', metric, json_path) |
Kuang-che Wu | dd80267 | 2018-08-10 19:40:14 +0800 | [diff] [blame] | 157 | return [] |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 158 | |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 159 | if 'values' in summary: |
| 160 | return summary['values'] |
| 161 | return [summary['value']] |
| 162 | |
| 163 | |
| 164 | def get_additional_test_args(test_name): |
| 165 | """Gets extra arguments to specific test. |
| 166 | |
| 167 | Some tests may require special arguments to run. |
| 168 | |
| 169 | Args: |
| 170 | test_name: test name |
| 171 | |
| 172 | Returns: |
| 173 | arguments (str) |
| 174 | """ |
| 175 | if test_name.startswith('telemetry_'): |
| 176 | return 'local=True' |
| 177 | return '' |
| 178 | |
| 179 | |
Kuang-che Wu | 6c5a5b2 | 2019-01-17 18:09:50 +0800 | [diff] [blame] | 180 | def prepare_to_run_test(opts): |
| 181 | # Some versions of ChromeOS SDK is broken and ship bad 'ssh' executable. This |
| 182 | # works around the issue before we fixed the issue. |
| 183 | # TODO(kcwu): fix crbug/899490 |
| 184 | cros_util.cros_sdk(opts.chromeos_root, 'sudo', 'emerge', 'net-misc/openssh') |
| 185 | |
| 186 | if opts.reinstall: |
| 187 | util.check_call('ssh', opts.dut, 'rm', '-rf', '/usr/local/autotest') |
| 188 | |
| 189 | |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 190 | def run_test(opts): |
| 191 | """Runs an autotest test. |
| 192 | |
| 193 | Args: |
| 194 | opts: An argparse.Namespace to hold command line arguments. |
| 195 | |
| 196 | Returns: |
| 197 | path of test result (outside chroot) |
| 198 | """ |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 199 | prebuilt_autotest_dir = os.path.join(cros_util.chromeos_root_inside_chroot, |
| 200 | cros_util.prebuilt_autotest_dir) |
| 201 | # Set results dir inside source tree, so it's easier to access them outside |
| 202 | # chroot. |
| 203 | results_dir = os.path.join(cros_util.chromeos_root_inside_chroot, |
| 204 | 'tmp/autotest_results_tmp') |
| 205 | if opts.prebuilt: |
| 206 | test_that_bin = os.path.join(prebuilt_autotest_dir, |
| 207 | 'site_utils/test_that.py') |
| 208 | else: |
| 209 | test_that_bin = '/usr/bin/test_that' |
| 210 | cmd = [test_that_bin, opts.dut, opts.test_name, '--results_dir', results_dir] |
| 211 | if opts.prebuilt: |
| 212 | cmd += ['--autotest_dir', prebuilt_autotest_dir] |
| 213 | |
| 214 | args = get_additional_test_args(opts.test_name) |
| 215 | if opts.args: |
| 216 | if args: |
Kuang-che Wu | 74768d3 | 2018-09-07 12:03:24 +0800 | [diff] [blame] | 217 | logger.info( |
| 218 | 'default test_that args `%s` is overridden by ' |
| 219 | 'command line option `%s`', args, opts.args) |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 220 | cmd += ['--args', opts.args] |
| 221 | elif args: |
| 222 | cmd += ['--args', args] |
| 223 | |
Kuang-che Wu | 171dcb6 | 2018-10-25 12:37:05 +0800 | [diff] [blame] | 224 | try: |
Kuang-che Wu | d4603d7 | 2018-11-29 17:51:21 +0800 | [diff] [blame] | 225 | output = cros_util.cros_sdk( |
| 226 | opts.chromeos_root, *cmd, chrome_root=opts.chrome_root) |
Kuang-che Wu | 171dcb6 | 2018-10-25 12:37:05 +0800 | [diff] [blame] | 227 | except subprocess.CalledProcessError as e: |
| 228 | output = e.output |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 229 | |
| 230 | m = re.search(r'Finished running tests. Results can be found in (\S+)', |
| 231 | output) |
| 232 | if not m: |
| 233 | logger.error('result dir is unknown') |
| 234 | return None |
| 235 | assert m.group(1) == results_dir |
| 236 | return results_dir.replace(cros_util.chromeos_root_inside_chroot, |
| 237 | opts.chromeos_root) |
| 238 | |
| 239 | |
| 240 | def gather_test_result(opts, result_dir): |
| 241 | result_log_path = os.path.join(result_dir, 'test_report.log') |
| 242 | result_log = open(result_log_path).read() |
| 243 | |
| 244 | passed, values = parse_test_report_log(result_log, opts.metric) |
Kuang-che Wu | 171dcb6 | 2018-10-25 12:37:05 +0800 | [diff] [blame] | 245 | if opts.metric and not values: |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 246 | values = [] |
| 247 | for root, _, files in os.walk(result_dir): |
| 248 | for filename in files: |
| 249 | if filename != 'results-chart.json': |
| 250 | continue |
| 251 | full_path = os.path.join(root, filename) |
| 252 | values += parse_test_result_chart(full_path, opts.metric) |
| 253 | |
| 254 | return passed, values |
| 255 | |
| 256 | |
| 257 | def main(args=None): |
| 258 | common.init() |
| 259 | parser = create_argument_parser() |
| 260 | opts = parser.parse_args(args) |
| 261 | common.config_logging(opts) |
| 262 | |
| 263 | if not cros_util.is_dut(opts.dut): |
| 264 | return FATAL |
| 265 | |
Kuang-che Wu | 85c613c | 2019-01-09 15:46:11 +0800 | [diff] [blame] | 266 | is_cts = ( |
| 267 | opts.cts_revision or opts.cts_abi or opts.cts_prefix or opts.cts_module or |
| 268 | opts.cts_test or opts.cts_timeout) |
| 269 | if is_cts: |
| 270 | if opts.test_name or opts.metric or opts.args: |
| 271 | parser.error( |
| 272 | 'do not specify --test_name, --metric, --args for CTS/GTS tests') |
| 273 | opts.test_name = '%s.tradefed-run-test' % opts.cts_prefix |
| 274 | opts.args = 'module=%s test=%s' % (opts.cts_module, opts.cts_test) |
| 275 | if opts.cts_revision: |
| 276 | opts.args += ' revision=%s' % opts.cts_revision |
| 277 | if opts.cts_abi: |
| 278 | opts.args += ' abi=%s' % opts.cts_abi |
| 279 | if opts.cts_timeout: |
| 280 | opts.args += ' timeout=%s' % opts.cts_timeout |
| 281 | else: |
| 282 | if not opts.test_name: |
| 283 | parser.error('argument --test_name is required') |
| 284 | |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 285 | # Verify command line options. |
| 286 | if opts.metric: |
| 287 | if opts.old_value is None: |
| 288 | logger.error('--old_value is not provided') |
| 289 | return FATAL |
| 290 | if opts.new_value is None: |
| 291 | logger.error('--new_value is not provided') |
| 292 | return FATAL |
| 293 | else: |
| 294 | if opts.old_value is not None: |
| 295 | logger.error('--old_value is provided but --metric is not') |
| 296 | return FATAL |
| 297 | if opts.new_value is not None: |
| 298 | logger.error('--new_value is provided but --metric is not') |
| 299 | return FATAL |
Kuang-che Wu | d4603d7 | 2018-11-29 17:51:21 +0800 | [diff] [blame] | 300 | if opts.test_name.startswith('telemetry_'): |
| 301 | if not opts.chrome_root: |
| 302 | logger.error('--chrome_root is mandatory for telemetry tests') |
| 303 | return FATAL |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 304 | |
Kuang-che Wu | 6c5a5b2 | 2019-01-17 18:09:50 +0800 | [diff] [blame] | 305 | try: |
| 306 | prepare_to_run_test(opts) |
| 307 | except Exception: |
| 308 | logger.exception('failed when prepare, assume it is temporary; SKIP') |
| 309 | return SKIP |
Kuang-che Wu | e47162d | 2018-10-29 17:24:04 +0800 | [diff] [blame] | 310 | |
Kuang-che Wu | 171dcb6 | 2018-10-25 12:37:05 +0800 | [diff] [blame] | 311 | result_dir = run_test(opts) |
| 312 | if not result_dir: |
Kuang-che Wu | dd80267 | 2018-08-10 19:40:14 +0800 | [diff] [blame] | 313 | return FATAL |
| 314 | |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 315 | passed, values = gather_test_result(opts, result_dir) |
| 316 | |
| 317 | if opts.metric: |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 318 | if not values: |
| 319 | logger.warning('no values found; SKIP') |
| 320 | return SKIP |
| 321 | |
| 322 | print('BISECT_RESULT_VALUES=', ' '.join(map(str, values))) |
| 323 | average = float(sum(values)) / len(values) |
Kuang-che Wu | 689f154 | 2018-08-20 17:45:58 +0800 | [diff] [blame] | 324 | if abs(average - opts.old_value) < abs(average - opts.new_value): |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 325 | logger.info('values=%s, average=%s; OLD', values, average) |
| 326 | return OLD |
| 327 | logger.info('values=%s, average=%s; NEW', values, average) |
| 328 | return NEW |
| 329 | else: |
| 330 | if passed: |
| 331 | logger.info('passed') |
| 332 | return OLD |
| 333 | logger.info('failed') |
| 334 | return NEW |
| 335 | |
| 336 | |
| 337 | if __name__ == '__main__': |
| 338 | sys.exit(EXIT_CODE_MAP[main()]) |