Skip to content

Commit 56108b5

Browse files
committed
need to fix precommit errors
1 parent 4de9323 commit 56108b5

File tree

2 files changed

+155
-1
lines changed

2 files changed

+155
-1
lines changed

code_to_optimize/tests/pytest/benchmarks/test_benchmark_bubble_sort.py

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,74 @@
1-
import pytest
1+
from typing import Callable, Any
22

3+
import pytest
34
from code_to_optimize.bubble_sort import sorter
45

56

7+
class DummyBenchmark:
8+
"""A dummy benchmark object that mimics pytest-benchmark's interface."""
9+
10+
def __init__(self):
11+
self.stats = {}
12+
13+
def __call__(self, func: Callable, *args, **kwargs) -> Any:
14+
"""Call the function and return its result without benchmarking."""
15+
return func(*args, **kwargs)
16+
17+
def pedantic(self, target: Callable, args: tuple = (), kwargs: dict = None,
18+
iterations: int = 1, rounds: int = 1, warmup_rounds: int = 0,
19+
setup: Callable = None) -> Any:
20+
"""Mimics the pedantic method of pytest-benchmark."""
21+
if setup:
22+
setup()
23+
if kwargs is None:
24+
kwargs = {}
25+
return target(*args, **kwargs)
26+
27+
@property
28+
def group(self):
29+
"""Return a dummy group object."""
30+
return type('Group', (), {'name': 'dummy'})()
31+
32+
@property
33+
def name(self):
34+
"""Return a dummy name."""
35+
return "dummy_benchmark"
36+
37+
@property
38+
def fullname(self):
39+
"""Return a dummy fullname."""
40+
return "dummy::benchmark"
41+
42+
@property
43+
def params(self):
44+
"""Return empty params."""
45+
return {}
46+
47+
@property
48+
def extra_info(self):
49+
"""Return empty extra info."""
50+
return {}
51+
52+
53+
@pytest.fixture
54+
def benchmark(request):
55+
"""
56+
Provide a benchmark fixture that works whether pytest-benchmark is installed or not.
57+
58+
When pytest-benchmark is disabled with '-p no:benchmark', this provides a dummy
59+
implementation that allows tests to run without modification.
60+
"""
61+
# Check if benchmark fixture is already available (pytest-benchmark is active)
62+
if 'benchmark' in request.fixturenames and hasattr(request, '_fixturemanager'):
63+
try:
64+
# Try to get the real benchmark fixture
65+
return request.getfixturevalue('benchmark')
66+
except (pytest.FixtureLookupError, AttributeError):
67+
pass
68+
69+
# Return dummy benchmark if real one is not available
70+
return DummyBenchmark()
71+
672
def test_sort(benchmark):
773
result = benchmark(sorter, list(reversed(range(500))))
874
assert result == list(range(500))
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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

Comments
 (0)