Skip to content

Commit adab009

Browse files
committed
feat: use instrument hooks
1 parent 01f1b7b commit adab009

2 files changed

Lines changed: 23 additions & 18 deletions

File tree

src/pytest_codspeed/instruments/valgrind.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,31 @@
66

77
from pytest_codspeed import __semver_version__
88
from pytest_codspeed.instruments import Instrument
9-
from pytest_codspeed.instruments.valgrind._wrapper import get_lib
9+
from pytest_codspeed.instruments.hooks import InstrumentHooks
1010

1111
if TYPE_CHECKING:
1212
from typing import Any, Callable
1313

1414
from pytest import Session
1515

1616
from pytest_codspeed.instruments import P, T
17-
from pytest_codspeed.instruments.valgrind._wrapper import LibType
1817
from pytest_codspeed.plugin import CodSpeedConfig
1918

2019
SUPPORTS_PERF_TRAMPOLINE = sys.version_info >= (3, 12)
2120

2221

2322
class ValgrindInstrument(Instrument):
2423
instrument = "valgrind"
25-
lib: LibType | None
2624

2725
def __init__(self, config: CodSpeedConfig) -> None:
2826
self.benchmark_count = 0
2927
self.should_measure = os.environ.get("CODSPEED_ENV") is not None
3028
if self.should_measure:
31-
self.lib = get_lib()
32-
self.lib.dump_stats_at(
33-
f"Metadata: pytest-codspeed {__semver_version__}".encode("ascii")
34-
)
29+
InstrumentHooks.set_integration("pytest-codspeed", __semver_version__);
30+
3531
if SUPPORTS_PERF_TRAMPOLINE:
3632
sys.activate_stack_trampoline("perf") # type: ignore
37-
else:
38-
self.lib = None
33+
3934

4035
def get_instrument_config_str_and_warns(self) -> tuple[str, list[str]]:
4136
config = (
@@ -61,7 +56,8 @@ def measure(
6156
**kwargs: P.kwargs,
6257
) -> T:
6358
self.benchmark_count += 1
64-
if self.lib is None: # Thus should_measure is False
59+
60+
if not InstrumentHooks.is_instrumented(): # Thus should_measure is False
6561
return fn(*args, **kwargs)
6662

6763
def __codspeed_root_frame__() -> T:
@@ -71,14 +67,13 @@ def __codspeed_root_frame__() -> T:
7167
# Warmup CPython performance map cache
7268
__codspeed_root_frame__()
7369

74-
self.lib.zero_stats()
75-
self.lib.start_instrumentation()
70+
InstrumentHooks.start_benchmark()
7671
try:
7772
return __codspeed_root_frame__()
7873
finally:
7974
# Ensure instrumentation is stopped even if the test failed
80-
self.lib.stop_instrumentation()
81-
self.lib.dump_stats_at(uri.encode("ascii"))
75+
InstrumentHooks.stop_benchmark()
76+
InstrumentHooks.set_current_benchmark(uri)
8277

8378
def report(self, session: Session) -> None:
8479
reporter = session.config.pluginmanager.get_plugin("terminalreporter")

src/pytest_codspeed/instruments/walltime.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
from statistics import mean, quantiles, stdev
66
from time import get_clock_info, perf_counter_ns
77
from typing import TYPE_CHECKING
8+
import sys
89

910
from rich.console import Console
1011
from rich.markup import escape
1112
from rich.table import Table
1213
from rich.text import Text
13-
14+
from pytest_codspeed.instruments.hooks import InstrumentHooks
1415
from pytest_codspeed.instruments import Instrument
16+
from pytest_codspeed import __semver_version__
1517

1618
if TYPE_CHECKING:
1719
from typing import Any, Callable
@@ -133,15 +135,18 @@ class Benchmark:
133135
def run_benchmark(
134136
name: str, uri: str, fn: Callable[P, T], args, kwargs, config: BenchmarkConfig
135137
) -> tuple[Benchmark, T]:
138+
def __codspeed_root_frame__() -> T:
139+
return fn(*args, **kwargs)
140+
136141
# Compute the actual result of the function
137-
out = fn(*args, **kwargs)
142+
out = __codspeed_root_frame__()
138143

139144
# Warmup
140145
times_per_round_ns: list[float] = []
141146
warmup_start = start = perf_counter_ns()
142147
while True:
143148
start = perf_counter_ns()
144-
fn(*args, **kwargs)
149+
__codspeed_root_frame__()
145150
end = perf_counter_ns()
146151
times_per_round_ns.append(end - start)
147152
if end - warmup_start > config.warmup_time_ns:
@@ -166,16 +171,19 @@ def run_benchmark(
166171
# Benchmark
167172
iter_range = range(iter_per_round)
168173
run_start = perf_counter_ns()
174+
InstrumentHooks.start_benchmark()
169175
for _ in range(rounds):
170176
start = perf_counter_ns()
171177
for _ in iter_range:
172-
fn(*args, **kwargs)
178+
__codspeed_root_frame__()
173179
end = perf_counter_ns()
174180
times_per_round_ns.append(end - start)
175181

176182
if end - run_start > config.max_time_ns:
177183
# TODO: log something
178184
break
185+
InstrumentHooks.stop_benchmark()
186+
InstrumentHooks.set_current_benchmark(name)
179187
benchmark_end = perf_counter_ns()
180188
total_time = (benchmark_end - run_start) / 1e9
181189

@@ -196,6 +204,8 @@ class WallTimeInstrument(Instrument):
196204
def __init__(self, config: CodSpeedConfig) -> None:
197205
self.config = config
198206
self.benchmarks: list[Benchmark] = []
207+
sys.activate_stack_trampoline("perf") # type: ignore
208+
InstrumentHooks.set_integration("pytest-codspeed", __semver_version__);
199209

200210
def get_instrument_config_str_and_warns(self) -> tuple[str, list[str]]:
201211
return f"mode: walltime, timer_resolution: {TIMER_RESOLUTION_NS:.1f}ns", []

0 commit comments

Comments
 (0)