From b61640c66b3bd2e9b5e925b9b91a4ed00183f31b Mon Sep 17 00:00:00 2001 From: Toby Coleman Date: Mon, 6 Apr 2026 10:28:04 +0000 Subject: [PATCH 1/2] Fix walltime mode --- src/pytest_codspeed/instruments/walltime.py | 2 +- tests/test_pytest_plugin_walltime.py | 48 +++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/pytest_codspeed/instruments/walltime.py b/src/pytest_codspeed/instruments/walltime.py index 17673a3..cb108d4 100644 --- a/src/pytest_codspeed/instruments/walltime.py +++ b/src/pytest_codspeed/instruments/walltime.py @@ -292,8 +292,8 @@ def __codspeed_root_frame__(*args, **kwargs) -> T: if self.instrument_hooks: self.instrument_hooks.start_benchmark() for _ in range(pedantic_options.rounds): - start = perf_counter_ns() args, kwargs = pedantic_options.setup_and_get_args_kwargs() + start = perf_counter_ns() for _ in iter_range: __codspeed_root_frame__(*args, **kwargs) end = perf_counter_ns() diff --git a/tests/test_pytest_plugin_walltime.py b/tests/test_pytest_plugin_walltime.py index 510ab30..d82d2f6 100644 --- a/tests/test_pytest_plugin_walltime.py +++ b/tests/test_pytest_plugin_walltime.py @@ -1,6 +1,12 @@ import pytest from conftest import run_pytest_codspeed_with_mode +from pytest_codspeed.config import ( + BenchmarkMarkerOptions, + CodSpeedConfig, + PedanticOptions, +) +from pytest_codspeed.instruments.walltime import WallTimeInstrument from pytest_codspeed.instruments import MeasurementMode @@ -86,3 +92,45 @@ def target(a, b, c): result = run_pytest_codspeed_with_mode(pytester, MeasurementMode.WallTime) assert result.ret == 0, "the run should have succeeded" result.assert_outcomes(passed=1) + + +def test_benchmark_pedantic_walltime_setup_not_timed(monkeypatch: pytest.MonkeyPatch): + """Verify that the setup time is not included in the measurement when using pedantic mode with walltime.""" + current_time_ns = 0 + + def fake_perf_counter_ns() -> int: + return current_time_ns + + monkeypatch.setattr( + "pytest_codspeed.instruments.walltime.perf_counter_ns", fake_perf_counter_ns + ) + + def setup() -> tuple[tuple[int], dict[str, int]]: + nonlocal current_time_ns + current_time_ns += 200 + return (1,), {"c": 2} + + def target(a: int, c: int) -> int: + nonlocal current_time_ns + current_time_ns += 400 + return a + c + + instrument = WallTimeInstrument(CodSpeedConfig(), MeasurementMode.WallTime) + result = instrument.measure_pedantic( + BenchmarkMarkerOptions(), + PedanticOptions( + target=target, + setup=setup, + teardown=None, + rounds=2, + warmup_rounds=0, + iterations=1, + ), + name="test_pedantic_setup_not_timed", + uri="tests/test_benchmark.py::test_pedantic_setup_not_timed", + ) + + assert result == 3 + assert len(instrument.benchmarks) == 1 + # Two rounds should each measure target-only time (400ns), excluding setup (200ns). + assert instrument.benchmarks[0].stats.min_ns == 400 From 4a1f0f88dd49bfa9a06fbe78de3b72dc2f205f17 Mon Sep 17 00:00:00 2001 From: Adrien Cacciaguerra Date: Wed, 8 Apr 2026 15:55:56 +0200 Subject: [PATCH 2/2] fix: fix ruff lint errors in walltime test Co-Authored-By: Claude Opus 4.6 (1M context) --- tests/test_pytest_plugin_walltime.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/test_pytest_plugin_walltime.py b/tests/test_pytest_plugin_walltime.py index d82d2f6..0aaaa76 100644 --- a/tests/test_pytest_plugin_walltime.py +++ b/tests/test_pytest_plugin_walltime.py @@ -1,13 +1,19 @@ -import pytest +from __future__ import annotations + +from typing import TYPE_CHECKING + from conftest import run_pytest_codspeed_with_mode +if TYPE_CHECKING: + import pytest + from pytest_codspeed.config import ( BenchmarkMarkerOptions, CodSpeedConfig, PedanticOptions, ) -from pytest_codspeed.instruments.walltime import WallTimeInstrument from pytest_codspeed.instruments import MeasurementMode +from pytest_codspeed.instruments.walltime import WallTimeInstrument def test_bench_enabled_header_with_perf( @@ -95,7 +101,8 @@ def target(a, b, c): def test_benchmark_pedantic_walltime_setup_not_timed(monkeypatch: pytest.MonkeyPatch): - """Verify that the setup time is not included in the measurement when using pedantic mode with walltime.""" + """Verify that the setup time is not included in the measurement + when using pedantic mode with walltime.""" current_time_ns = 0 def fake_perf_counter_ns() -> int: