File: test_cache.py

package info (click to toggle)
python-zeroconf 0.147.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,380 kB
  • sloc: python: 15,356; makefile: 23
file content (52 lines) | stat: -rw-r--r-- 1,439 bytes parent folder | download | duplicates (2)
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
from __future__ import annotations

import pytest
pytest_codspeed = pytest.importorskip("pytest_codspeed")
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 + id,
        )
        for id in range(1000)
    ]

    @benchmark
    def _expire_records() -> None:
        cache.async_add_records(records)
        cache.async_expire(now + 100_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 + id,
        )
        for id in range(1000)
    )
    cache.async_expire(now)

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