1111from rich .table import Table
1212from rich .text import Text
1313
14+ from pytest_codspeed import __semver_version__
1415from pytest_codspeed .instruments import Instrument
16+ from pytest_codspeed .instruments .hooks import InstrumentHooks
1517
1618if TYPE_CHECKING :
1719 from typing import Any , Callable
@@ -131,17 +133,26 @@ class Benchmark:
131133
132134
133135def run_benchmark (
134- name : str , uri : str , fn : Callable [P , T ], args , kwargs , config : BenchmarkConfig
136+ instrument_hooks : InstrumentHooks ,
137+ name : str ,
138+ uri : str ,
139+ fn : Callable [P , T ],
140+ args ,
141+ kwargs ,
142+ config : BenchmarkConfig ,
135143) -> tuple [Benchmark , T ]:
144+ def __codspeed_root_frame__ () -> T :
145+ return fn (* args , ** kwargs )
146+
136147 # Compute the actual result of the function
137- out = fn ( * args , ** kwargs )
148+ out = __codspeed_root_frame__ ( )
138149
139150 # Warmup
140151 times_per_round_ns : list [float ] = []
141152 warmup_start = start = perf_counter_ns ()
142153 while True :
143154 start = perf_counter_ns ()
144- fn ( * args , ** kwargs )
155+ __codspeed_root_frame__ ( )
145156 end = perf_counter_ns ()
146157 times_per_round_ns .append (end - start )
147158 if end - warmup_start > config .warmup_time_ns :
@@ -166,16 +177,19 @@ def run_benchmark(
166177 # Benchmark
167178 iter_range = range (iter_per_round )
168179 run_start = perf_counter_ns ()
180+ instrument_hooks .start_benchmark ()
169181 for _ in range (rounds ):
170182 start = perf_counter_ns ()
171183 for _ in iter_range :
172- fn ( * args , ** kwargs )
184+ __codspeed_root_frame__ ( )
173185 end = perf_counter_ns ()
174186 times_per_round_ns .append (end - start )
175187
176188 if end - run_start > config .max_time_ns :
177189 # TODO: log something
178190 break
191+ instrument_hooks .stop_benchmark ()
192+ instrument_hooks .set_current_benchmark (uri )
179193 benchmark_end = perf_counter_ns ()
180194 total_time = (benchmark_end - run_start ) / 1e9
181195
@@ -192,8 +206,12 @@ def run_benchmark(
192206
193207class WallTimeInstrument (Instrument ):
194208 instrument = "walltime"
209+ instrument_hooks : InstrumentHooks
195210
196211 def __init__ (self , config : CodSpeedConfig ) -> None :
212+ self .instrument_hooks = InstrumentHooks ()
213+ self .instrument_hooks .set_integration ("pytest-codspeed" , __semver_version__ )
214+
197215 self .config = config
198216 self .benchmarks : list [Benchmark ] = []
199217
@@ -209,6 +227,7 @@ def measure(
209227 ** kwargs : P .kwargs ,
210228 ) -> T :
211229 bench , out = run_benchmark (
230+ instrument_hooks = self .instrument_hooks ,
212231 name = name ,
213232 uri = uri ,
214233 fn = fn ,
0 commit comments