55from statistics import mean , quantiles , stdev
66from time import get_clock_info , perf_counter_ns
77from typing import TYPE_CHECKING
8+ import sys
89
910from rich .console import Console
1011from rich .markup import escape
1112from rich .table import Table
1213from rich .text import Text
13-
14+ from pytest_codspeed . instruments . hooks import InstrumentHooks
1415from pytest_codspeed .instruments import Instrument
16+ from pytest_codspeed import __semver_version__
1517
1618if TYPE_CHECKING :
1719 from typing import Any , Callable
@@ -133,15 +135,18 @@ class Benchmark:
133135def 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