Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/angle
Path: blob/main_old/scripts/perf_test_runner.py
1693 views
1
#!/usr/bin/python2
2
#
3
# Copyright 2015 The ANGLE Project 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
#
7
# perf_test_runner.py:
8
# Helper script for running and analyzing perftest results. Runs the
9
# tests in an infinite batch, printing out the mean and coefficient of
10
# variation of the population continuously.
11
#
12
13
import argparse
14
import glob
15
import logging
16
import os
17
import re
18
import subprocess
19
import sys
20
21
base_path = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..'))
22
23
# Look for a [Rr]elease build.
24
TEST_SUITE_SEARCH_PATH = glob.glob('out/*elease*')
25
DEFAULT_METRIC = 'wall_time'
26
DEFAULT_EXPERIMENTS = 10
27
28
DEFAULT_TEST_SUITE = 'angle_perftests'
29
30
if sys.platform == 'win32':
31
DEFAULT_TEST_NAME = 'DrawCallPerfBenchmark.Run/d3d11_null'
32
else:
33
DEFAULT_TEST_NAME = 'DrawCallPerfBenchmark.Run/gl'
34
35
EXIT_SUCCESS = 0
36
EXIT_FAILURE = 1
37
38
scores = []
39
40
41
# Danke to http://stackoverflow.com/a/27758326
42
def mean(data):
43
"""Return the sample arithmetic mean of data."""
44
n = len(data)
45
if n < 1:
46
raise ValueError('mean requires at least one data point')
47
return float(sum(data)) / float(n) # in Python 2 use sum(data)/float(n)
48
49
50
def sum_of_square_deviations(data, c):
51
"""Return sum of square deviations of sequence data."""
52
ss = sum((float(x) - c)**2 for x in data)
53
return ss
54
55
56
def coefficient_of_variation(data):
57
"""Calculates the population coefficient of variation."""
58
n = len(data)
59
if n < 2:
60
raise ValueError('variance requires at least two data points')
61
c = mean(data)
62
ss = sum_of_square_deviations(data, c)
63
pvar = ss / n # the population variance
64
stddev = (pvar**0.5) # population standard deviation
65
return stddev / c
66
67
68
def truncated_list(data, n):
69
"""Compute a truncated list, n is truncation size"""
70
if len(data) < n * 2:
71
raise ValueError('list not large enough to truncate')
72
return sorted(data)[n:-n]
73
74
75
def truncated_mean(data, n):
76
"""Compute a truncated mean, n is truncation size"""
77
return mean(truncated_list(data, n))
78
79
80
def truncated_cov(data, n):
81
"""Compute a truncated coefficient of variation, n is truncation size"""
82
return coefficient_of_variation(truncated_list(data, n))
83
84
85
def main(raw_args):
86
parser = argparse.ArgumentParser()
87
parser.add_argument(
88
'--suite',
89
help='Test suite binary. Default is "%s".' % DEFAULT_TEST_SUITE,
90
default=DEFAULT_TEST_SUITE)
91
parser.add_argument(
92
'-m',
93
'--metric',
94
help='Test metric. Default is "%s".' % DEFAULT_METRIC,
95
default=DEFAULT_METRIC)
96
parser.add_argument(
97
'--experiments',
98
help='Number of experiments to run. Default is %d.' % DEFAULT_EXPERIMENTS,
99
default=DEFAULT_EXPERIMENTS,
100
type=int)
101
parser.add_argument('-v', '--verbose', help='Extra verbose logging.', action='store_true')
102
parser.add_argument('test_name', help='Test to run', default=DEFAULT_TEST_NAME)
103
args, extra_args = parser.parse_known_args(raw_args)
104
105
if args.verbose:
106
logging.basicConfig(level='DEBUG')
107
108
if sys.platform == 'win32':
109
args.suite += '.exe'
110
111
# Find most recent binary
112
newest_binary = None
113
newest_mtime = None
114
115
for path in TEST_SUITE_SEARCH_PATH:
116
binary_path = os.path.join(base_path, path, args.suite)
117
if os.path.exists(binary_path):
118
binary_mtime = os.path.getmtime(binary_path)
119
if (newest_binary is None) or (binary_mtime > newest_mtime):
120
newest_binary = binary_path
121
newest_mtime = binary_mtime
122
123
perftests_path = newest_binary
124
125
if perftests_path == None or not os.path.exists(perftests_path):
126
print('Cannot find Release %s!' % args.test_suite)
127
return EXIT_FAILURE
128
129
print('Using test executable: %s' % perftests_path)
130
print('Test name: %s' % args.test_name)
131
132
def get_results(metric, extra_args=[]):
133
run = [perftests_path, '--gtest_filter=%s' % args.test_name] + extra_args
134
logging.info('running %s' % str(run))
135
process = subprocess.Popen(run, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
136
output, err = process.communicate()
137
138
m = re.search(r'Running (\d+) tests', output)
139
if m and int(m.group(1)) > 1:
140
print(output)
141
raise Exception('Found more than one test result in output')
142
143
# Results are reported in the format:
144
# name_backend.metric: story= value units.
145
pattern = r'\.' + metric + r':.*= ([0-9.]+)'
146
logging.debug('searching for %s in output' % pattern)
147
m = re.findall(pattern, output)
148
if not m:
149
print(output)
150
raise Exception('Did not find the metric "%s" in the test output' % metric)
151
152
return [float(value) for value in m]
153
154
# Calibrate the number of steps
155
steps = get_results("steps_to_run", ["--calibration"] + extra_args)[0]
156
print("running with %d steps." % steps)
157
158
# Loop 'args.experiments' times, running the tests.
159
for experiment in range(args.experiments):
160
experiment_scores = get_results(args.metric,
161
["--steps-per-trial", str(steps)] + extra_args)
162
163
for score in experiment_scores:
164
sys.stdout.write("%s: %.2f" % (args.metric, score))
165
scores.append(score)
166
167
if (len(scores) > 1):
168
sys.stdout.write(", mean: %.2f" % mean(scores))
169
sys.stdout.write(", variation: %.2f%%" %
170
(coefficient_of_variation(scores) * 100.0))
171
172
if (len(scores) > 7):
173
truncation_n = len(scores) >> 3
174
sys.stdout.write(", truncated mean: %.2f" % truncated_mean(scores, truncation_n))
175
sys.stdout.write(", variation: %.2f%%" %
176
(truncated_cov(scores, truncation_n) * 100.0))
177
178
print("")
179
180
return EXIT_SUCCESS
181
182
183
if __name__ == "__main__":
184
sys.exit(main(sys.argv[1:]))
185
186