|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Any, Callable |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | + |
| 8 | +class CodeFlashBenchmarkDummyPlugin: |
| 9 | + @staticmethod |
| 10 | + def pytest_plugin_registered(plugin, manager) -> None: # noqa: ANN001 |
| 11 | + # Not necessary since run with -p no:benchmark, but just in case |
| 12 | + if hasattr(plugin, "name") and plugin.name == "pytest-benchmark": |
| 13 | + manager.unregister(plugin) |
| 14 | + |
| 15 | + @staticmethod |
| 16 | + def pytest_configure(config: pytest.Config) -> None: |
| 17 | + """Register the benchmark marker.""" |
| 18 | + config.addinivalue_line( |
| 19 | + "markers", |
| 20 | + "benchmark: mark test as a benchmark that should be run without modification if the benchmark fixture is disabled", |
| 21 | + ) |
| 22 | + |
| 23 | + # Benchmark fixture |
| 24 | + class DummyBenchmark: |
| 25 | + """A dummy benchmark object that mimics pytest-benchmark's interface.""" |
| 26 | + |
| 27 | + def __init__(self) -> None: |
| 28 | + self.stats = {} |
| 29 | + |
| 30 | + def __call__(self, func: Callable, *args: tuple[Any, ...], **kwargs: dict) -> Any: |
| 31 | + """Call the function and return its result without benchmarking.""" |
| 32 | + return func(*args, **kwargs) |
| 33 | + |
| 34 | + def pedantic( |
| 35 | + self, |
| 36 | + target: Callable, |
| 37 | + args: tuple = (), |
| 38 | + kwargs: dict = None, |
| 39 | + iterations: int = 1, |
| 40 | + rounds: int = 1, |
| 41 | + warmup_rounds: int = 0, |
| 42 | + setup: Callable = None, |
| 43 | + ) -> Any: |
| 44 | + """Mimics the pedantic method of pytest-benchmark.""" |
| 45 | + if setup: |
| 46 | + setup() |
| 47 | + if kwargs is None: |
| 48 | + kwargs = {} |
| 49 | + return target(*args, **kwargs) |
| 50 | + |
| 51 | + @property |
| 52 | + def group(self): |
| 53 | + """Return a dummy group object.""" |
| 54 | + return type("Group", (), {"name": "dummy"})() |
| 55 | + |
| 56 | + @property |
| 57 | + def name(self): |
| 58 | + """Return a dummy name.""" |
| 59 | + return "dummy_benchmark" |
| 60 | + |
| 61 | + @property |
| 62 | + def fullname(self): |
| 63 | + """Return a dummy fullname.""" |
| 64 | + return "dummy::benchmark" |
| 65 | + |
| 66 | + @property |
| 67 | + def params(self): |
| 68 | + """Return empty params.""" |
| 69 | + return {} |
| 70 | + |
| 71 | + @property |
| 72 | + def extra_info(self): |
| 73 | + """Return empty extra info.""" |
| 74 | + return {} |
| 75 | + |
| 76 | + @staticmethod |
| 77 | + @pytest.fixture |
| 78 | + def benchmark(request: pytest.FixtureRequest) -> object: |
| 79 | + # Check if benchmark fixture is already available (pytest-benchmark is active) |
| 80 | + if "benchmark" in request.fixturenames and hasattr(request, "_fixturemanager"): |
| 81 | + try: |
| 82 | + return request.getfixturevalue("benchmark") |
| 83 | + except (pytest.FixtureLookupError, AttributeError): |
| 84 | + pass |
| 85 | + return CodeFlashBenchmarkDummyPlugin.DummyBenchmark(request) |
| 86 | + |
| 87 | + |
| 88 | +codeflash_benchmark_plugin = CodeFlashBenchmarkDummyPlugin() |
0 commit comments