Skip to content

Commit

Permalink
refactor: differentiate the mode from the underlying instrument
Browse files Browse the repository at this point in the history
  • Loading branch information
art049 committed Sep 18, 2024
1 parent 0b3de7d commit f387418
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 20 deletions.
12 changes: 6 additions & 6 deletions src/pytest_codspeed/instruments/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


class Instrument(metaclass=ABCMeta):
instrument: ClassVar[MeasurementMode]
instrument: ClassVar[str]

@abstractmethod
def __init__(self, config: CodSpeedConfig): ...
Expand Down Expand Up @@ -44,17 +44,17 @@ def get_result_dict(


class MeasurementMode(str, Enum):
CPUInstrumentation = "cpu_instrumentation"
Instrumentation = "instrumentation"
WallTime = "walltime"


def get_instrument_from_mode(mode: MeasurementMode) -> type[Instrument]:
from pytest_codspeed.instruments.cpu_instrumentation import (
CPUInstrumentationInstrument,
from pytest_codspeed.instruments.valgrind import (
ValgrindInstrument,
)
from pytest_codspeed.instruments.walltime import WallTimeInstrument

if mode == MeasurementMode.CPUInstrumentation:
return CPUInstrumentationInstrument
if mode == MeasurementMode.Instrumentation:
return ValgrindInstrument
else:
return WallTimeInstrument
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@
from typing import TYPE_CHECKING

from pytest_codspeed import __version__
from pytest_codspeed.instruments import Instrument, MeasurementMode
from pytest_codspeed.instruments.cpu_instrumentation._wrapper import get_lib
from pytest_codspeed.instruments import Instrument
from pytest_codspeed.instruments.valgrind._wrapper import get_lib

if TYPE_CHECKING:
from typing import Any, Callable

from pytest import Session

from pytest_codspeed.instruments import P, T
from pytest_codspeed.instruments.cpu_instrumentation._wrapper import LibType
from pytest_codspeed.instruments.valgrind._wrapper import LibType
from pytest_codspeed.plugin import CodSpeedConfig

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


class CPUInstrumentationInstrument(Instrument):
instrument = MeasurementMode.CPUInstrumentation
class ValgrindInstrument(Instrument):
instrument = "valgrind"
lib: LibType | None

def __init__(self, config: CodSpeedConfig) -> None:
Expand Down Expand Up @@ -90,6 +90,6 @@ def report(self, session: Session) -> None:

def get_result_dict(self) -> dict[str, Any]:
return {
"instrument": {"type": self.instrument.value},
"instrument": {"type": self.instrument},
# bench results will be dumped by valgrind
}
6 changes: 3 additions & 3 deletions src/pytest_codspeed/instruments/walltime.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from rich.console import Console
from rich.table import Table

from pytest_codspeed.instruments import Instrument, MeasurementMode
from pytest_codspeed.instruments import Instrument

if TYPE_CHECKING:
from typing import Any, Callable
Expand Down Expand Up @@ -153,7 +153,7 @@ def run_benchmark(


class WallTimeInstrument(Instrument):
instrument = MeasurementMode.WallTime
instrument = "walltime"

def __init__(self, config: CodSpeedConfig) -> None:
self.config = config
Expand Down Expand Up @@ -224,7 +224,7 @@ def _print_benchmark_table(self) -> None:
def get_result_dict(self) -> dict[str, Any]:
return {
"instrument": {
"type": self.instrument.value,
"type": self.instrument,
"clock_info": get_clock_info("perf_counter").__dict__,
},
"benchmarks": [asdict(bench) for bench in self.benchmarks],
Expand Down
4 changes: 2 additions & 2 deletions src/pytest_codspeed/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def pytest_configure(config: pytest.Config):
if os.environ.get("CODSPEED_RUNNER_MODE") == "walltime":
default_mode = MeasurementMode.WallTime.value
else:
default_mode = MeasurementMode.CPUInstrumentation.value
default_mode = MeasurementMode.Instrumentation.value
else:
default_mode = MeasurementMode.WallTime.value

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

profile_folder = os.environ.get("CODSPEED_PROFILE_FOLDER")
if profile_folder:
result_path = Path(profile_folder) / "walltime" / f"{os.getpid()}.json"
result_path = Path(profile_folder) / "results" / f"{os.getpid()}.json"
else:
result_path = config.rootpath / f".codspeed/results_{time() * 1000:.0f}.json"

Expand Down
5 changes: 2 additions & 3 deletions tests/test_pytest_plugin_cpu_instrumentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def _():
return 1 + 1
"""
)
result = run_pytest_codspeed_with_mode(pytester, MeasurementMode.CPUInstrumentation)
result = run_pytest_codspeed_with_mode(pytester, MeasurementMode.Instrumentation)
result.stdout.fnmatch_lines(
[
(
Expand Down Expand Up @@ -76,8 +76,7 @@ def fixtured_child():
with open(perf_filepath) as perf_file:
lines = perf_file.readlines()
assert any(
"py::CPUInstrumentationInstrument.measure.<locals>.__codspeed_root_frame__"
in line
"py::ValgrindInstrument.measure.<locals>.__codspeed_root_frame__" in line
for line in lines
), "No root frame found in perf map"
assert any(
Expand Down

0 comments on commit f387418

Please sign in to comment.