11
11
from rich .table import Table
12
12
from rich .text import Text
13
13
14
+ from pytest_codspeed .benchmark import Benchmark
14
15
from pytest_codspeed .instruments import Instrument
15
16
16
17
if TYPE_CHECKING :
28
29
29
30
30
31
@dataclass
31
- class BenchmarkConfig :
32
+ class WalltimeBenchmarkConfig :
32
33
warmup_time_ns : int
33
34
min_round_time_ns : float
34
35
max_time_ns : int
35
36
max_rounds : int | None
36
37
37
38
@classmethod
38
- def from_codspeed_config (cls , config : CodSpeedConfig ) -> BenchmarkConfig :
39
+ def from_codspeed_config (cls , config : CodSpeedConfig ) -> WalltimeBenchmarkConfig :
39
40
return cls (
40
41
warmup_time_ns = config .warmup_time_ns
41
42
if config .warmup_time_ns is not None
@@ -49,7 +50,7 @@ def from_codspeed_config(cls, config: CodSpeedConfig) -> BenchmarkConfig:
49
50
50
51
51
52
@dataclass
52
- class BenchmarkStats :
53
+ class WalltimeBenchmarkStats :
53
54
min_ns : float
54
55
max_ns : float
55
56
mean_ns : float
@@ -75,7 +76,7 @@ def from_list(
75
76
iter_per_round : int ,
76
77
warmup_iters : int ,
77
78
total_time : float ,
78
- ) -> BenchmarkStats :
79
+ ) -> WalltimeBenchmarkStats :
79
80
stdev_ns = stdev (times_ns ) if len (times_ns ) > 1 else 0
80
81
mean_ns = mean (times_ns )
81
82
if len (times_ns ) > 1 :
@@ -114,17 +115,18 @@ def from_list(
114
115
115
116
116
117
@dataclass
117
- class Benchmark :
118
- name : str
119
- uri : str
120
-
121
- config : BenchmarkConfig
122
- stats : BenchmarkStats
118
+ class WalltimeBenchmark (Benchmark ):
119
+ config : WalltimeBenchmarkConfig
120
+ stats : WalltimeBenchmarkStats
123
121
124
122
125
123
def run_benchmark (
126
- name : str , uri : str , fn : Callable [P , T ], args , kwargs , config : BenchmarkConfig
127
- ) -> tuple [Benchmark , T ]:
124
+ benchmark : Benchmark ,
125
+ fn : Callable [P , T ],
126
+ args ,
127
+ kwargs ,
128
+ config : WalltimeBenchmarkConfig ,
129
+ ) -> tuple [WalltimeBenchmark , T ]:
128
130
# Compute the actual result of the function
129
131
out = fn (* args , ** kwargs )
130
132
@@ -171,42 +173,44 @@ def run_benchmark(
171
173
benchmark_end = perf_counter_ns ()
172
174
total_time = (benchmark_end - run_start ) / 1e9
173
175
174
- stats = BenchmarkStats .from_list (
176
+ stats = WalltimeBenchmarkStats .from_list (
175
177
times_ns ,
176
178
rounds = rounds ,
177
179
total_time = total_time ,
178
180
iter_per_round = iter_per_round ,
179
181
warmup_iters = warmup_iters ,
180
182
)
181
183
182
- return Benchmark (name = name , uri = uri , config = config , stats = stats ), out
184
+ return WalltimeBenchmark (
185
+ ** asdict (benchmark ),
186
+ config = config ,
187
+ stats = stats ,
188
+ ), out
183
189
184
190
185
191
class WallTimeInstrument (Instrument ):
186
192
instrument = "walltime"
187
193
188
194
def __init__ (self , config : CodSpeedConfig ) -> None :
189
195
self .config = config
190
- self .benchmarks : list [Benchmark ] = []
196
+ self .benchmarks : list [WalltimeBenchmark ] = []
191
197
192
198
def get_instrument_config_str_and_warns (self ) -> tuple [str , list [str ]]:
193
199
return f"mode: walltime, timer_resolution: { TIMER_RESOLUTION_NS :.1f} ns" , []
194
200
195
201
def measure (
196
202
self ,
197
- name : str ,
198
- uri : str ,
203
+ benchmark : Benchmark ,
199
204
fn : Callable [P , T ],
200
205
* args : P .args ,
201
206
** kwargs : P .kwargs ,
202
207
) -> T :
203
208
bench , out = run_benchmark (
204
- name = name ,
205
- uri = uri ,
209
+ benchmark = benchmark ,
206
210
fn = fn ,
207
211
args = args ,
208
212
kwargs = kwargs ,
209
- config = BenchmarkConfig .from_codspeed_config (self .config ),
213
+ config = WalltimeBenchmarkConfig .from_codspeed_config (self .config ),
210
214
)
211
215
self .benchmarks .append (bench )
212
216
return out
@@ -244,7 +248,7 @@ def _print_benchmark_table(self) -> None:
244
248
if rsd > 0.1 :
245
249
rsd_text .stylize ("red bold" )
246
250
table .add_row (
247
- escape (bench .name ),
251
+ escape (bench .display_name ),
248
252
f"{ bench .stats .min_ns / bench .stats .iter_per_round :,.0f} ns" ,
249
253
rsd_text ,
250
254
f"{ bench .stats .total_time :,.2f} s" ,
0 commit comments