Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions tests/benchmarks/test_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from pytest_codspeed import BenchmarkFixture

from zeroconf import DNSCache, DNSPointer, current_time_millis
from zeroconf.const import _CLASS_IN, _TYPE_PTR


def test_add_expire_1000_records(benchmark: BenchmarkFixture) -> None:
"""Benchmark for DNSCache to expire 10000 records."""
cache = DNSCache()
now = current_time_millis()
records = [
DNSPointer(
name=f"test{id}.local.",
type_=_TYPE_PTR,
class_=_CLASS_IN,
ttl=60,
alias=f"test{id}.local.",
created=now,
)
for id in range(1000)
]

@benchmark
def _expire_records() -> None:
cache.async_add_records(records)
cache.async_expire(now + 61_000)


def test_expire_no_records_to_expire(benchmark: BenchmarkFixture) -> None:
"""Benchmark for DNSCache with 1000 records none to expire."""
cache = DNSCache()
now = current_time_millis()
cache.async_add_records(
DNSPointer(
name=f"test{id}.local.",
type_=_TYPE_PTR,
class_=_CLASS_IN,
ttl=60,
alias=f"test{id}.local.",
created=now,
)
for id in range(1000)
)
cache.async_expire(now)

@benchmark
def _expire_records() -> None:
cache.async_expire(now)