blob: 4628d63d96aceb8eb9e483d52b6ffde071686e77 [file] [log] [blame]
#!/usr/bin/env python
# Copyright 2017 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Runs telemetry benchmarks and gtest perf tests.
This script attempts to emulate the contract of gtest-style tests
invoked via recipes. The main contract is that the caller passes the
json is written to that file in the format detailed here:
Optional argument:
is a double-colon-separated ("::") list of test names, to run just that subset
of tests. This list is forwarded to the run_telemetry_benchmark_as_googletest
This script is intended to be the base command invoked by the isolate,
followed by a subsequent Python script. It could be generalized to
invoke an arbitrary executable.
It currently runs several benchmarks. The benchmarks it will execute are
based on the shard it is running on and the sharding_map_path.
If this is executed with a gtest perf test, the flag --non-telemetry
has to be passed in to the script so the script knows it is running
an executable and not the run_benchmark command.
The results of running the benchmark are put in separate directories per
benchmark. Two files will be present in each directory; perf_results.json, which
is the perf specific results (with unenforced format, could be histogram or
graph json), and test_results.json, which is a JSON test results
format file
To test changes to this script, please run
cd tools/perf
./run_tests ScriptsSmokeTest.testRunPerformanceTests
import argparse
import json
import os
import shutil
import sys
import time
import tempfile
import traceback
import common
CHROMIUM_SRC_DIR = os.path.abspath(
os.path.join(os.path.dirname(__file__), '..', '..'))
PERF_DIR = os.path.join(CHROMIUM_SRC_DIR, 'tools', 'perf')
# Add src/tools/perf where lives
import generate_legacy_perf_dashboard_json
# Add src/testing/ into sys.path for importing xvfb and test_env.
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import xvfb
import test_env
# Unfortunately we need to copy these variables from ../
# Importing it and using its get_sandbox_env breaks test runs on Linux
# (it seems to unset DISPLAY).
CHROME_SANDBOX_PATH = '/opt/chromium/chrome_sandbox'
SHARD_MAPS_DIRECTORY = os.path.join(
os.path.dirname(__file__), '..', '..', 'tools', 'perf', 'core',
class OutputFilePaths(object):
"""Provide paths to where results outputs should be written.
The merge script later will pull all of these
together, so that's why they aren't in the standard locations. Also,
note that because of the OBBS (One Build Bot Step), Telemetry
has multiple tests running on a single shard, so we need to prefix
these locations with a directory named by the benchmark name.
def __init__(self, isolated_out_dir, perf_test_name):
self.benchmark_path = os.path.join(isolated_out_dir, perf_test_name)
def SetUp(self):
return self
def perf_results(self):
return os.path.join(self.benchmark_path, 'perf_results.json')
def test_results(self):
return os.path.join(self.benchmark_path, 'test_results.json')
def logs(self):
return os.path.join(self.benchmark_path, 'benchmark_log.txt')
def print_duration(step, start):
print 'Duration of %s: %d seconds' % (step, time.time() - start)
def IsWindows():
return sys.platform == 'cygwin' or sys.platform.startswith('win')
class GtestCommandGenerator(object):
def __init__(self, options):
self._options = options
def generate(self):
"""Generate the command to run to start the gtest perf test.
list of strings, the executable and its arguments.
return ([self._get_executable()] +
self._generate_filter_args() +
self._generate_repeat_args() +
self._generate_also_run_disabled_tests_args() +
self._generate_output_args() +
def _get_executable(self):
executable = self._options.executable
if IsWindows():
return r'.\%s.exe' % executable
return './%s' % executable
def _get_passthrough_args(self):
return self._options.passthrough_args
def _generate_filter_args(self):
if self._options.isolated_script_test_filter:
filter_list = common.extract_filter_list(
return ['--gtest_filter=' + ':'.join(filter_list)]
return []
def _generate_repeat_args(self):
# TODO( Support --isolated-script-test-repeat.
return []
def _generate_also_run_disabled_tests_args(self):
# TODO( Support
# --isolated-script-test-also-run-disabled-tests.
return []
def _generate_output_args(self):
output_args = []
# These flags are to make sure that test output perf metrics in the log.
if not '--verbose' in self._options.passthrough_args:
if (not '--test-launcher-print-test-stdio=always'
in self._options.passthrough_args):
return output_args
def write_legacy_test_results(return_code, output_filepath):
# TODO( Fix to output
valid = (return_code == 0)
failures = [] if valid else ['(entire test suite)']
output_json = {
'valid': valid,
'failures': failures,
with open(output_filepath, 'w') as fh:
json.dump(output_json, fh)
def execute_gtest_perf_test(command_generator, output_paths, use_xvfb=False):
env = os.environ.copy()
# Assume we want to set up the sandbox environment variables all the
# time; doing so is harmless on non-Linux platforms and is needed
# all the time on Linux.
env['CHROME_HEADLESS'] = '1'
return_code = 0
command = command_generator.generate()
if use_xvfb:
return_code = xvfb.run_executable(
command, env, stdoutfile=output_paths.logs)
return_code = test_env.run_command_with_output(
command, env=env, stdoutfile=output_paths.logs)
# Get the correct json format from the stdout to write to the perf
# results file.
results_processor = generate_legacy_perf_dashboard_json.\
graph_json_string = results_processor.GenerateJsonResults(
with open(output_paths.perf_results, 'w') as fh:
except Exception:
return_code = 1
write_legacy_test_results(return_code, output_paths.test_results)
return return_code
class TelemetryCommandGenerator(object):
def __init__(self, benchmark, options,
stories=None, is_reference=False):
self.benchmark = benchmark
self._options = options
self._stories = stories
self._is_reference = is_reference
def generate(self, output_dir):
"""Generate the command to run to start the benchmark.
output_dir: The directory to configure the command to put output files
list of strings, the executable and its arguments.
return ([sys.executable, self._options.executable] +
[self.benchmark] +
self._generate_filter_args() +
self._generate_repeat_args() +
self._generate_also_run_disabled_tests_args() +
self._generate_output_args(output_dir) +
self._generate_story_range_args() +
# passthrough args must be before reference args:
self._get_passthrough_args() +
def _get_passthrough_args(self):
return self._options.passthrough_args
def _generate_filter_args(self):
if self._options.isolated_script_test_filter:
filter_list = common.extract_filter_list(
# Need to convert this to a valid regex.
filter_regex = '(' + '|'.join(filter_list) + ')'
return ['--story-filter=' + filter_regex]
return []
def _generate_repeat_args(self):
if self._options.isolated_script_test_repeat:
return ['--pageset-repeat=' + str(
return []
def _generate_also_run_disabled_tests_args(self):
if self._options.isolated_script_test_also_run_disabled_tests:
return ['--also-run-disabled-tests']
return []
def _generate_output_args(self, output_dir):
return ['--output-format=json-test-results',
'--output-dir=' + output_dir]
def _generate_story_range_args(self):
"""Returns arguments that limit the stories to be run inside the benchmark.
range_arguments = []
if self._stories:
if 'begin' in self._stories.keys():
range_arguments.append('--story-shard-begin-index=%d' % (
if 'end' in self._stories.keys():
range_arguments.append('--story-shard-end-index=%d' % (
return range_arguments
def _generate_reference_build_args(self):
if self._is_reference:
return ['--browser=reference',
return []
def execute_telemetry_benchmark(
command_generator, output_paths, use_xvfb=False):
start = time.time()
env = os.environ.copy()
env['CHROME_HEADLESS'] = '1'
# Assume we want to set up the sandbox environment variables all the
# time; doing so is harmless on non-Linux platforms and is needed
# all the time on Linux.
return_code = 1
temp_dir = tempfile.mkdtemp('telemetry')
command = command_generator.generate(temp_dir)
if use_xvfb:
return_code = xvfb.run_executable(
command, env=env, stdoutfile=output_paths.logs)
return_code = test_env.run_command_with_output(
command, env=env, stdoutfile=output_paths.logs)
expected_perf_filename = os.path.join(temp_dir, 'histograms.json')
shutil.move(expected_perf_filename, output_paths.perf_results)
expected_results_filename = os.path.join(temp_dir, 'test-results.json')
shutil.move(expected_results_filename, output_paths.test_results)
except Exception:
print ('The following exception may have prevented the code from '
'outputing structured test results and perf results output:')
print traceback.format_exc()
# Add ignore_errors=True because otherwise rmtree may fail due to leaky
# processes of tests are still holding opened handles to files under
# |tempfile_dir|. For example, see
shutil.rmtree(temp_dir, ignore_errors=True)
print_duration('executing benchmark %s' % command_generator.benchmark, start)
if return_code:
return return_code
return 0
def parse_arguments(args):
parser = argparse.ArgumentParser()
parser.add_argument('executable', help='The name of the executable to run.')
'--isolated-script-test-output', required=True)
# The following two flags may be passed in sometimes by Pinpoint
# or by the recipe, but they don't do anything.
'--isolated-script-test-chartjson-output', required=False)
'--isolated-script-test-perf-output', required=False)
'--isolated-script-test-filter', type=str, required=False)
# Note that the following three arguments are only supported by Telemetry
# tests right now. See
'--isolated-script-test-repeat', type=int, required=False)
'--isolated-script-test-launcher-retry-limit', type=int, required=False,
choices=[0]) # Telemetry does not support retries.
default=False, action='store_true', required=False)
parser.add_argument('--xvfb', help='Start xvfb.', action='store_true')
help='Type of perf test', type=bool, default=False)
help='Name of the gtest benchmark', type=str,
help='Comma separated list of benchmark names'
' to run in lieu of indexing into our benchmark bot maps',
# Some executions may have a different sharding scheme and/or set of tests.
# These files must live in src/tools/perf/core/shard_maps
parser.add_argument('--test-shard-map-filename', type=str, required=False)
help='Run test on reference browser', action='store_true')
help='Arguments to pass directly through to the test '
'executable.', action='append',
options, leftover_args = parser.parse_known_args(args)
return options
def main():
args = sys.argv[1:] # Skip program name.
options = parse_arguments(args)
isolated_out_dir = os.path.dirname(options.isolated_script_test_output)
overall_return_code = 0
if options.non_telemetry:
command_generator = GtestCommandGenerator(options)
benchmark_name = options.gtest_benchmark_name
# Fallback to use the name of the executable if flag isn't set.
# TODO( remove fallback logic and raise parser error if
# --non-telemetry is set but --gtest-benchmark-name is not set once pinpoint
# is converted to always pass --gtest-benchmark-name flag.
if not benchmark_name:
benchmark_name = options.executable
output_paths = OutputFilePaths(isolated_out_dir, benchmark_name).SetUp()
overall_return_code = execute_gtest_perf_test(
command_generator, output_paths, options.xvfb)
# If the user has supplied a list of benchmark names, execute those instead
# of using the shard map.
if options.benchmarks:
benchmarks = options.benchmarks.split(',')
for benchmark in benchmarks:
output_paths = OutputFilePaths(isolated_out_dir, benchmark).SetUp()
command_generator = TelemetryCommandGenerator(
benchmark, options)
return_code = execute_telemetry_benchmark(
command_generator, output_paths, options.xvfb)
overall_return_code = return_code or overall_return_code
if options.run_ref_build:
print ('Not running reference build. --run-ref-build argument is only '
'supported for sharded benchmarks. It is simple to support '
'this for unsharded --benchmarks if needed.')
elif options.test_shard_map_filename:
# First determine what shard we are running on to know how to
# index into the bot map to get list of telemetry benchmarks to run.
total_shards = None
shard_index = None
shard_map_path = os.path.join(SHARD_MAPS_DIRECTORY,
env = os.environ.copy()
total_shards = env['GTEST_TOTAL_SHARDS']
if 'GTEST_SHARD_INDEX' in env:
shard_index = env['GTEST_SHARD_INDEX']
if not total_shards or not shard_index:
raise Exception(
'Sharded Telemetry perf tests must either specify --benchmarks '
'list or have shard indicator environment variables present.')
# Copy sharding map file to isolated_out_dir so that the merge script
# can collect it later.
# TODO(crouleau): Move this step over to merge script
# (
os.path.join(isolated_out_dir, 'benchmarks_shard_map.json'))
with open(shard_map_path) as f:
shard_map = json.load(f)
benchmarks_and_stories = shard_map[shard_index]['benchmarks']
for benchmark, stories in benchmarks_and_stories.iteritems():
# Need to run the benchmark on both latest browser and reference build.
output_paths = OutputFilePaths(isolated_out_dir, benchmark).SetUp()
command_generator = TelemetryCommandGenerator(
benchmark, options, stories=stories)
return_code = execute_telemetry_benchmark(
command_generator, output_paths, options.xvfb)
overall_return_code = return_code or overall_return_code
if options.run_ref_build:
reference_benchmark_foldername = benchmark + '.reference'
reference_output_paths = OutputFilePaths(
isolated_out_dir, reference_benchmark_foldername).SetUp()
reference_command_generator = TelemetryCommandGenerator(
benchmark, options,
stories=stories, is_reference=True)
# We intentionally ignore the return code of the reference build.
reference_command_generator, reference_output_paths,
raise Exception('Telemetry tests must provide either a shard map or a '
'--benchmarks list so that we know which stories to run.')
return overall_return_code
# This is not really a "script test" so does not need to manually add
# any additional compile targets.
def main_compile_targets(args):
json.dump([], args.output)
if __name__ == '__main__':
# Conform minimally to the protocol defined by ScriptTest.
if 'compile_targets' in sys.argv:
funcs = {
'run': None,
'compile_targets': main_compile_targets,
sys.exit(common.run_script(sys.argv[1:], funcs))