1# Copyright 2013 The Chromium 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"""Generates test runner factory and tests for performance tests.""" 6 7import json 8import fnmatch 9import logging 10import os 11import shutil 12 13from pylib import android_commands 14from pylib import constants 15from pylib import forwarder 16from pylib.device import device_list 17from pylib.perf import test_runner 18from pylib.utils import test_environment 19 20 21def _GetAllDevices(): 22 devices_path = os.path.join(os.environ.get('CHROMIUM_OUT_DIR', 'out'), 23 device_list.LAST_DEVICES_FILENAME) 24 try: 25 devices = device_list.GetPersistentDeviceList(devices_path) 26 except IOError as e: 27 logging.error('Unable to find %s [%s]', devices_path, e) 28 devices = android_commands.GetAttachedDevices() 29 return sorted(devices) 30 31 32def _GetStepsDictFromSingleStep(test_options): 33 # Running a single command, build the tests structure. 34 steps_dict = { 35 'version': 1, 36 'steps': { 37 'single_step': { 38 'device_affinity': 0, 39 'cmd': test_options.single_step 40 }, 41 } 42 } 43 return steps_dict 44 45 46def _GetStepsDict(test_options): 47 if test_options.single_step: 48 return _GetStepsDictFromSingleStep(test_options) 49 if test_options.steps: 50 with file(test_options.steps, 'r') as f: 51 steps = json.load(f) 52 53 # Already using the new format. 54 assert steps['version'] == 1 55 return steps 56 57 58def Setup(test_options): 59 """Create and return the test runner factory and tests. 60 61 Args: 62 test_options: A PerformanceOptions object. 63 64 Returns: 65 A tuple of (TestRunnerFactory, tests, devices). 66 """ 67 # TODO(bulach): remove this once the bot side lands. BUG=318369 68 constants.SetBuildType('Release') 69 if os.path.exists(constants.PERF_OUTPUT_DIR): 70 shutil.rmtree(constants.PERF_OUTPUT_DIR) 71 os.makedirs(constants.PERF_OUTPUT_DIR) 72 73 # Before running the tests, kill any leftover server. 74 test_environment.CleanupLeftoverProcesses() 75 forwarder.Forwarder.UseMultiprocessing() 76 77 # We want to keep device affinity, so return all devices ever seen. 78 all_devices = _GetAllDevices() 79 80 steps_dict = _GetStepsDict(test_options) 81 sorted_step_names = sorted(steps_dict['steps'].keys()) 82 83 if test_options.test_filter: 84 sorted_step_names = fnmatch.filter(sorted_step_names, 85 test_options.test_filter) 86 87 flaky_steps = [] 88 if test_options.flaky_steps: 89 with file(test_options.flaky_steps, 'r') as f: 90 flaky_steps = json.load(f) 91 92 def TestRunnerFactory(device, shard_index): 93 return test_runner.TestRunner( 94 test_options, device, shard_index, len(all_devices), 95 steps_dict, flaky_steps) 96 97 return (TestRunnerFactory, sorted_step_names, all_devices) 98