Skip to content

Commit b61640c

Browse files
toby-colemanadriencaccia
authored andcommitted
Fix walltime mode
1 parent aa267f3 commit b61640c

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

src/pytest_codspeed/instruments/walltime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ def __codspeed_root_frame__(*args, **kwargs) -> T:
292292
if self.instrument_hooks:
293293
self.instrument_hooks.start_benchmark()
294294
for _ in range(pedantic_options.rounds):
295-
start = perf_counter_ns()
296295
args, kwargs = pedantic_options.setup_and_get_args_kwargs()
296+
start = perf_counter_ns()
297297
for _ in iter_range:
298298
__codspeed_root_frame__(*args, **kwargs)
299299
end = perf_counter_ns()

tests/test_pytest_plugin_walltime.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import pytest
22
from conftest import run_pytest_codspeed_with_mode
33

4+
from pytest_codspeed.config import (
5+
BenchmarkMarkerOptions,
6+
CodSpeedConfig,
7+
PedanticOptions,
8+
)
9+
from pytest_codspeed.instruments.walltime import WallTimeInstrument
410
from pytest_codspeed.instruments import MeasurementMode
511

612

@@ -86,3 +92,45 @@ def target(a, b, c):
8692
result = run_pytest_codspeed_with_mode(pytester, MeasurementMode.WallTime)
8793
assert result.ret == 0, "the run should have succeeded"
8894
result.assert_outcomes(passed=1)
95+
96+
97+
def test_benchmark_pedantic_walltime_setup_not_timed(monkeypatch: pytest.MonkeyPatch):
98+
"""Verify that the setup time is not included in the measurement when using pedantic mode with walltime."""
99+
current_time_ns = 0
100+
101+
def fake_perf_counter_ns() -> int:
102+
return current_time_ns
103+
104+
monkeypatch.setattr(
105+
"pytest_codspeed.instruments.walltime.perf_counter_ns", fake_perf_counter_ns
106+
)
107+
108+
def setup() -> tuple[tuple[int], dict[str, int]]:
109+
nonlocal current_time_ns
110+
current_time_ns += 200
111+
return (1,), {"c": 2}
112+
113+
def target(a: int, c: int) -> int:
114+
nonlocal current_time_ns
115+
current_time_ns += 400
116+
return a + c
117+
118+
instrument = WallTimeInstrument(CodSpeedConfig(), MeasurementMode.WallTime)
119+
result = instrument.measure_pedantic(
120+
BenchmarkMarkerOptions(),
121+
PedanticOptions(
122+
target=target,
123+
setup=setup,
124+
teardown=None,
125+
rounds=2,
126+
warmup_rounds=0,
127+
iterations=1,
128+
),
129+
name="test_pedantic_setup_not_timed",
130+
uri="tests/test_benchmark.py::test_pedantic_setup_not_timed",
131+
)
132+
133+
assert result == 3
134+
assert len(instrument.benchmarks) == 1
135+
# Two rounds should each measure target-only time (400ns), excluding setup (200ns).
136+
assert instrument.benchmarks[0].stats.min_ns == 400

0 commit comments

Comments
 (0)