66
77from pytest_codspeed import __semver_version__
88from pytest_codspeed .instruments import Instrument
9- from pytest_codspeed .instruments .hooks import InstrumentHooks
9+ from pytest_codspeed .instruments .hooks import (
10+ FEATURE_DISABLE_CALLGRIND_MARKERS ,
11+ InstrumentHooks ,
12+ )
1013from pytest_codspeed .utils import SUPPORTS_PERF_TRAMPOLINE
1114
1215if TYPE_CHECKING :
1518 from pytest import Session
1619
1720 from pytest_codspeed .config import PedanticOptions
18- from pytest_codspeed .instruments import P , T
21+ from pytest_codspeed .instruments import MeasurementMode , P , T
1922 from pytest_codspeed .plugin import BenchmarkMarkerOptions , CodSpeedConfig
2023
2124
22- class ValgrindInstrument (Instrument ):
23- instrument = "valgrind "
25+ class AnalysisInstrument (Instrument ):
26+ instrument = "analysis "
2427 instrument_hooks : InstrumentHooks | None
28+ mode : MeasurementMode
2529
26- def __init__ (self , config : CodSpeedConfig ) -> None :
30+ def __init__ (self , config : CodSpeedConfig , mode : MeasurementMode ) -> None :
31+ self .mode = mode
2732 self .benchmark_count = 0
2833 try :
2934 self .instrument_hooks = InstrumentHooks ()
3035 self .instrument_hooks .set_integration ("pytest-codspeed" , __semver_version__ )
3136 except RuntimeError as e :
3237 if os .environ .get ("CODSPEED_ENV" ) is not None :
3338 raise Exception (
34- "Failed to initialize CPU simulation instrument hooks"
39+ f "Failed to initialize { self . mode . value } instrument hooks"
3540 ) from e
3641 self .instrument_hooks = None
3742
3843 self .should_measure = self .instrument_hooks is not None
3944
4045 def get_instrument_config_str_and_warns (self ) -> tuple [str , list [str ]]:
4146 config = (
42- f"mode: simulation , "
47+ f"mode: { self . mode . value } , "
4348 f"callgraph: { 'enabled' if SUPPORTS_PERF_TRAMPOLINE else 'not supported' } "
4449 )
4550 warnings = []
@@ -73,6 +78,9 @@ def __codspeed_root_frame__() -> T:
7378 # Warmup CPython performance map cache
7479 __codspeed_root_frame__ ()
7580
81+ self .instrument_hooks .set_feature (FEATURE_DISABLE_CALLGRIND_MARKERS , True )
82+ self .instrument_hooks .start_benchmark ()
83+
7684 # Manually call the library function to avoid an extra stack frame. Also
7785 # call the callgrind markers directly to avoid extra overhead.
7886 self .instrument_hooks .lib .callgrind_start_instrumentation ()
@@ -81,6 +89,7 @@ def __codspeed_root_frame__() -> T:
8189 finally :
8290 # Ensure instrumentation is stopped even if the test failed
8391 self .instrument_hooks .lib .callgrind_stop_instrumentation ()
92+ self .instrument_hooks .stop_benchmark ()
8493 self .instrument_hooks .set_executed_benchmark (uri )
8594
8695 def measure_pedantic (
@@ -92,8 +101,8 @@ def measure_pedantic(
92101 ) -> T :
93102 if pedantic_options .rounds != 1 or pedantic_options .iterations != 1 :
94103 warnings .warn (
95- "Valgrind instrument ignores rounds and iterations settings "
96- "in pedantic mode"
104+ f" { self . mode . value . capitalize () } instrument ignores rounds and "
105+ "iterations settings in pedantic mode"
97106 )
98107 if not self .instrument_hooks :
99108 args , kwargs = pedantic_options .setup_and_get_args_kwargs ()
@@ -117,11 +126,18 @@ def __codspeed_root_frame__(*args, **kwargs) -> T:
117126
118127 # Compute the actual result of the function
119128 args , kwargs = pedantic_options .setup_and_get_args_kwargs ()
129+
130+ self .instrument_hooks .set_feature (FEATURE_DISABLE_CALLGRIND_MARKERS , True )
131+ self .instrument_hooks .start_benchmark ()
132+
133+ # Manually call the library function to avoid an extra stack frame. Also
134+ # call the callgrind markers directly to avoid extra overhead.
120135 self .instrument_hooks .lib .callgrind_start_instrumentation ()
121136 try :
122137 out = __codspeed_root_frame__ (* args , ** kwargs )
123138 finally :
124139 self .instrument_hooks .lib .callgrind_stop_instrumentation ()
140+ self .instrument_hooks .stop_benchmark ()
125141 self .instrument_hooks .set_executed_benchmark (uri )
126142 if pedantic_options .teardown is not None :
127143 pedantic_options .teardown (* args , ** kwargs )
@@ -140,5 +156,5 @@ def report(self, session: Session) -> None:
140156 def get_result_dict (self ) -> dict [str , Any ]:
141157 return {
142158 "instrument" : {"type" : self .instrument },
143- # bench results will be dumped by valgrind
159+ # bench results will be dumped by the runner
144160 }
0 commit comments