blob: 9095ae2f85bbf29d0b393a4f3ab0d54a5296347a [file] [log] [blame]
#!/usr/bin/env python
# Copyright (c) 2013 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.
"""Run Performance Test Bisect Tool
This script is used by a trybot to run the src/tools/
script with the parameters specified in run-bisect-perf-regression.cfg. It will
check out a copy of the depot in a subdirectory 'bisect' of the working
directory provided, and run the script there.
import imp
import optparse
import os
import platform
import subprocess
import sys
import traceback
from auto_bisect import bisect_utils
from auto_bisect import math_utils
bisect = imp.load_source('bisect-perf-regression',
# Default config file names.
BISECT_REGRESSION_CONFIG = 'run-bisect-perf-regression.cfg'
RUN_TEST_CONFIG = 'run-perf-test.cfg'
WEBKIT_RUN_TEST_CONFIG = os.path.join(
'..', 'third_party', 'WebKit', 'Tools', 'run-perf-test.cfg')
class Goma(object):
def __init__(self, path_to_goma):
self._abs_path_to_goma = None
self._abs_path_to_goma_file = None
if not path_to_goma:
self._abs_path_to_goma = os.path.abspath(path_to_goma)
filename = 'goma_ctl.bat' if == 'nt' else ''
self._abs_path_to_goma_file = os.path.join(self._abs_path_to_goma, filename)
def __enter__(self):
if self._HasGomaPath():
return self
def __exit__(self, *_):
if self._HasGomaPath():
def _HasGomaPath(self):
return bool(self._abs_path_to_goma)
def _SetupEnvVars(self):
if == 'nt':
os.environ['CC'] = (os.path.join(self._abs_path_to_goma, 'gomacc.exe') +
' cl.exe')
os.environ['CXX'] = (os.path.join(self._abs_path_to_goma, 'gomacc.exe') +
' cl.exe')
os.environ['PATH'] = os.pathsep.join([self._abs_path_to_goma,
def _SetupAndStart(self):
"""Sets up goma and launches it.
path_to_goma: Path to goma directory.
True if successful."""
# Sometimes goma is lingering around if something went bad on a previous
# run. Stop it before starting a new process. Can ignore the return code
# since it will return an error if it wasn't running.
if[self._abs_path_to_goma_file, 'start']):
raise RuntimeError('Goma failed to start.')
def _Stop(self):[self._abs_path_to_goma_file, 'stop'])
def _LoadConfigFile(config_file_path):
"""Attempts to load the specified config file as a module
and grab the global config dict.
config_file_path: Path to the config file.
If successful, returns the config dict loaded from the file. If no
such dictionary could be loaded, returns the empty dictionary.
local_vars = {}
execfile(config_file_path, local_vars)
return local_vars['config']
except Exception:
return {}
def _ValidateConfigFile(config_contents, valid_parameters):
"""Validates the config file contents, checking whether all values are
config_contents: A config dictionary.
valid_parameters: A list of parameters to check for.
True if valid.
for parameter in valid_parameters:
if parameter not in config_contents:
return False
value = config_contents[parameter]
if not value or type(value) is not str:
return False
return True
def _ValidatePerfConfigFile(config_contents):
"""Validates the perf config file contents.
This is used when we're doing a perf try job, rather than a bisect.
The config file is called run-perf-test.cfg by default.
The parameters checked are the required parameters; any additional optional
parameters won't be checked and validation will still pass.
config_contents: A config dictionary.
True if valid.
valid_parameters = [
return _ValidateConfigFile(config_contents, valid_parameters)
def _ValidateBisectConfigFile(config_contents):
"""Validates the bisect config file contents.
The parameters checked are the required parameters; any additional optional
parameters won't be checked and validation will still pass.
config_contents: A config dictionary.
True if valid.
valid_params = [
return _ValidateConfigFile(config_contents, valid_params)
def _OutputFailedResults(text_to_print):
bisect_utils.OutputAnnotationStepStart('Results - Failed')
print text_to_print
def _CreateBisectOptionsFromConfig(config):
print config['command']
opts_dict = {}
opts_dict['command'] = config['command']
opts_dict['metric'] = config.get('metric')
if config['repeat_count']:
opts_dict['repeat_test_count'] = int(config['repeat_count'])
if config['truncate_percent']:
opts_dict['truncate_percent'] = int(config['truncate_percent'])
if config['max_time_minutes']:
opts_dict['max_time_minutes'] = int(config['max_time_minutes'])
if config.has_key('use_goma'):
opts_dict['use_goma'] = config['use_goma']
if config.has_key('goma_dir'):
opts_dict['goma_dir'] = config['goma_dir']
opts_dict['build_preference'] = 'ninja'
opts_dict['output_buildbot_annotations'] = True
if '--browser=cros' in config['command']:
opts_dict['target_platform'] = 'cros'
if os.environ[CROS_BOARD_ENV] and os.environ[CROS_IP_ENV]:
opts_dict['cros_board'] = os.environ[CROS_BOARD_ENV]
opts_dict['cros_remote_ip'] = os.environ[CROS_IP_ENV]
raise RuntimeError('Cros build selected, but BISECT_CROS_IP or'
'BISECT_CROS_BOARD undefined.')
elif 'android' in config['command']:
if 'android-chrome-shell' in config['command']:
opts_dict['target_platform'] = 'android'
elif 'android-chrome' in config['command']:
opts_dict['target_platform'] = 'android-chrome'
opts_dict['target_platform'] = 'android'
return bisect.BisectOptions.FromDict(opts_dict)
def _RunPerformanceTest(config, path_to_file):
"""Runs a performance test with and without the current patch.
config: Contents of the config file, a dictionary.
path_to_file: Path to the script.
Attempts to build and run the current revision with and without the
current patch, with the parameters passed in.
# Bisect script expects to be run from the src directory
os.chdir(os.path.join(path_to_file, '..'))
bisect_utils.OutputAnnotationStepStart('Building With Patch')
opts = _CreateBisectOptionsFromConfig(config)
b = bisect.BisectPerformanceMetrics(None, opts)
if bisect_utils.RunGClient(['runhooks']):
raise RuntimeError('Failed to run gclient runhooks')
if not b.BuildCurrentRevision('chromium'):
raise RuntimeError('Patched version failed to build.')
bisect_utils.OutputAnnotationStepStart('Running With Patch')
results_with_patch = b.RunPerformanceTestAndParseResults(
opts.command, opts.metric, reset_on_first_run=True, results_label='Patch')
if results_with_patch[1]:
raise RuntimeError('Patched version failed to run performance test.')
bisect_utils.OutputAnnotationStepStart('Reverting Patch')
# TODO: When this is re-written to recipes, this should use bot_update's
# revert mechanism to fully revert the client. But for now, since we know that
# the perf trybot currently only supports src/ and src/third_party/WebKit, we
# simply reset those two directories.
bisect_utils.CheckRunGit(['reset', '--hard'])
bisect_utils.CheckRunGit(['reset', '--hard'],
os.path.join('third_party', 'WebKit'))
bisect_utils.OutputAnnotationStepStart('Building Without Patch')
if bisect_utils.RunGClient(['runhooks']):
raise RuntimeError('Failed to run gclient runhooks')
if not b.BuildCurrentRevision('chromium'):
raise RuntimeError('Unpatched version failed to build.')
bisect_utils.OutputAnnotationStepStart('Running Without Patch')
results_without_patch = b.RunPerformanceTestAndParseResults(
opts.command, opts.metric, upload_on_last_run=True, results_label='ToT')
if results_without_patch[1]:
raise RuntimeError('Unpatched version failed to run performance test.')
# Find the link to the cloud stored results file.
output = results_without_patch[2]
cloud_file_link = [t for t in output.splitlines()
if '' in t]
if cloud_file_link:
# What we're getting here is basically "View online at http://..." so parse
# out just the url portion.
cloud_file_link = cloud_file_link[0]
cloud_file_link = [t for t in cloud_file_link.split(' ')
if '' in t]
assert cloud_file_link, "Couldn't parse url from output."
cloud_file_link = cloud_file_link[0]
cloud_file_link = ''
# Calculate the % difference in the means of the 2 runs.
percent_diff_in_means = None
std_err = None
if (results_with_patch[0].has_key('mean') and
percent_diff_in_means = (results_with_patch[0]['mean'] /
max(0.0001, results_without_patch[0]['mean'])) * 100.0 - 100.0
std_err = math_utils.PooledStandardError(
[results_with_patch[0]['values'], results_without_patch[0]['values']])
if percent_diff_in_means is not None and std_err is not None:
bisect_utils.OutputAnnotationStepStart('Results - %.02f +- %0.02f delta' %
(percent_diff_in_means, std_err))
print ' %s %s %s' % (''.center(10, ' '), 'Mean'.center(20, ' '),
'Std. Error'.center(20, ' '))
print ' %s %s %s' % ('Patch'.center(10, ' '),
('%.02f' % results_with_patch[0]['mean']).center(20, ' '),
('%.02f' % results_with_patch[0]['std_err']).center(20, ' '))
print ' %s %s %s' % ('No Patch'.center(10, ' '),
('%.02f' % results_without_patch[0]['mean']).center(20, ' '),
('%.02f' % results_without_patch[0]['std_err']).center(20, ' '))
if cloud_file_link:
bisect_utils.OutputAnnotationStepLink('HTML Results', cloud_file_link)
elif cloud_file_link:
bisect_utils.OutputAnnotationStepLink('HTML Results', cloud_file_link)
def _SetupAndRunPerformanceTest(config, path_to_file, path_to_goma):
"""Attempts to build and run the current revision with and without the
current patch, with the parameters passed in.
config: The config read from run-perf-test.cfg.
path_to_file: Path to the script.
path_to_goma: Path to goma directory.
The exit code of 0 on success, otherwise 1.
with Goma(path_to_goma) as _:
config['use_goma'] = bool(path_to_goma)
config['goma_dir'] = os.path.abspath(path_to_goma)
_RunPerformanceTest(config, path_to_file)
return 0
except RuntimeError, e:
_OutputFailedResults('Error: %s' % e.message)
return 1
def _RunBisectionScript(
config, working_directory, path_to_file, path_to_goma, path_to_extra_src,
"""Attempts to execute with the given parameters.
config: A dict containing the parameters to pass to the script.
working_directory: A working directory to provide to the script, where it will store it's own copy of
the depot.
path_to_file: Path to the script.
path_to_goma: Path to goma directory.
path_to_extra_src: Path to extra source file.
dry_run: Do a dry run, skipping sync, build, and performance testing steps.
An exit status code: 0 on success, otherwise 1.
cmd = ['python', os.path.join(path_to_file, ''),
'-c', config['command'],
'-g', config['good_revision'],
'-b', config['bad_revision'],
'-m', config['metric'],
'--working_directory', working_directory,
if config.get('metric'):
cmd.extend(['-m', config['metric']])
if config['repeat_count']:
cmd.extend(['-r', config['repeat_count']])
if config['truncate_percent']:
cmd.extend(['-t', config['truncate_percent']])
if config['max_time_minutes']:
cmd.extend(['--max_time_minutes', config['max_time_minutes']])
if config.has_key('bisect_mode'):
cmd.extend(['--bisect_mode', config['bisect_mode']])
cmd.extend(['--build_preference', 'ninja'])
if '--browser=cros' in config['command']:
cmd.extend(['--target_platform', 'cros'])
if os.environ[CROS_BOARD_ENV] and os.environ[CROS_IP_ENV]:
cmd.extend(['--cros_board', os.environ[CROS_BOARD_ENV]])
cmd.extend(['--cros_remote_ip', os.environ[CROS_IP_ENV]])
print ('Error: Cros build selected, but BISECT_CROS_IP or'
'BISECT_CROS_BOARD undefined.\n')
return 1
if 'android' in config['command']:
if 'android-chrome-shell' in config['command']:
cmd.extend(['--target_platform', 'android'])
elif 'android-chrome' in config['command']:
cmd.extend(['--target_platform', 'android-chrome'])
cmd.extend(['--target_platform', 'android'])
if path_to_goma:
# For Windows XP platforms, goma service is not supported.
# Moreover we don't compile chrome when gs_bucket flag is set instead
# use builds archives, therefore ignore goma service for Windows XP.
# See
if config.get('gs_bucket') and platform.release() == 'XP':
print ('Goma doesn\'t have a win32 binary, therefore it is not supported '
'on Windows XP platform. Please refer to')
path_to_goma = None
if path_to_extra_src:
cmd.extend(['--extra_src', path_to_extra_src])
# These flags are used to download build archives from cloud storage if
# available, otherwise will post a try_job_http request to build it on
# tryserver.
if config.get('gs_bucket'):
if config.get('builder_host') and config.get('builder_port'):
cmd.extend(['--gs_bucket', config['gs_bucket'],
'--builder_host', config['builder_host'],
'--builder_port', config['builder_port']
print ('Error: Specified gs_bucket, but missing builder_host or '
'builder_port information in config.')
return 1
if dry_run:
cmd.extend(['--debug_ignore_build', '--debug_ignore_sync',
cmd = [str(c) for c in cmd]
with Goma(path_to_goma) as _:
return_code =
if return_code:
print ('Error: returned with error %d\n'
% return_code)
return return_code
def _PrintConfigStep(config):
"""Prints out the given config, along with Buildbot annotations."""
for k, v in config.iteritems():
print ' %s : %s' % (k, v)
def _OptionParser():
"""Returns the options parser for"""
usage = ('%prog [options] [-- chromium-options]\n'
'Used by a trybot to run the bisection script using the parameters'
' provided in the run-bisect-perf-regression.cfg file.')
parser = optparse.OptionParser(usage=usage)
parser.add_option('-w', '--working_directory',
help='A working directory to supply to the bisection '
'script, which will use it as the location to checkout '
'a copy of the chromium depot.')
parser.add_option('-p', '--path_to_goma',
help='Path to goma directory. If this is supplied, goma '
'builds will be enabled.')
help='Path to the config file to use. If this is supplied, '
'the bisect script will use this to override the default '
'config file path. The script will attempt to load it '
'as a bisect config first, then a perf config.')
help='Path to extra source file. If this is supplied, '
'bisect script will use this to override default behavior.')
help='The script will perform the full bisect, but '
'without syncing, building, or running the performance '
return parser
def main():
"""Entry point for
Reads the config file, and then tries to either bisect a regression or
just run a performance test, depending on the particular config parameters
specified in the config file.
parser = _OptionParser()
opts, _ = parser.parse_args()
current_dir = os.path.abspath(os.path.dirname(sys.argv[0]))
# Use the default config file path unless one was specified.
config_path = os.path.join(current_dir, BISECT_REGRESSION_CONFIG)
if opts.path_to_config:
config_path = opts.path_to_config
config = _LoadConfigFile(config_path)
# Check if the config is valid for running bisect job.
config_is_valid = _ValidateBisectConfigFile(config)
if config and config_is_valid:
if not opts.working_directory:
print 'Error: missing required parameter: --working_directory\n'
return 1
return _RunBisectionScript(
config, opts.working_directory, current_dir,
opts.path_to_goma, opts.extra_src, opts.dry_run)
# If it wasn't valid for running a bisect, then maybe the user wanted
# to run a perf test instead of a bisect job. Try reading any possible
# perf test config files.
for current_perf_cfg_file in perf_cfg_files:
if opts.path_to_config:
path_to_perf_cfg = opts.path_to_config
path_to_perf_cfg = os.path.join(
config = _LoadConfigFile(path_to_perf_cfg)
config_is_valid = _ValidatePerfConfigFile(config)
if config and config_is_valid:
return _SetupAndRunPerformanceTest(
config, current_dir, opts.path_to_goma)
print ('Error: Could not load config file. Double check your changes to '
'run-bisect-perf-regression.cfg or run-perf-test.cfg for syntax '
return 1
if __name__ == '__main__':