Path: blob/main_old/scripts/perf_test_runner.py
1693 views
#!/usr/bin/python21#2# Copyright 2015 The ANGLE Project Authors. All rights reserved.3# Use of this source code is governed by a BSD-style license that can be4# found in the LICENSE file.5#6# perf_test_runner.py:7# Helper script for running and analyzing perftest results. Runs the8# tests in an infinite batch, printing out the mean and coefficient of9# variation of the population continuously.10#1112import argparse13import glob14import logging15import os16import re17import subprocess18import sys1920base_path = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..'))2122# Look for a [Rr]elease build.23TEST_SUITE_SEARCH_PATH = glob.glob('out/*elease*')24DEFAULT_METRIC = 'wall_time'25DEFAULT_EXPERIMENTS = 102627DEFAULT_TEST_SUITE = 'angle_perftests'2829if sys.platform == 'win32':30DEFAULT_TEST_NAME = 'DrawCallPerfBenchmark.Run/d3d11_null'31else:32DEFAULT_TEST_NAME = 'DrawCallPerfBenchmark.Run/gl'3334EXIT_SUCCESS = 035EXIT_FAILURE = 13637scores = []383940# Danke to http://stackoverflow.com/a/2775832641def mean(data):42"""Return the sample arithmetic mean of data."""43n = len(data)44if n < 1:45raise ValueError('mean requires at least one data point')46return float(sum(data)) / float(n) # in Python 2 use sum(data)/float(n)474849def sum_of_square_deviations(data, c):50"""Return sum of square deviations of sequence data."""51ss = sum((float(x) - c)**2 for x in data)52return ss535455def coefficient_of_variation(data):56"""Calculates the population coefficient of variation."""57n = len(data)58if n < 2:59raise ValueError('variance requires at least two data points')60c = mean(data)61ss = sum_of_square_deviations(data, c)62pvar = ss / n # the population variance63stddev = (pvar**0.5) # population standard deviation64return stddev / c656667def truncated_list(data, n):68"""Compute a truncated list, n is truncation size"""69if len(data) < n * 2:70raise ValueError('list not large enough to truncate')71return sorted(data)[n:-n]727374def truncated_mean(data, n):75"""Compute a truncated mean, n is truncation size"""76return mean(truncated_list(data, n))777879def truncated_cov(data, n):80"""Compute a truncated coefficient of variation, n is truncation size"""81return coefficient_of_variation(truncated_list(data, n))828384def main(raw_args):85parser = argparse.ArgumentParser()86parser.add_argument(87'--suite',88help='Test suite binary. Default is "%s".' % DEFAULT_TEST_SUITE,89default=DEFAULT_TEST_SUITE)90parser.add_argument(91'-m',92'--metric',93help='Test metric. Default is "%s".' % DEFAULT_METRIC,94default=DEFAULT_METRIC)95parser.add_argument(96'--experiments',97help='Number of experiments to run. Default is %d.' % DEFAULT_EXPERIMENTS,98default=DEFAULT_EXPERIMENTS,99type=int)100parser.add_argument('-v', '--verbose', help='Extra verbose logging.', action='store_true')101parser.add_argument('test_name', help='Test to run', default=DEFAULT_TEST_NAME)102args, extra_args = parser.parse_known_args(raw_args)103104if args.verbose:105logging.basicConfig(level='DEBUG')106107if sys.platform == 'win32':108args.suite += '.exe'109110# Find most recent binary111newest_binary = None112newest_mtime = None113114for path in TEST_SUITE_SEARCH_PATH:115binary_path = os.path.join(base_path, path, args.suite)116if os.path.exists(binary_path):117binary_mtime = os.path.getmtime(binary_path)118if (newest_binary is None) or (binary_mtime > newest_mtime):119newest_binary = binary_path120newest_mtime = binary_mtime121122perftests_path = newest_binary123124if perftests_path == None or not os.path.exists(perftests_path):125print('Cannot find Release %s!' % args.test_suite)126return EXIT_FAILURE127128print('Using test executable: %s' % perftests_path)129print('Test name: %s' % args.test_name)130131def get_results(metric, extra_args=[]):132run = [perftests_path, '--gtest_filter=%s' % args.test_name] + extra_args133logging.info('running %s' % str(run))134process = subprocess.Popen(run, stdout=subprocess.PIPE, stderr=subprocess.PIPE)135output, err = process.communicate()136137m = re.search(r'Running (\d+) tests', output)138if m and int(m.group(1)) > 1:139print(output)140raise Exception('Found more than one test result in output')141142# Results are reported in the format:143# name_backend.metric: story= value units.144pattern = r'\.' + metric + r':.*= ([0-9.]+)'145logging.debug('searching for %s in output' % pattern)146m = re.findall(pattern, output)147if not m:148print(output)149raise Exception('Did not find the metric "%s" in the test output' % metric)150151return [float(value) for value in m]152153# Calibrate the number of steps154steps = get_results("steps_to_run", ["--calibration"] + extra_args)[0]155print("running with %d steps." % steps)156157# Loop 'args.experiments' times, running the tests.158for experiment in range(args.experiments):159experiment_scores = get_results(args.metric,160["--steps-per-trial", str(steps)] + extra_args)161162for score in experiment_scores:163sys.stdout.write("%s: %.2f" % (args.metric, score))164scores.append(score)165166if (len(scores) > 1):167sys.stdout.write(", mean: %.2f" % mean(scores))168sys.stdout.write(", variation: %.2f%%" %169(coefficient_of_variation(scores) * 100.0))170171if (len(scores) > 7):172truncation_n = len(scores) >> 3173sys.stdout.write(", truncated mean: %.2f" % truncated_mean(scores, truncation_n))174sys.stdout.write(", variation: %.2f%%" %175(truncated_cov(scores, truncation_n) * 100.0))176177print("")178179return EXIT_SUCCESS180181182if __name__ == "__main__":183sys.exit(main(sys.argv[1:]))184185186