1414import pytest
1515from _pytest .fixtures import FixtureManager
1616
17+ from pytest_codspeed .config import BenchmarkMarkerOptions , CodSpeedConfig
1718from pytest_codspeed .instruments import (
1819 MeasurementMode ,
1920 get_instrument_from_mode ,
@@ -58,8 +59,7 @@ def pytest_addoption(parser: pytest.Parser):
5859 action = "store" ,
5960 type = float ,
6061 help = (
61- "The time to warm up the benchmark for (in seconds), "
62- "only for walltime mode"
62+ "The time to warm up the benchmark for (in seconds), only for walltime mode"
6363 ),
6464 )
6565 group .addoption (
@@ -82,27 +82,6 @@ def pytest_addoption(parser: pytest.Parser):
8282 )
8383
8484
85- @dataclass (frozen = True )
86- class CodSpeedConfig :
87- warmup_time_ns : int | None = None
88- max_time_ns : int | None = None
89- max_rounds : int | None = None
90-
91- @classmethod
92- def from_pytest_config (cls , config : pytest .Config ) -> CodSpeedConfig :
93- warmup_time = config .getoption ("--codspeed-warmup-time" , None )
94- warmup_time_ns = (
95- int (warmup_time * 1_000_000_000 ) if warmup_time is not None else None
96- )
97- max_time = config .getoption ("--codspeed-max-time" , None )
98- max_time_ns = int (max_time * 1_000_000_000 ) if max_time is not None else None
99- return cls (
100- warmup_time_ns = warmup_time_ns ,
101- max_rounds = config .getoption ("--codspeed-max-rounds" , None ),
102- max_time_ns = max_time_ns ,
103- )
104-
105-
10685@dataclass (unsafe_hash = True )
10786class CodSpeedPlugin :
10887 is_codspeed_enabled : bool
@@ -254,20 +233,21 @@ def pytest_collection_modifyitems(
254233
255234def _measure (
256235 plugin : CodSpeedPlugin ,
257- nodeid : str ,
236+ node : pytest . Item ,
258237 config : pytest .Config ,
259238 fn : Callable [P , T ],
260239 * args : P .args ,
261240 ** kwargs : P .kwargs ,
262241) -> T :
242+ marker_options = BenchmarkMarkerOptions .from_pytest_item (node )
263243 random .seed (0 )
264244 is_gc_enabled = gc .isenabled ()
265245 if is_gc_enabled :
266246 gc .collect ()
267247 gc .disable ()
268248 try :
269- uri , name = get_git_relative_uri_and_name (nodeid , config .rootpath )
270- return plugin .instrument .measure (name , uri , fn , * args , ** kwargs )
249+ uri , name = get_git_relative_uri_and_name (node . nodeid , config .rootpath )
250+ return plugin .instrument .measure (marker_options , name , uri , fn , * args , ** kwargs )
271251 finally :
272252 # Ensure GC is re-enabled even if the test failed
273253 if is_gc_enabled :
@@ -276,13 +256,13 @@ def _measure(
276256
277257def wrap_runtest (
278258 plugin : CodSpeedPlugin ,
279- nodeid : str ,
259+ node : pytest . Item ,
280260 config : pytest .Config ,
281261 fn : Callable [P , T ],
282262) -> Callable [P , T ]:
283263 @functools .wraps (fn )
284264 def wrapped (* args : P .args , ** kwargs : P .kwargs ) -> T :
285- return _measure (plugin , nodeid , config , fn , * args , ** kwargs )
265+ return _measure (plugin , node , config , fn , * args , ** kwargs )
286266
287267 return wrapped
288268
@@ -299,7 +279,7 @@ def pytest_runtest_protocol(item: pytest.Item, nextitem: pytest.Item | None):
299279 return None
300280
301281 # Wrap runtest and defer to default protocol
302- item .runtest = wrap_runtest (plugin , item . nodeid , item .config , item .runtest )
282+ item .runtest = wrap_runtest (plugin , item , item .config , item .runtest )
303283 return None
304284
305285
@@ -343,9 +323,7 @@ def __call__(self, func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T
343323 config = self ._request .config
344324 plugin = get_plugin (config )
345325 if plugin .is_codspeed_enabled :
346- return _measure (
347- plugin , self ._request .node .nodeid , config , func , * args , ** kwargs
348- )
326+ return _measure (plugin , self ._request .node , config , func , * args , ** kwargs )
349327 else :
350328 return func (* args , ** kwargs )
351329
0 commit comments