11from __future__ import annotations
22
3+ import sys
34from dataclasses import asdict , dataclass
45from math import ceil
56from statistics import mean , quantiles , stdev
1112from rich .table import Table
1213from rich .text import Text
1314
15+ from pytest_codspeed import __semver_version__
1416from pytest_codspeed .instruments import Instrument
17+ from pytest_codspeed .instruments .hooks import InstrumentHooks
1518
1619if TYPE_CHECKING :
1720 from typing import Any , Callable
@@ -133,15 +136,18 @@ class Benchmark:
133136def run_benchmark (
134137 name : str , uri : str , fn : Callable [P , T ], args , kwargs , config : BenchmarkConfig
135138) -> tuple [Benchmark , T ]:
139+ def __codspeed_root_frame__ () -> T :
140+ return fn (* args , ** kwargs )
141+
136142 # Compute the actual result of the function
137- out = fn ( * args , ** kwargs )
143+ out = __codspeed_root_frame__ ( )
138144
139145 # Warmup
140146 times_per_round_ns : list [float ] = []
141147 warmup_start = start = perf_counter_ns ()
142148 while True :
143149 start = perf_counter_ns ()
144- fn ( * args , ** kwargs )
150+ __codspeed_root_frame__ ( )
145151 end = perf_counter_ns ()
146152 times_per_round_ns .append (end - start )
147153 if end - warmup_start > config .warmup_time_ns :
@@ -166,16 +172,19 @@ def run_benchmark(
166172 # Benchmark
167173 iter_range = range (iter_per_round )
168174 run_start = perf_counter_ns ()
175+ InstrumentHooks .start_benchmark ()
169176 for _ in range (rounds ):
170177 start = perf_counter_ns ()
171178 for _ in iter_range :
172- fn ( * args , ** kwargs )
179+ __codspeed_root_frame__ ( )
173180 end = perf_counter_ns ()
174181 times_per_round_ns .append (end - start )
175182
176183 if end - run_start > config .max_time_ns :
177184 # TODO: log something
178185 break
186+ InstrumentHooks .stop_benchmark ()
187+ InstrumentHooks .set_current_benchmark (uri )
179188 benchmark_end = perf_counter_ns ()
180189 total_time = (benchmark_end - run_start ) / 1e9
181190
@@ -196,6 +205,8 @@ class WallTimeInstrument(Instrument):
196205 def __init__ (self , config : CodSpeedConfig ) -> None :
197206 self .config = config
198207 self .benchmarks : list [Benchmark ] = []
208+ sys .activate_stack_trampoline ("perf" ) # type: ignore
209+ InstrumentHooks .set_integration ("pytest-codspeed" , __semver_version__ )
199210
200211 def get_instrument_config_str_and_warns (self ) -> tuple [str , list [str ]]:
201212 return f"mode: walltime, timer_resolution: { TIMER_RESOLUTION_NS :.1f} ns" , []
0 commit comments