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_ipaddress.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""Benchmarks for zeroconf._utils.ipaddress address objects."""

from __future__ import annotations

from pytest_codspeed import BenchmarkFixture

from zeroconf._utils.ipaddress import ZeroconfIPv4Address, ZeroconfIPv6Address

_IPV4_STRS = [f"10.{(i >> 8) & 0xFF}.{i & 0xFF}.1" for i in range(1000)]
_IPV6_BYTES = [(0x20010DB8 << 96 | i).to_bytes(16, "big") for i in range(1000)]


def test_create_ipv4_addresses(benchmark: BenchmarkFixture) -> None:
"""Benchmark constructing 1000 distinct IPv4 address objects."""

@benchmark
def _create() -> None:
for addr in _IPV4_STRS:
ZeroconfIPv4Address(addr)


def test_create_ipv6_addresses(benchmark: BenchmarkFixture) -> None:
"""Benchmark constructing 1000 distinct IPv6 address objects."""

@benchmark
def _create() -> None:
for addr in _IPV6_BYTES:
ZeroconfIPv6Address(addr)


def test_hash_ipv4_address(benchmark: BenchmarkFixture) -> None:
"""Benchmark hashing the same IPv4 address object 1000 times."""
addr = ZeroconfIPv4Address("10.0.0.1")

@benchmark
def _hash() -> None:
for _ in range(1000):
hash(addr)


def test_hash_ipv6_address(benchmark: BenchmarkFixture) -> None:
"""Benchmark hashing the same IPv6 address object 1000 times."""
addr = ZeroconfIPv6Address(b"\x20\x01\x0d\xb8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01")

@benchmark
def _hash() -> None:
for _ in range(1000):
hash(addr)
Loading