|
1 | 1 | import pytest |
2 | 2 | from conftest import run_pytest_codspeed_with_mode |
3 | 3 |
|
| 4 | +from pytest_codspeed.config import BenchmarkMarkerOptions, CodSpeedConfig, PedanticOptions |
| 5 | +from pytest_codspeed.instruments.walltime import WallTimeInstrument |
4 | 6 | from pytest_codspeed.instruments import MeasurementMode |
5 | 7 |
|
6 | 8 |
|
@@ -86,3 +88,45 @@ def target(a, b, c): |
86 | 88 | result = run_pytest_codspeed_with_mode(pytester, MeasurementMode.WallTime) |
87 | 89 | assert result.ret == 0, "the run should have succeeded" |
88 | 90 | 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