-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathwalltime.py
More file actions
269 lines (231 loc) · 7.64 KB
/
walltime.py
File metadata and controls
269 lines (231 loc) · 7.64 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
from __future__ import annotations
from dataclasses import asdict, dataclass
from math import ceil
from statistics import mean, quantiles, stdev
from time import get_clock_info, perf_counter_ns
from typing import TYPE_CHECKING
from rich.console import Console
from rich.markup import escape
from rich.table import Table
from rich.text import Text
from pytest_codspeed.benchmark import BenchmarkMetadata
from pytest_codspeed.instruments import Instrument
if TYPE_CHECKING:
from typing import Any, Callable
from pytest import Session
from pytest_codspeed.instruments import P, T
from pytest_codspeed.plugin import CodSpeedConfig
DEFAULT_WARMUP_TIME_NS = 1_000_000_000
DEFAULT_MAX_TIME_NS = 3_000_000_000
TIMER_RESOLUTION_NS = get_clock_info("perf_counter").resolution * 1e9
DEFAULT_MIN_ROUND_TIME_NS = TIMER_RESOLUTION_NS * 100
@dataclass
class WalltimeBenchmarkConfig:
warmup_time_ns: int
min_round_time_ns: float
max_time_ns: int
max_rounds: int | None
@classmethod
def from_codspeed_config(cls, config: CodSpeedConfig) -> WalltimeBenchmarkConfig:
return cls(
warmup_time_ns=config.warmup_time_ns
if config.warmup_time_ns is not None
else DEFAULT_WARMUP_TIME_NS,
min_round_time_ns=DEFAULT_MIN_ROUND_TIME_NS,
max_time_ns=config.max_time_ns
if config.max_time_ns is not None
else DEFAULT_MAX_TIME_NS,
max_rounds=config.max_rounds,
)
@dataclass
class WalltimeBenchmarkStats:
min_ns: float
max_ns: float
mean_ns: float
stdev_ns: float
q1_ns: float
median_ns: float
q3_ns: float
rounds: int
total_time: float
iqr_outlier_rounds: int
stdev_outlier_rounds: int
iter_per_round: int
warmup_iters: int
@classmethod
def from_list(
cls,
times_ns: list[float],
*,
rounds: int,
iter_per_round: int,
warmup_iters: int,
total_time: float,
) -> WalltimeBenchmarkStats:
stdev_ns = stdev(times_ns) if len(times_ns) > 1 else 0
mean_ns = mean(times_ns)
if len(times_ns) > 1:
q1_ns, median_ns, q3_ns = quantiles(times_ns, n=4)
else:
q1_ns, median_ns, q3_ns = (
mean_ns,
mean_ns,
mean_ns,
)
iqr_ns = q3_ns - q1_ns
iqr_outlier_rounds = sum(
1 for t in times_ns if t < q1_ns - 1.5 * iqr_ns or t > q3_ns + 1.5 * iqr_ns
)
stdev_outlier_rounds = sum(
1
for t in times_ns
if t < mean_ns - 3 * stdev_ns or t > mean_ns + 3 * stdev_ns
)
return cls(
min_ns=min(times_ns),
max_ns=max(times_ns),
stdev_ns=stdev_ns,
mean_ns=mean_ns,
q1_ns=q1_ns,
median_ns=median_ns,
q3_ns=q3_ns,
rounds=rounds,
total_time=total_time,
iqr_outlier_rounds=iqr_outlier_rounds,
stdev_outlier_rounds=stdev_outlier_rounds,
iter_per_round=iter_per_round,
warmup_iters=warmup_iters,
)
@dataclass
class WalltimeBenchmark(BenchmarkMetadata):
config: WalltimeBenchmarkConfig
stats: WalltimeBenchmarkStats
def run_benchmark(
benchmark_metadata: BenchmarkMetadata,
fn: Callable[P, T],
args,
kwargs,
config: WalltimeBenchmarkConfig,
) -> tuple[WalltimeBenchmark, T]:
# Compute the actual result of the function
out = fn(*args, **kwargs)
# Warmup
times_ns: list[float] = []
warmup_start = start = perf_counter_ns()
while True:
start = perf_counter_ns()
fn(*args, **kwargs)
end = perf_counter_ns()
times_ns.append(end - start)
if end - warmup_start > config.warmup_time_ns:
break
# Round sizing
warmup_mean_ns = mean(times_ns)
warmup_iters = len(times_ns)
times_ns.clear()
iter_per_round = (
int(ceil(config.min_round_time_ns / warmup_mean_ns))
if warmup_mean_ns <= config.min_round_time_ns
else 1
)
if config.max_rounds is None:
round_time_ns = warmup_mean_ns * iter_per_round
rounds = int(config.max_time_ns / round_time_ns)
else:
rounds = config.max_rounds
rounds = max(1, rounds)
# Benchmark
iter_range = range(iter_per_round)
run_start = perf_counter_ns()
for _ in range(rounds):
start = perf_counter_ns()
for _ in iter_range:
fn(*args, **kwargs)
end = perf_counter_ns()
times_ns.append(end - start)
if end - run_start > config.max_time_ns:
# TODO: log something
break
benchmark_end = perf_counter_ns()
total_time = (benchmark_end - run_start) / 1e9
stats = WalltimeBenchmarkStats.from_list(
times_ns,
rounds=rounds,
total_time=total_time,
iter_per_round=iter_per_round,
warmup_iters=warmup_iters,
)
return WalltimeBenchmark(
**asdict(benchmark_metadata),
config=config,
stats=stats,
), out
class WallTimeInstrument(Instrument):
instrument = "walltime"
def __init__(self, config: CodSpeedConfig) -> None:
self.config = config
self.benchmarks: list[WalltimeBenchmark] = []
def get_instrument_config_str_and_warns(self) -> tuple[str, list[str]]:
return f"mode: walltime, timer_resolution: {TIMER_RESOLUTION_NS:.1f}ns", []
def measure(
self,
benchmark_metadata: BenchmarkMetadata,
fn: Callable[P, T],
*args: P.args,
**kwargs: P.kwargs,
) -> T:
bench, out = run_benchmark(
benchmark_metadata=benchmark_metadata,
fn=fn,
args=args,
kwargs=kwargs,
config=WalltimeBenchmarkConfig.from_codspeed_config(self.config),
)
self.benchmarks.append(bench)
return out
def report(self, session: Session) -> None:
reporter = session.config.pluginmanager.get_plugin("terminalreporter")
if len(self.benchmarks) == 0:
reporter.write_sep(
"=",
f"{len(self.benchmarks)} benchmarked",
)
return
self._print_benchmark_table()
reporter.write_sep(
"=",
f"{len(self.benchmarks)} benchmarked",
)
def _print_benchmark_table(self) -> None:
table = Table(title="Benchmark Results")
table.add_column("Benchmark", justify="right", style="cyan", no_wrap=True)
table.add_column("Time (best)", justify="right", style="green bold")
table.add_column(
"Rel. StdDev",
justify="right",
)
table.add_column("Run time", justify="right")
table.add_column("Iters", justify="right")
for bench in self.benchmarks:
rsd = bench.stats.stdev_ns / bench.stats.mean_ns
rsd_text = Text(f"{rsd*100:.1f}%")
if rsd > 0.1:
rsd_text.stylize("red bold")
table.add_row(
escape(bench.display_name),
f"{bench.stats.min_ns/bench.stats.iter_per_round:,.0f}ns",
rsd_text,
f"{bench.stats.total_time:,.2f}s",
f"{bench.stats.iter_per_round * bench.stats.rounds:,}",
)
console = Console()
print("\n")
console.print(table)
def get_result_dict(self) -> dict[str, Any]:
return {
"instrument": {
"type": self.instrument,
"clock_info": get_clock_info("perf_counter").__dict__,
},
"benchmarks": [asdict(bench) for bench in self.benchmarks],
}