Skip to content

Commit 8637946

Browse files
committed
Fix walltime mode
1 parent aa267f3 commit 8637946

2 files changed

Lines changed: 45 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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import pytest
22
from conftest import run_pytest_codspeed_with_mode
33

4+
from pytest_codspeed.config import BenchmarkMarkerOptions, CodSpeedConfig, PedanticOptions
5+
from pytest_codspeed.instruments.walltime import WallTimeInstrument
46
from pytest_codspeed.instruments import MeasurementMode
57

68

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

0 commit comments

Comments
 (0)