forked from CodSpeedHQ/pytest-codspeed
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pytest_plugin_walltime.py
More file actions
143 lines (119 loc) · 4.18 KB
/
test_pytest_plugin_walltime.py
File metadata and controls
143 lines (119 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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 import MeasurementMode
from pytest_codspeed.instruments.walltime import WallTimeInstrument
def test_bench_enabled_header_with_perf(
pytester: pytest.Pytester,
) -> None:
pytester.copy_example("tests/examples/test_addition_fixture.py")
result = run_pytest_codspeed_with_mode(pytester, MeasurementMode.WallTime)
result.stdout.fnmatch_lines(["*test_some_addition_performance*", "*1 benchmarked*"])
def test_parametrization_naming(
pytester: pytest.Pytester,
) -> None:
pytester.makepyfile(
"""
import time, pytest
@pytest.mark.parametrize("inp", ["toto", 12, 58.3])
def test_my_stuff(benchmark, inp):
benchmark(lambda: time.sleep(0.01))
"""
)
result = run_pytest_codspeed_with_mode(pytester, MeasurementMode.WallTime)
# Make sure the parametrization is not broken
print(result.outlines)
result.stdout.fnmatch_lines_random(
[
"*test_my_stuff[[]toto[]]*",
"*test_my_stuff[[]12[]]*",
"*test_my_stuff[[]58.3[]]*",
"*3 benchmarked*",
]
)
def test_benchmark_pedantic_walltime(
pytester: pytest.Pytester,
) -> None:
"""Test that pedantic mode works with walltime mode."""
pytester.makepyfile(
"""
def test_pedantic_full_features(benchmark):
setup_calls = 0
teardown_calls = 0
target_calls = 0
def setup():
nonlocal setup_calls
setup_calls += 1
return (1, 2), {"c": 3}
def teardown(a, b, c):
nonlocal teardown_calls
teardown_calls += 1
assert a == 1
assert b == 2
assert c == 3
def target(a, b, c):
nonlocal target_calls
target_calls += 1
assert a == 1
assert b == 2
assert c == 3
return a + b + c
result = benchmark.pedantic(
target,
setup=setup,
teardown=teardown,
rounds=3,
warmup_rounds=1
)
# Verify the results
assert result == 6 # 1 + 2 + 3
assert setup_calls == 5 # 3 rounds + 1 warmup + 1 calibration
assert teardown_calls == 5
assert target_calls == 5
"""
)
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