1111from rich .table import Table
1212from rich .text import Text
1313
14+ from pytest_codspeed .benchmark import Benchmark
1415from pytest_codspeed .instruments import Instrument
1516
1617if TYPE_CHECKING :
2829
2930
3031@dataclass
31- class BenchmarkConfig :
32+ class WalltimeBenchmarkConfig :
3233 warmup_time_ns : int
3334 min_round_time_ns : float
3435 max_time_ns : int
3536 max_rounds : int | None
3637
3738 @classmethod
38- def from_codspeed_config (cls , config : CodSpeedConfig ) -> BenchmarkConfig :
39+ def from_codspeed_config (cls , config : CodSpeedConfig ) -> WalltimeBenchmarkConfig :
3940 return cls (
4041 warmup_time_ns = config .warmup_time_ns
4142 if config .warmup_time_ns is not None
@@ -49,7 +50,7 @@ def from_codspeed_config(cls, config: CodSpeedConfig) -> BenchmarkConfig:
4950
5051
5152@dataclass
52- class BenchmarkStats :
53+ class WalltimeBenchmarkStats :
5354 min_ns : float
5455 max_ns : float
5556 mean_ns : float
@@ -75,7 +76,7 @@ def from_list(
7576 iter_per_round : int ,
7677 warmup_iters : int ,
7778 total_time : float ,
78- ) -> BenchmarkStats :
79+ ) -> WalltimeBenchmarkStats :
7980 stdev_ns = stdev (times_ns ) if len (times_ns ) > 1 else 0
8081 mean_ns = mean (times_ns )
8182 if len (times_ns ) > 1 :
@@ -114,17 +115,18 @@ def from_list(
114115
115116
116117@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
123121
124122
125123def 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 ]:
128130 # Compute the actual result of the function
129131 out = fn (* args , ** kwargs )
130132
@@ -171,42 +173,44 @@ def run_benchmark(
171173 benchmark_end = perf_counter_ns ()
172174 total_time = (benchmark_end - run_start ) / 1e9
173175
174- stats = BenchmarkStats .from_list (
176+ stats = WalltimeBenchmarkStats .from_list (
175177 times_ns ,
176178 rounds = rounds ,
177179 total_time = total_time ,
178180 iter_per_round = iter_per_round ,
179181 warmup_iters = warmup_iters ,
180182 )
181183
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
183189
184190
185191class WallTimeInstrument (Instrument ):
186192 instrument = "walltime"
187193
188194 def __init__ (self , config : CodSpeedConfig ) -> None :
189195 self .config = config
190- self .benchmarks : list [Benchmark ] = []
196+ self .benchmarks : list [WalltimeBenchmark ] = []
191197
192198 def get_instrument_config_str_and_warns (self ) -> tuple [str , list [str ]]:
193199 return f"mode: walltime, timer_resolution: { TIMER_RESOLUTION_NS :.1f} ns" , []
194200
195201 def measure (
196202 self ,
197- name : str ,
198- uri : str ,
203+ benchmark : Benchmark ,
199204 fn : Callable [P , T ],
200205 * args : P .args ,
201206 ** kwargs : P .kwargs ,
202207 ) -> T :
203208 bench , out = run_benchmark (
204- name = name ,
205- uri = uri ,
209+ benchmark = benchmark ,
206210 fn = fn ,
207211 args = args ,
208212 kwargs = kwargs ,
209- config = BenchmarkConfig .from_codspeed_config (self .config ),
213+ config = WalltimeBenchmarkConfig .from_codspeed_config (self .config ),
210214 )
211215 self .benchmarks .append (bench )
212216 return out
@@ -244,7 +248,7 @@ def _print_benchmark_table(self) -> None:
244248 if rsd > 0.1 :
245249 rsd_text .stylize ("red bold" )
246250 table .add_row (
247- escape (bench .name ),
251+ escape (bench .display_name ),
248252 f"{ bench .stats .min_ns / bench .stats .iter_per_round :,.0f} ns" ,
249253 rsd_text ,
250254 f"{ bench .stats .total_time :,.2f} s" ,
0 commit comments