1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
"""codspeed benchmarks for propcache."""
import pytest
try:
from pytest_codspeed import BenchmarkFixture
except ImportError:
pytestmark = pytest.mark.skip("pytest_codspeed needs to be installed")
from propcache import cached_property, under_cached_property
def test_under_cached_property_cache_hit(benchmark: "BenchmarkFixture") -> None:
"""Benchmark for under_cached_property cache hit."""
class Test:
def __init__(self) -> None:
self._cache = {"prop": 42}
@under_cached_property
def prop(self) -> int:
"""Return the value of the property."""
raise NotImplementedError
t = Test()
@benchmark
def _run() -> None:
for _ in range(100):
t.prop
def test_cached_property_cache_hit(benchmark: "BenchmarkFixture") -> None:
"""Benchmark for cached_property cache hit."""
class Test:
def __init__(self) -> None:
self.__dict__["prop"] = 42
@cached_property
def prop(self) -> int:
"""Return the value of the property."""
raise NotImplementedError
t = Test()
@benchmark
def _run() -> None:
for _ in range(100):
t.prop
def test_under_cached_property_cache_miss(benchmark: "BenchmarkFixture") -> None:
"""Benchmark for under_cached_property cache miss."""
class Test:
def __init__(self) -> None:
self._cache: dict[str, int] = {}
@under_cached_property
def prop(self) -> int:
"""Return the value of the property."""
return 42
t = Test()
cache = t._cache
@benchmark
def _run() -> None:
for _ in range(100):
cache.pop("prop", None)
t.prop
def test_cached_property_cache_miss(benchmark: "BenchmarkFixture") -> None:
"""Benchmark for cached_property cache miss."""
class Test:
@cached_property
def prop(self) -> int:
"""Return the value of the property."""
return 42
t = Test()
cache = t.__dict__
@benchmark
def _run() -> None:
for _ in range(100):
cache.pop("prop", None)
t.prop
|