Skip to content

Commit 433dd40

Browse files
committed
test: add benchmarks for ipaddress object creation and hashing
1 parent cb81e67 commit 433dd40

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

tests/benchmarks/test_ipaddress.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""Benchmarks for zeroconf._utils.ipaddress address objects."""
2+
3+
from __future__ import annotations
4+
5+
from pytest_codspeed import BenchmarkFixture
6+
7+
from zeroconf._utils.ipaddress import ZeroconfIPv4Address, ZeroconfIPv6Address
8+
9+
_IPV4_STRS = [f"10.{(i >> 8) & 0xFF}.{i & 0xFF}.1" for i in range(1000)]
10+
_IPV6_BYTES = [(0x20010DB8 << 96 | i).to_bytes(16, "big") for i in range(1000)]
11+
12+
13+
def test_create_ipv4_addresses(benchmark: BenchmarkFixture) -> None:
14+
"""Benchmark constructing 1000 distinct IPv4 address objects."""
15+
16+
@benchmark
17+
def _create() -> None:
18+
for addr in _IPV4_STRS:
19+
ZeroconfIPv4Address(addr)
20+
21+
22+
def test_create_ipv6_addresses(benchmark: BenchmarkFixture) -> None:
23+
"""Benchmark constructing 1000 distinct IPv6 address objects."""
24+
25+
@benchmark
26+
def _create() -> None:
27+
for addr in _IPV6_BYTES:
28+
ZeroconfIPv6Address(addr)
29+
30+
31+
def test_hash_ipv4_address(benchmark: BenchmarkFixture) -> None:
32+
"""Benchmark hashing the same IPv4 address object 1000 times."""
33+
addr = ZeroconfIPv4Address("10.0.0.1")
34+
35+
@benchmark
36+
def _hash() -> None:
37+
for _ in range(1000):
38+
hash(addr)
39+
40+
41+
def test_hash_ipv6_address(benchmark: BenchmarkFixture) -> None:
42+
"""Benchmark hashing the same IPv6 address object 1000 times."""
43+
addr = ZeroconfIPv6Address(b"\x20\x01\x0d\xb8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01")
44+
45+
@benchmark
46+
def _hash() -> None:
47+
for _ in range(1000):
48+
hash(addr)

0 commit comments

Comments
 (0)