-
Notifications
You must be signed in to change notification settings - Fork 8
/
runbench.py
301 lines (259 loc) · 9.67 KB
/
runbench.py
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
from __future__ import annotations
from importlib import import_module
from typing import NamedTuple
import argparse
import glob
import re
import os
import sys
import time
import subprocess
import statistics
from pathlib import Path
from benchmarking import BenchmarkInfo, benchmarks
from typing_extensions import Final
# Minimum total time (seconds) to run a benchmark
MIN_TIME = 2.0
# Minimum number of iterations to run a benchmark
MIN_ITER = 10
BINARY_EXTENSION: Final = 'pyd' if sys.platform == 'win32' else 'so'
def run_in_subprocess(benchmark: BenchmarkInfo,
binary: str | None,
compiled: bool,
priority: bool = False,
env: dict[str, str] | None = None) -> float:
module = benchmark.module
program = 'import %s; import benchmarking as bm; print("\\nelapsed:", bm.run_once("%s"))' % (
module,
benchmark.name,
)
if not compiled and binary:
os.rename(binary, binary + '.tmp')
cmd = [sys.executable, '-c', program]
if priority:
# Use nice to increase process priority.
cmd = ['sudo', 'nice', '-n', '-5'] + cmd
try:
result = subprocess.run(cmd, check=True, stdout=subprocess.PIPE, env=env)
finally:
if not compiled and binary:
os.rename(binary + '.tmp', binary)
return parse_elapsed_time(result.stdout)
def parse_elapsed_time(output: bytes) -> float:
m = re.search(rb"\belapsed: ([-+0-9.e]+)\b", output)
assert m is not None, 'could not find elapsed time in output:\n%r' % output
return float(m.group(1))
def smoothen(a: list[float]) -> list[float]:
# Keep the lowest half of values
return sorted(a)[: (len(a) + 1) // 2]
def run_benchmark(benchmark: BenchmarkInfo,
binary: str | None,
raw_output: bool,
priority: bool,
interpreted: bool,
compiled: bool,
min_iter: int,
mypy_repo: str | None) -> None:
assert compiled or interpreted
if benchmark.compiled_only:
assert not interpreted
if min_iter < 0:
# Use default minimum iterations
if benchmark.min_iterations is not None:
min_iter = benchmark.min_iterations
else:
min_iter = MIN_ITER
if benchmark.prepare:
if not raw_output:
print('preparing %s' % benchmark.name)
benchmark.prepare(mypy_repo)
if not raw_output:
print('running %s' % benchmark.name)
# Warm up
if interpreted:
run_in_subprocess(benchmark, binary, compiled=False)
if compiled:
run_in_subprocess(benchmark, binary, compiled=True)
env = os.environ.copy()
times_compiled = []
times_interpreted = []
n = 0
while True:
if benchmark.stable_hash_seed:
# This makes hash values more predictable.
env["PYTHONHASHSEED"] = "1"
if compiled:
t = run_in_subprocess(benchmark, binary, compiled=True, priority=priority, env=env)
times_compiled.append(t)
if interpreted:
t = run_in_subprocess(benchmark, binary, compiled=False, priority=priority, env=env)
times_interpreted.append(t)
if not raw_output:
sys.stdout.write('.')
sys.stdout.flush()
n += 1
long_enough = sum(times_interpreted) >= MIN_TIME or sum(times_compiled) >= MIN_TIME
if long_enough and n >= min_iter:
break
if not raw_output:
print()
if benchmark.compiled_only:
# TODO: Remove this once it's no longer needed for debugging
print(f'runtimes: {sorted(times_compiled)}')
if benchmark.strip_outlier_runs:
times_interpreted = smoothen(times_interpreted)
times_compiled = smoothen(times_compiled)
n = max(len(times_interpreted), len(times_compiled))
if interpreted:
stdev1 = statistics.stdev(times_interpreted)
mean1 = sum(times_interpreted) / n
else:
stdev1 = 0.0
mean1 = 0.0
if compiled:
stdev2 = statistics.stdev(times_compiled)
mean2 = sum(times_compiled) / n
else:
stdev2 = 0.0
mean2 = 0.0
if not raw_output:
if interpreted:
print('interpreted: %.6fs (avg of %d iterations; stdev %.2g%%)' % (
mean1, n, 100.0 * stdev1 / mean1)
)
if compiled:
print('compiled: %.6fs (avg of %d iterations; stdev %.2g%%)' % (
mean2, n, 100.0 * stdev2 / mean2)
)
if compiled and interpreted:
print()
relative = sum(times_interpreted) / sum(times_compiled)
print('compiled is %.3fx faster' % relative)
else:
print('%d %.6f %.6f %.6f %.6f' % (
n,
sum(times_interpreted) / n,
stdev1,
sum(times_compiled) / n,
stdev2))
def compile_benchmark(module: str, raw_output: bool, mypy_repo: str | None) -> str:
fnam = module.replace('.', '/') + '.py'
if not raw_output:
print('compiling %s...' % module)
env = os.environ.copy()
legacy_script = None
if mypy_repo:
# Use mypyc from specific mypy repository.
env['PYTHONPATH'] = mypy_repo
script_path = os.path.join(mypy_repo, 'scripts', 'mypyc')
if os.path.isfile(script_path):
# With older mypy revisions we must use scripts/mypyc.
legacy_script = script_path
if not legacy_script:
cmd = [sys.executable, '-m', 'mypyc']
else:
cmd = [sys.executable, legacy_script]
subprocess.run(cmd + [fnam], check=True, env=env)
pattern = module.replace('.', '/') + f'.*.{BINARY_EXTENSION}'
paths = glob.glob(pattern)
assert len(paths) == 1
return paths[0]
def import_all() -> None:
files = glob.glob('microbenchmarks/*.py')
files += glob.glob('benchmarks/*.py')
for fnam in files:
filepath = Path(fnam).resolve()
if filepath.name == '__init__.py' or filepath.suffix != '.py':
continue
benchmarks_root_dir = Path(__file__).parent.resolve()
module_parts = filepath.with_suffix("").relative_to(benchmarks_root_dir).parts
module = ".".join(module_parts)
import_module(module)
def delete_binaries() -> None:
files = glob.glob(f'microbenchmarks/*.{BINARY_EXTENSION}')
files += glob.glob(f'benchmarks/*.{BINARY_EXTENSION}')
for fnam in files:
os.remove(fnam)
class Args(NamedTuple):
benchmark: str
mypy_repo: str | None
is_list: bool
raw: bool
priority: bool
compiled_only: bool
interpreted_only: bool
min_iter: int
def parse_args() -> Args:
parser = argparse.ArgumentParser(
description="Run a mypyc benchmark in compiled and/or interpreted modes.")
parser.add_argument('benchmark', nargs='?',
help="name of benchmark to run (use --list to show options)")
parser.add_argument('--mypy-repo', metavar="DIR", type=str, default=None,
help="""use mypyc from a mypy git repository (by default, use mypyc
found via PATH and PYTHONPATH)""")
parser.add_argument('--list', action='store_true', help='show names of all benchmarks')
parser.add_argument('--raw', action='store_true', help='use machine-readable raw output')
parser.add_argument('--priority', action='store_true',
help="increase process priority using 'nice' (uses sudo)")
parser.add_argument('-c', action='store_true',
help="only run in compiled mode")
parser.add_argument('-i', action='store_true',
help="only run in interpreted mode")
parser.add_argument('--min-iter', type=int, default=-1, metavar="N",
help="""set minimum number of iterations (half of the results
will be discarded; default %d)""" % MIN_ITER)
parsed = parser.parse_args()
if not parsed.list and not parsed.benchmark:
parser.print_help()
sys.exit(2)
args = Args(parsed.benchmark,
parsed.mypy_repo,
parsed.list,
parsed.raw,
parsed.priority,
parsed.c,
parsed.i,
parsed.min_iter)
if args.compiled_only and args.interpreted_only:
sys.exit("error: only give one of -c and -i")
return args
def main() -> None:
# Delete compiled modules before importing, as they may be stale.
delete_binaries()
# Import before parsing args so that syntax errors get reported.
import_all()
args = parse_args()
if args.is_list:
for benchmark in sorted(benchmarks):
suffix = ''
if not args.raw:
if benchmark.module.startswith('microbenchmarks.'):
suffix += ' (micro)'
if benchmark.compiled_only:
suffix += ' (compiled only)'
print(benchmark.name + suffix)
sys.exit(0)
name = args.benchmark
for benchmark in benchmarks:
if benchmark.name == name:
break
else:
sys.exit('unknown benchmark %r' % name)
if not args.compiled_only and benchmark.compiled_only:
sys.exit(f'Benchmark "{benchmark.name}" cannot be run in interpreted mode')
if args.interpreted_only:
binary = None
else:
binary = compile_benchmark(benchmark.module, args.raw, args.mypy_repo)
run_benchmark(
benchmark,
binary,
args.raw,
args.priority,
not args.compiled_only,
not args.interpreted_only,
args.min_iter,
args.mypy_repo,
)
if __name__ == "__main__":
main()