Skip to content

Commit f387418

Browse files
committed
refactor: differentiate the mode from the underlying instrument
1 parent 0b3de7d commit f387418

File tree

10 files changed

+19
-20
lines changed

10 files changed

+19
-20
lines changed

src/pytest_codspeed/instruments/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717

1818
class Instrument(metaclass=ABCMeta):
19-
instrument: ClassVar[MeasurementMode]
19+
instrument: ClassVar[str]
2020

2121
@abstractmethod
2222
def __init__(self, config: CodSpeedConfig): ...
@@ -44,17 +44,17 @@ def get_result_dict(
4444

4545

4646
class MeasurementMode(str, Enum):
47-
CPUInstrumentation = "cpu_instrumentation"
47+
Instrumentation = "instrumentation"
4848
WallTime = "walltime"
4949

5050

5151
def get_instrument_from_mode(mode: MeasurementMode) -> type[Instrument]:
52-
from pytest_codspeed.instruments.cpu_instrumentation import (
53-
CPUInstrumentationInstrument,
52+
from pytest_codspeed.instruments.valgrind import (
53+
ValgrindInstrument,
5454
)
5555
from pytest_codspeed.instruments.walltime import WallTimeInstrument
5656

57-
if mode == MeasurementMode.CPUInstrumentation:
58-
return CPUInstrumentationInstrument
57+
if mode == MeasurementMode.Instrumentation:
58+
return ValgrindInstrument
5959
else:
6060
return WallTimeInstrument

src/pytest_codspeed/instruments/cpu_instrumentation/__init__.py renamed to src/pytest_codspeed/instruments/valgrind/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@
55
from typing import TYPE_CHECKING
66

77
from pytest_codspeed import __version__
8-
from pytest_codspeed.instruments import Instrument, MeasurementMode
9-
from pytest_codspeed.instruments.cpu_instrumentation._wrapper import get_lib
8+
from pytest_codspeed.instruments import Instrument
9+
from pytest_codspeed.instruments.valgrind._wrapper import get_lib
1010

1111
if TYPE_CHECKING:
1212
from typing import Any, Callable
1313

1414
from pytest import Session
1515

1616
from pytest_codspeed.instruments import P, T
17-
from pytest_codspeed.instruments.cpu_instrumentation._wrapper import LibType
17+
from pytest_codspeed.instruments.valgrind._wrapper import LibType
1818
from pytest_codspeed.plugin import CodSpeedConfig
1919

2020
SUPPORTS_PERF_TRAMPOLINE = sys.version_info >= (3, 12)
2121

2222

23-
class CPUInstrumentationInstrument(Instrument):
24-
instrument = MeasurementMode.CPUInstrumentation
23+
class ValgrindInstrument(Instrument):
24+
instrument = "valgrind"
2525
lib: LibType | None
2626

2727
def __init__(self, config: CodSpeedConfig) -> None:
@@ -90,6 +90,6 @@ def report(self, session: Session) -> None:
9090

9191
def get_result_dict(self) -> dict[str, Any]:
9292
return {
93-
"instrument": {"type": self.instrument.value},
93+
"instrument": {"type": self.instrument},
9494
# bench results will be dumped by valgrind
9595
}

src/pytest_codspeed/instruments/walltime.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from rich.console import Console
1010
from rich.table import Table
1111

12-
from pytest_codspeed.instruments import Instrument, MeasurementMode
12+
from pytest_codspeed.instruments import Instrument
1313

1414
if TYPE_CHECKING:
1515
from typing import Any, Callable
@@ -153,7 +153,7 @@ def run_benchmark(
153153

154154

155155
class WallTimeInstrument(Instrument):
156-
instrument = MeasurementMode.WallTime
156+
instrument = "walltime"
157157

158158
def __init__(self, config: CodSpeedConfig) -> None:
159159
self.config = config
@@ -224,7 +224,7 @@ def _print_benchmark_table(self) -> None:
224224
def get_result_dict(self) -> dict[str, Any]:
225225
return {
226226
"instrument": {
227-
"type": self.instrument.value,
227+
"type": self.instrument,
228228
"clock_info": get_clock_info("perf_counter").__dict__,
229229
},
230230
"benchmarks": [asdict(bench) for bench in self.benchmarks],

src/pytest_codspeed/plugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def pytest_configure(config: pytest.Config):
123123
if os.environ.get("CODSPEED_RUNNER_MODE") == "walltime":
124124
default_mode = MeasurementMode.WallTime.value
125125
else:
126-
default_mode = MeasurementMode.CPUInstrumentation.value
126+
default_mode = MeasurementMode.Instrumentation.value
127127
else:
128128
default_mode = MeasurementMode.WallTime.value
129129

@@ -144,7 +144,7 @@ def pytest_configure(config: pytest.Config):
144144

145145
profile_folder = os.environ.get("CODSPEED_PROFILE_FOLDER")
146146
if profile_folder:
147-
result_path = Path(profile_folder) / "walltime" / f"{os.getpid()}.json"
147+
result_path = Path(profile_folder) / "results" / f"{os.getpid()}.json"
148148
else:
149149
result_path = config.rootpath / f".codspeed/results_{time() * 1000:.0f}.json"
150150

tests/test_pytest_plugin_cpu_instrumentation.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def _():
3636
return 1 + 1
3737
"""
3838
)
39-
result = run_pytest_codspeed_with_mode(pytester, MeasurementMode.CPUInstrumentation)
39+
result = run_pytest_codspeed_with_mode(pytester, MeasurementMode.Instrumentation)
4040
result.stdout.fnmatch_lines(
4141
[
4242
(
@@ -76,8 +76,7 @@ def fixtured_child():
7676
with open(perf_filepath) as perf_file:
7777
lines = perf_file.readlines()
7878
assert any(
79-
"py::CPUInstrumentationInstrument.measure.<locals>.__codspeed_root_frame__"
80-
in line
79+
"py::ValgrindInstrument.measure.<locals>.__codspeed_root_frame__" in line
8180
for line in lines
8281
), "No root frame found in perf map"
8382
assert any(

0 commit comments

Comments
 (0)