Repository navigation
Expand file tree
/
Copy pathrun.py
More file actions
197 lines (169 loc) · 6.39 KB
/
Copy pathrun.py
File metadata and controls
197 lines (169 loc) · 6.39 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
import argparse
from fnmatch import fnmatch
import random
import sys
import cProfile
import time
import gc
import typing
PROFILES = {
"fast": {"confidence_level": 0.80, "precision": 0.05, "rounds": 3},
"default": {"confidence_level": 0.95, "precision": 0.02, "rounds": 5},
"slow": {"confidence_level": 0.95, "precision": 0.01, "rounds": 10},
"slower": {"confidence_level": 0.98, "precision": 0.005, "rounds": 15},
"slowest": {"confidence_level": 0.99, "precision": 0.0025, "rounds": 20},
}
ap = argparse.ArgumentParser()
ap.add_argument(
"-p",
"--profile",
default="default",
choices=PROFILES,
help="Benchmark stability profile: fast, default, slow, slower, or slowest",
)
ap.add_argument(
"-t",
"--mintime",
default=1,
type=float,
help="Continue testing for at least this many seconds per test",
)
ap.add_argument(
"-r",
"--rounds",
default=None,
type=int,
help="Minimum number of times to repeat the entire benchmark",
)
ap.add_argument(
"--append",
action="store_true",
help="Append results to previous run instead of replacing results.",
)
ap.add_argument(
"--list",
action="store_true",
help="List all benchmark names instead of running them.",
)
ap.add_argument(
"--sleep",
default=0.1,
type=int,
help="Seconds to wait between tests to allow CPUs to cool down.",
)
ap.add_argument(
"benchmarks", nargs="*", default="*", help="Glob patterns for benchmarks to run"
)
def shuffle(values):
values = list(values)
random.shuffle(values)
return values
if __name__ == "__main__":
args = ap.parse_args()
#: Allow CPU to cool down between tests
sleeptime = args.sleep
#: Continue testing for at least this many seconds per test
mintime = args.mintime
#: Confidence interval and precision for the selected stability profile
profile = PROFILES[args.profile]
confidence_level = profile["confidence_level"]
precision = profile["precision"]
rounds = args.rounds if args.rounds is not None else profile["rounds"]
from multipart_bench.scenarios import SCENARIOS, Result, Scenario
from multipart_bench.parsers import PARSERS, dummy_parser
if args.list:
for scenario in SCENARIOS:
for parser in PARSERS:
name = scenario.name_for(parser)
if not any(fnmatch(name, glob) for glob in args.benchmarks):
continue
print(name)
sys.exit(0)
print("Preparing benchmarks...")
alltests: list[
tuple[str, Scenario, typing.Callable[[Scenario], Result]]
] = [] # (name, scenario, parser)
baseline = PARSERS[0]
calibrated_n = {}
# Collecting and calibrating benchmarks (scenarios x parsers)
for scenario in shuffle(SCENARIOS):
for parser in shuffle(PARSERS):
name = scenario.name_for(parser)
if not any(fnmatch(name, glob) for glob in args.benchmarks):
continue
# Create profiles for each benchmark and skip failing benchmarks
pr = cProfile.Profile(timer=time.perf_counter)
pr.enable()
try:
scenario.run_once(parser)
pr.disable()
pr.dump_stats(f"var/{name}.prof")
except Exception as e:
pr.disable()
print(f"Skipping {name}: {e}")
continue
# Calibarate the number of repeats per test so that each test needs roughly
# the same time to complete. This makes fast tests more stable, while slow
# tests still complete in a reasonable amount of time.
gc.collect()
target_time = 1.0
min_n = 10
result = scenario.run_bench(parser, n=min_n, null_func=dummy_parser) * min_n
calibrated_n[name] = n = max(min_n, int(target_time // result))
print(f"Seeded {name} (n={n}) ", flush=True)
alltests.append((name, scenario, parser))
print(
f"Running benchmarks ({args.profile}: {rounds} min rounds, {confidence_level:.0%} confidence level, ±{precision:.2%} precision) ..."
)
results: dict[str, Result] = {}
confidence_reached = set()
round = 0
while round < rounds or any(name not in confidence_reached for name in results):
print()
print(
f"Round {round + 1}/{rounds}: Skipping {len(confidence_reached)}/{len(alltests)} stable tests"
)
for name, scenario, parser in shuffle(alltests):
if name in confidence_reached:
continue
print(f"{round + 1}/{rounds} {name} ", end="", flush=True)
rfname = f"var/{name}.json"
# Load previous results in append mode
if args.append and name not in results:
try:
results[name] = Result.load(rfname)
except FileNotFoundError:
pass
if name not in results:
results[name] = Result(name, scenario.size, [])
result = results[name]
# Allow CPU to cool down
time.sleep(sleeptime)
# Run the actual benchmark
measurement = scenario.run_bench(
parser, n=calibrated_n[name], null_func=dummy_parser
)
result.times.append(measurement)
# Store results for later processing
result.save_to(rfname)
# Mark test as 'good enough' after min-rounds
if (
round + 1 >= rounds
and name not in confidence_reached
and result.relative_confidence_interval(confidence_level) <= precision
):
confidence_reached.add(name)
# Print result
if baseline is parser or round == 0:
print(
f"{result.throughput / 1024 / 1024:.2f}MB/s (±{result.relative_confidence_interval(confidence_level):.2%}, n={calibrated_n[name]})"
)
else:
bsresult = results[scenario.name_for(baseline)]
percent = 100 * (
(result.throughput - bsresult.throughput) / bsresult.throughput
)
print(
f"{result.throughput / 1024 / 1024:.2f}MB/s ({percent:+.2f}%, ±{result.relative_confidence_interval(confidence_level):.2%}, n={calibrated_n[name]})"
)
round += 1