-
-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathrun.py
More file actions
254 lines (209 loc) · 7.26 KB
/
Copy pathrun.py
File metadata and controls
254 lines (209 loc) · 7.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import hashlib
import json
import os
import sys
import time
import traceback
from collections import namedtuple
import pyperformance
from . import _python, _pythoninfo, _utils, _venv
from .venv import REQUIREMENTS_FILE, VenvForBenchmarks
class BenchmarkException(Exception):
pass
class RunID(namedtuple("RunID", "python compat bench timestamp")):
def __new__(cls, python, compat, bench, timestamp):
self = super().__new__(
cls,
python,
compat,
bench or None,
int(timestamp) if timestamp else None,
)
return self
def __str__(self):
if not self.timestamp:
return self.name
return f"{self.name}-{self.timestamp}"
@property
def name(self):
try:
return self._name
except AttributeError:
name = f"{self.python}-compat-{self.compat}"
if self.bench:
name = f"{name}-bm-{self.bench.name}"
self._name = name
return self._name
def get_run_id(python, bench=None):
py_id = _python.get_id(python, prefix=True)
compat_id = get_compatibility_id(bench)
ts = time.time()
return RunID(py_id, compat_id, bench, ts)
def get_loops_from_file(filename):
with open(filename) as fd:
data = json.load(fd)
loops = {}
for benchmark in data["benchmarks"]:
metadata = benchmark.get("metadata", data["metadata"])
name = metadata["name"]
if name.endswith("_none"):
name = name[: -len("_none")]
if "loops" in metadata:
loops[name] = metadata["loops"]
return loops
def run_benchmarks(should_run, python, options):
if options.same_loops is not None:
loops = get_loops_from_file(options.same_loops)
else:
loops = {}
to_run = sorted(should_run)
info = _pythoninfo.get_info(python)
runid = get_run_id(info)
unique = getattr(options, "unique_venvs", False)
if not unique:
common = VenvForBenchmarks.ensure(
_venv.get_venv_root(runid.name, python=info),
info,
upgrade="oncreate",
inherit_environ=options.inherit_environ,
)
benchmarks = {}
venvs = set()
for i, bench in enumerate(to_run):
bench_runid = runid._replace(bench=bench)
assert bench_runid.name, (bench, bench_runid)
name = bench_runid.name
venv_root = _venv.get_venv_root(name, python=info)
print()
print("=" * 50)
print(f"({i + 1:>2}/{len(to_run)}) creating venv for benchmark ({bench.name})")
print()
if not unique:
print("(trying common venv first)")
# Try the common venv first.
try:
common.ensure_reqs(bench)
except _venv.RequirementsInstallationFailedError:
print("(falling back to unique venv)")
else:
benchmarks[bench] = (common, bench_runid)
continue
try:
venv = VenvForBenchmarks.ensure(
venv_root,
info,
upgrade="oncreate",
inherit_environ=options.inherit_environ,
)
# XXX Do not override when there is a requirements collision.
venv.ensure_reqs(bench)
except _venv.RequirementsInstallationFailedError:
print("(benchmark will be skipped)")
print()
venv = None
venvs.add(venv_root)
benchmarks[bench] = (venv, bench_runid)
print()
suite = None
run_count = str(len(to_run))
errors = []
base_pyperf_opts = get_pyperf_opts(options)
import pyperf
for index, bench in enumerate(to_run):
name = bench.name
print("[%s/%s] %s..." % (str(index + 1).rjust(len(run_count)), run_count, name))
sys.stdout.flush()
def add_bench(dest_suite, obj):
if isinstance(obj, pyperf.BenchmarkSuite):
results = obj
else:
results = (obj,)
version = pyperformance.__version__
for res in results:
res.update_metadata(
{"performance_version": version, "tags": bench.tags}
)
if dest_suite is not None:
dest_suite.add_benchmark(res)
else:
dest_suite = pyperf.BenchmarkSuite([res])
return dest_suite
if name in loops:
pyperf_opts = [*base_pyperf_opts, f"--loops={loops[name]}"]
else:
pyperf_opts = base_pyperf_opts
bench_venv, bench_runid = benchmarks.get(bench)
if bench_venv is None:
print("ERROR: Benchmark %s failed: could not install requirements" % name)
errors.append((name, "Install requirements error"))
continue
try:
result = bench.run(
bench_venv.python,
bench_runid,
pyperf_opts,
venv=bench_venv,
verbose=options.verbose,
)
except TimeoutError as exc:
print("ERROR: Benchmark %s timed out" % name)
errors.append((name, exc))
except RuntimeError as exc:
print("ERROR: Benchmark %s failed: %s" % (name, exc))
traceback.print_exc()
errors.append((name, exc))
except Exception as exc:
print("ERROR: Benchmark %s failed: %s" % (name, exc))
traceback.print_exc()
errors.append((name, exc))
else:
suite = add_bench(suite, result)
print()
return (suite, errors)
# Utility functions
def get_compatibility_id(bench=None):
# XXX Do not include the pyperformance reqs if a benchmark was provided?
reqs = sorted(_utils.iter_clean_lines(REQUIREMENTS_FILE))
if bench:
lockfile = bench.requirements_lockfile
if lockfile and os.path.exists(lockfile):
reqs += sorted(_utils.iter_clean_lines(lockfile))
data = [
# XXX Favor pyperf.__version__ instead?
pyperformance.__version__,
"\n".join(reqs),
]
h = hashlib.sha256()
for value in data:
h.update(value.encode("utf-8"))
compat_id = h.hexdigest()
# XXX Return the whole string?
compat_id = compat_id[:12]
return compat_id
def get_pyperf_opts(options):
opts = []
if options.debug_single_value:
opts.append("--debug-single-value")
elif options.rigorous:
opts.append("--rigorous")
elif options.fast:
opts.append("--fast")
if options.verbose:
opts.append("--verbose")
if options.affinity:
opts.append("--affinity=%s" % options.affinity)
if options.track_memory:
opts.append("--track-memory")
if options.inherit_environ:
opts.append("--inherit-environ=%s" % ",".join(options.inherit_environ))
if options.min_time:
opts.append("--min-time=%s" % options.min_time)
if options.timeout:
opts.append("--timeout=%s" % options.timeout)
if options.hook:
for hook in options.hook:
opts.append("--hook=%s" % hook)
# --warmups=0 is a valid option, so check for `not None` here
if options.warmups is not None:
opts.append("--warmups=%s" % options.warmups)
return opts