Skip to content

Commit cc10330

Browse files
committed
fix: use time per iteration instead of total round time in stats
it doesn't affect existing data since having sub 100ns time in python is quite difficult
1 parent 003c3b4 commit cc10330

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/pytest_codspeed/instruments/walltime.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,14 @@ class BenchmarkStats:
6969
@classmethod
7070
def from_list(
7171
cls,
72-
times_ns: list[float],
72+
times_per_round_ns: list[float],
7373
*,
7474
rounds: int,
7575
iter_per_round: int,
7676
warmup_iters: int,
7777
total_time: float,
7878
) -> BenchmarkStats:
79+
times_ns = [t / iter_per_round for t in times_per_round_ns]
7980
stdev_ns = stdev(times_ns) if len(times_ns) > 1 else 0
8081
mean_ns = mean(times_ns)
8182
if len(times_ns) > 1:
@@ -129,20 +130,20 @@ def run_benchmark(
129130
out = fn(*args, **kwargs)
130131

131132
# Warmup
132-
times_ns: list[float] = []
133+
times_per_round_ns: list[float] = []
133134
warmup_start = start = perf_counter_ns()
134135
while True:
135136
start = perf_counter_ns()
136137
fn(*args, **kwargs)
137138
end = perf_counter_ns()
138-
times_ns.append(end - start)
139+
times_per_round_ns.append(end - start)
139140
if end - warmup_start > config.warmup_time_ns:
140141
break
141142

142143
# Round sizing
143-
warmup_mean_ns = mean(times_ns)
144-
warmup_iters = len(times_ns)
145-
times_ns.clear()
144+
warmup_mean_ns = mean(times_per_round_ns)
145+
warmup_iters = len(times_per_round_ns)
146+
times_per_round_ns.clear()
146147
iter_per_round = (
147148
int(ceil(config.min_round_time_ns / warmup_mean_ns))
148149
if warmup_mean_ns <= config.min_round_time_ns
@@ -163,7 +164,7 @@ def run_benchmark(
163164
for _ in iter_range:
164165
fn(*args, **kwargs)
165166
end = perf_counter_ns()
166-
times_ns.append(end - start)
167+
times_per_round_ns.append(end - start)
167168

168169
if end - run_start > config.max_time_ns:
169170
# TODO: log something
@@ -172,7 +173,7 @@ def run_benchmark(
172173
total_time = (benchmark_end - run_start) / 1e9
173174

174175
stats = BenchmarkStats.from_list(
175-
times_ns,
176+
times_per_round_ns,
176177
rounds=rounds,
177178
total_time=total_time,
178179
iter_per_round=iter_per_round,
@@ -240,12 +241,12 @@ def _print_benchmark_table(self) -> None:
240241

241242
for bench in self.benchmarks:
242243
rsd = bench.stats.stdev_ns / bench.stats.mean_ns
243-
rsd_text = Text(f"{rsd*100:.1f}%")
244+
rsd_text = Text(f"{rsd * 100:.1f}%")
244245
if rsd > 0.1:
245246
rsd_text.stylize("red bold")
246247
table.add_row(
247248
escape(bench.name),
248-
f"{bench.stats.min_ns/bench.stats.iter_per_round:,.0f}ns",
249+
f"{bench.stats.min_ns / bench.stats.iter_per_round:,.0f}ns",
249250
rsd_text,
250251
f"{bench.stats.total_time:,.2f}s",
251252
f"{bench.stats.iter_per_round * bench.stats.rounds:,}",

0 commit comments

Comments
 (0)