1
1
from __future__ import annotations
2
2
3
+ import sys
3
4
from dataclasses import asdict , dataclass
4
5
from math import ceil
5
6
from statistics import mean , quantiles , stdev
11
12
from rich .table import Table
12
13
from rich .text import Text
13
14
15
+ from pytest_codspeed import __semver_version__
14
16
from pytest_codspeed .instruments import Instrument
17
+ from pytest_codspeed .instruments .hooks import InstrumentHooks
15
18
16
19
if TYPE_CHECKING :
17
20
from typing import Any , Callable
@@ -133,15 +136,18 @@ class Benchmark:
133
136
def run_benchmark (
134
137
name : str , uri : str , fn : Callable [P , T ], args , kwargs , config : BenchmarkConfig
135
138
) -> tuple [Benchmark , T ]:
139
+ def __codspeed_root_frame__ () -> T :
140
+ return fn (* args , ** kwargs )
141
+
136
142
# Compute the actual result of the function
137
- out = fn ( * args , ** kwargs )
143
+ out = __codspeed_root_frame__ ( )
138
144
139
145
# Warmup
140
146
times_per_round_ns : list [float ] = []
141
147
warmup_start = start = perf_counter_ns ()
142
148
while True :
143
149
start = perf_counter_ns ()
144
- fn ( * args , ** kwargs )
150
+ __codspeed_root_frame__ ( )
145
151
end = perf_counter_ns ()
146
152
times_per_round_ns .append (end - start )
147
153
if end - warmup_start > config .warmup_time_ns :
@@ -166,16 +172,19 @@ def run_benchmark(
166
172
# Benchmark
167
173
iter_range = range (iter_per_round )
168
174
run_start = perf_counter_ns ()
175
+ InstrumentHooks .start_benchmark ()
169
176
for _ in range (rounds ):
170
177
start = perf_counter_ns ()
171
178
for _ in iter_range :
172
- fn ( * args , ** kwargs )
179
+ __codspeed_root_frame__ ( )
173
180
end = perf_counter_ns ()
174
181
times_per_round_ns .append (end - start )
175
182
176
183
if end - run_start > config .max_time_ns :
177
184
# TODO: log something
178
185
break
186
+ InstrumentHooks .stop_benchmark ()
187
+ InstrumentHooks .set_current_benchmark (uri )
179
188
benchmark_end = perf_counter_ns ()
180
189
total_time = (benchmark_end - run_start ) / 1e9
181
190
@@ -196,6 +205,8 @@ class WallTimeInstrument(Instrument):
196
205
def __init__ (self , config : CodSpeedConfig ) -> None :
197
206
self .config = config
198
207
self .benchmarks : list [Benchmark ] = []
208
+ sys .activate_stack_trampoline ("perf" ) # type: ignore
209
+ InstrumentHooks .set_integration ("pytest-codspeed" , __semver_version__ )
199
210
200
211
def get_instrument_config_str_and_warns (self ) -> tuple [str , list [str ]]:
201
212
return f"mode: walltime, timer_resolution: { TIMER_RESOLUTION_NS :.1f} ns" , []
0 commit comments