Skip to content

Commit d6f1fda

Browse files
authored
chore: add benchmarks for adding and expiring records (#1489)
1 parent 8f86b35 commit d6f1fda

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

tests/benchmarks/test_cache.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from pytest_codspeed import BenchmarkFixture
2+
3+
from zeroconf import DNSCache, DNSPointer, current_time_millis
4+
from zeroconf.const import _CLASS_IN, _TYPE_PTR
5+
6+
7+
def test_add_expire_1000_records(benchmark: BenchmarkFixture) -> None:
8+
"""Benchmark for DNSCache to expire 10000 records."""
9+
cache = DNSCache()
10+
now = current_time_millis()
11+
records = [
12+
DNSPointer(
13+
name=f"test{id}.local.",
14+
type_=_TYPE_PTR,
15+
class_=_CLASS_IN,
16+
ttl=60,
17+
alias=f"test{id}.local.",
18+
created=now,
19+
)
20+
for id in range(1000)
21+
]
22+
23+
@benchmark
24+
def _expire_records() -> None:
25+
cache.async_add_records(records)
26+
cache.async_expire(now + 61_000)
27+
28+
29+
def test_expire_no_records_to_expire(benchmark: BenchmarkFixture) -> None:
30+
"""Benchmark for DNSCache with 1000 records none to expire."""
31+
cache = DNSCache()
32+
now = current_time_millis()
33+
cache.async_add_records(
34+
DNSPointer(
35+
name=f"test{id}.local.",
36+
type_=_TYPE_PTR,
37+
class_=_CLASS_IN,
38+
ttl=60,
39+
alias=f"test{id}.local.",
40+
created=now,
41+
)
42+
for id in range(1000)
43+
)
44+
cache.async_expire(now)
45+
46+
@benchmark
47+
def _expire_records() -> None:
48+
cache.async_expire(now)

0 commit comments

Comments
 (0)