Skip to content

Commit 3181f6d

Browse files
committed
chore: improve reliability of perf trampoline compatibility checks
1 parent e49de52 commit 3181f6d

File tree

6 files changed

+25
-16
lines changed

6 files changed

+25
-16
lines changed

src/pytest_codspeed/instruments/hooks/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
import warnings
66
from typing import TYPE_CHECKING
77

8+
from pytest_codspeed.utils import SUPPORTS_PERF_TRAMPOLINE
9+
810
if TYPE_CHECKING:
911
from .dist_instrument_hooks import InstrumentHooksPointer, LibType
1012

11-
SUPPORTS_PERF_TRAMPOLINE = sys.version_info >= (3, 12)
12-
1313

1414
class InstrumentHooks:
1515
"""Zig library wrapper class providing benchmark measurement functionality."""

src/pytest_codspeed/instruments/valgrind.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from __future__ import annotations
22

3-
import sys
43
import warnings
54
from typing import TYPE_CHECKING
65

76
from pytest_codspeed import __semver_version__
87
from pytest_codspeed.instruments import Instrument
98
from pytest_codspeed.instruments.hooks import InstrumentHooks
9+
from pytest_codspeed.utils import SUPPORTS_PERF_TRAMPOLINE
1010

1111
if TYPE_CHECKING:
1212
from typing import Any, Callable
@@ -17,8 +17,6 @@
1717
from pytest_codspeed.instruments import T
1818
from pytest_codspeed.plugin import BenchmarkMarkerOptions, CodSpeedConfig
1919

20-
SUPPORTS_PERF_TRAMPOLINE = sys.version_info >= (3, 12)
21-
2220

2321
class ValgrindInstrument(Instrument):
2422
instrument = "valgrind"

src/pytest_codspeed/instruments/walltime.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from pytest_codspeed import __semver_version__
1717
from pytest_codspeed.instruments import Instrument
1818
from pytest_codspeed.instruments.hooks import InstrumentHooks
19+
from pytest_codspeed.utils import SUPPORTS_PERF_TRAMPOLINE
1920

2021
if TYPE_CHECKING:
2122
from typing import Any, Callable
@@ -173,7 +174,13 @@ def __init__(self, config: CodSpeedConfig) -> None:
173174
self.benchmarks: list[Benchmark] = []
174175

175176
def get_instrument_config_str_and_warns(self) -> tuple[str, list[str]]:
176-
return f"mode: walltime, timer_resolution: {TIMER_RESOLUTION_NS:.1f}ns", []
177+
config_str = (
178+
f"mode: walltime, "
179+
f"callgraph: "
180+
f"{'enabled' if SUPPORTS_PERF_TRAMPOLINE else 'not supported'}, "
181+
f"timer_resolution: {TIMER_RESOLUTION_NS:.1f}ns"
182+
)
183+
return config_str, []
177184

178185
def measure(
179186
self,

src/pytest_codspeed/plugin.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import functools
44
import gc
5-
import importlib.util
65
import json
76
import os
87
import random
@@ -19,11 +18,11 @@
1918
CodSpeedConfig,
2019
PedanticOptions,
2120
)
22-
from pytest_codspeed.instruments import (
23-
MeasurementMode,
24-
get_instrument_from_mode,
25-
)
21+
from pytest_codspeed.instruments import MeasurementMode, get_instrument_from_mode
2622
from pytest_codspeed.utils import (
23+
BEFORE_PYTEST_8_1_1,
24+
IS_PYTEST_BENCHMARK_INSTALLED,
25+
IS_PYTEST_SPEED_INSTALLED,
2726
get_environment_metadata,
2827
get_git_relative_uri_and_name,
2928
)
@@ -37,10 +36,6 @@
3736

3837
T = TypeVar("T")
3938

40-
IS_PYTEST_BENCHMARK_INSTALLED = importlib.util.find_spec("pytest_benchmark") is not None
41-
IS_PYTEST_SPEED_INSTALLED = importlib.util.find_spec("pytest_speed") is not None
42-
BEFORE_PYTEST_8_1_1 = pytest.version_tuple < (8, 1, 1)
43-
4439

4540
@pytest.hookimpl(trylast=True)
4641
def pytest_addoption(parser: pytest.Parser):

src/pytest_codspeed/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
from __future__ import annotations
22

3+
import importlib.util
34
import os
45
import sys
56
import sysconfig
67
from pathlib import Path
78

9+
import pytest
10+
811
from pytest_codspeed import __semver_version__
912

1013
if sys.version_info < (3, 10):
@@ -13,6 +16,12 @@
1316
import importlib.metadata as importlib_metadata
1417

1518

19+
IS_PYTEST_BENCHMARK_INSTALLED = importlib.util.find_spec("pytest_benchmark") is not None
20+
IS_PYTEST_SPEED_INSTALLED = importlib.util.find_spec("pytest_speed") is not None
21+
BEFORE_PYTEST_8_1_1 = pytest.version_tuple < (8, 1, 1)
22+
SUPPORTS_PERF_TRAMPOLINE = sysconfig.get_config_var("PY_HAVE_PERF_TRAMPOLINE") == 1
23+
24+
1625
def get_git_relative_path(abs_path: Path) -> Path:
1726
"""Get the path relative to the git root directory. If the path is not
1827
inside a git repository, the original path itself is returned.

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
import pytest
1111

1212
from pytest_codspeed.instruments import MeasurementMode
13+
from pytest_codspeed.utils import IS_PYTEST_BENCHMARK_INSTALLED
1314

1415
if TYPE_CHECKING:
1516
from _pytest.pytester import RunResult
1617

1718
pytest_plugins = ["pytester"]
1819

19-
IS_PYTEST_BENCHMARK_INSTALLED = importlib.util.find_spec("pytest_benchmark") is not None
2020
skip_without_pytest_benchmark = pytest.mark.skipif(
2121
not IS_PYTEST_BENCHMARK_INSTALLED, reason="pytest_benchmark not installed"
2222
)

0 commit comments

Comments
 (0)