From 7550acf5e6e2a96b32bb76b20b033f13c10a1f50 Mon Sep 17 00:00:00 2001 From: Bobronium Date: Fri, 3 Apr 2026 20:30:45 +0400 Subject: [PATCH] Improve codspeed cases naming --- tests/conftest.py | 22 ++++++++++++++++- tests/test_performance.py | 51 ++++++++++++++++++++------------------- 2 files changed, 47 insertions(+), 26 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 7e6dec3..9dd4964 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -12,8 +12,10 @@ import sys import textwrap from pathlib import Path +from types import FunctionType from types import MappingProxyType from typing import TYPE_CHECKING +from typing import Any from typing import Final import pytest @@ -25,7 +27,9 @@ from datamodelzoo import EVIL_CASES if TYPE_CHECKING: - from types import FunctionType + from _pytest.nodes import Collector + +TEST_PERFORMANCE_FILENAME = "test_performance.py" COPIUM_ENV: Final = MappingProxyType( dict.fromkeys( @@ -297,3 +301,19 @@ def pytest_pyfunc_call(pyfuncitem): # We handled execution ourselves; pytest must not call the test function again. return True + + +def pytest_pycollect_makeitem(collector: Collector, name: str, obj: Any): + if collector.path.name != TEST_PERFORMANCE_FILENAME or not isinstance(obj, FunctionType): + return None + + if obj.__name__.startswith("test_"): + raise NameError( + f"{TEST_PERFORMANCE_FILENAME} must contain only benchmark functions: " + f"remove 'test_' prefix from {name!r}." + ) + + if "benchmark" in obj.__code__.co_varnames: + return list(collector._genfunctions(name, obj)) + + return None diff --git a/tests/test_performance.py b/tests/test_performance.py index bab0cf3..98fc7d3 100644 --- a/tests/test_performance.py +++ b/tests/test_performance.py @@ -267,7 +267,7 @@ def __deepcopy__(self, memo): return CustomDeepcopyObject(stdlib_copy.deepcopy(self.v, memo)) -REDUCE_CASES = chain( +GENERIC_CASES = chain( scaled( "dataclass_simple", lambda n: [SimpleDataclass(i, f"v{i}") for i in range(n)], @@ -342,7 +342,7 @@ def wide_dict(n): # ═══════════════════════════════════════════════════════════ -# REAL-WORLD +# Sample data # # Representative production deepcopy patterns. # Data is self-contained and deterministic. @@ -520,7 +520,7 @@ def make_orm_graph(): ] -REAL_WORLD_CASES = [ +SAMPLE_CASES = [ Case("json_api_response", make_json_api_response()), Case("config_shared_defaults", make_config_with_shared_defaults()), Case("openapi_schema", make_openapi_fragment()), @@ -532,65 +532,66 @@ def make_orm_graph(): # ═══════════════════════════════════════════════════════════ -# TESTS +# BENCHMARKS # ═══════════════════════════════════════════════════════════ -@generate_params(MEMO_CASES) @PYTHON_VERSION -def test_memo(case: Case, _python, benchmark): +@generate_params(MEMO_CASES) +def memo(case: Case, _python, benchmark): benchmark(copium.deepcopy, case.obj) -@generate_params(CONTAINER_CASES) @PYTHON_VERSION -def test_container(case: Case, _python, benchmark): +@generate_params(CONTAINER_CASES) +def container(case: Case, _python, benchmark): benchmark(copium.deepcopy, case.obj) -@generate_params(DEPTH_CASES) @PYTHON_VERSION -def test_depth(case: Case, _python, benchmark): +@generate_params(DEPTH_CASES) +def depth(case: Case, _python, benchmark): benchmark(copium.deepcopy, case.obj) -@generate_params(ATOMIC_CASES) @PYTHON_VERSION -def test_atomic(case: Case, _python, benchmark): +@generate_params(ATOMIC_CASES) +def atomic(case: Case, _python, benchmark): benchmark(copium.deepcopy, case.obj) -@generate_params(REDUCE_CASES) @PYTHON_VERSION -def test_reduce(case: Case, _python, benchmark): +@generate_params(GENERIC_CASES) +def generic(case: Case, _python, benchmark): benchmark(copium.deepcopy, case.obj) -@generate_params(EDGE_CASES) @PYTHON_VERSION -def test_edge(case: Case, _python, benchmark): +@generate_params(EDGE_CASES) +def edge_cases(case: Case, _python, benchmark): benchmark(copium.deepcopy, case.obj) -@generate_params(REAL_WORLD_CASES) @PYTHON_VERSION -def test_real(case: Case, _python, benchmark): +@generate_params(SAMPLE_CASES) +def sample_data(case: Case, _python, benchmark): benchmark(copium.deepcopy, case.obj) -@generate_params(REAL_WORLD_CASES) @PYTHON_VERSION -def test_real_dict_memo(case: Case, _python, benchmark): - benchmark(copium.deepcopy, case.obj, {}) +@generate_params(SAMPLE_CASES) +def dict_memo_sample_data(case: Case, _python, benchmark): + copium.config.apply(memo="dict") + benchmark(copium.deepcopy, case.obj) -@generate_params(REAL_WORLD_CASES) @PYTHON_VERSION -def test_real_stdlib_patched(case: Case, _python, benchmark, copium_patch_enabled): +@generate_params(SAMPLE_CASES) +def stdlib_sample_data(case: Case, _python, benchmark): benchmark(stdlib_copy.deepcopy, case.obj) -@generate_params(REAL_WORLD_CASES) @PYTHON_VERSION -def test_real_stdlib(case: Case, _python, benchmark): +@generate_params(SAMPLE_CASES) +def patched_stdlib_sample_data(case: Case, _python, benchmark, copium_patch_enabled): benchmark(stdlib_copy.deepcopy, case.obj)