Skip to content

Commit 0dad543

Browse files
authored
chore: add benchmark for sending packets (#1471)
1 parent 3594988 commit 0dad543

3 files changed

Lines changed: 178 additions & 153 deletions

File tree

tests/benchmarks/helpers.py

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
"""Benchmark helpers."""
2+
3+
import socket
4+
5+
from zeroconf import DNSAddress, DNSOutgoing, DNSService, DNSText, const
6+
7+
8+
def generate_packets() -> DNSOutgoing:
9+
out = DNSOutgoing(const._FLAGS_QR_RESPONSE | const._FLAGS_AA)
10+
address = socket.inet_pton(socket.AF_INET, "192.168.208.5")
11+
12+
additionals = [
13+
{
14+
"name": "HASS Bridge ZJWH FF5137._hap._tcp.local.",
15+
"address": address,
16+
"port": 51832,
17+
"text": b"\x13md=HASS Bridge"
18+
b" ZJWH\x06pv=1.0\x14id=01:6B:30:FF:51:37\x05c#=12\x04s#=1\x04ff=0\x04"
19+
b"ci=2\x04sf=0\x0bsh=L0m/aQ==",
20+
},
21+
{
22+
"name": "HASS Bridge 3K9A C2582A._hap._tcp.local.",
23+
"address": address,
24+
"port": 51834,
25+
"text": b"\x13md=HASS Bridge"
26+
b" 3K9A\x06pv=1.0\x14id=E2:AA:5B:C2:58:2A\x05c#=12\x04s#=1\x04ff=0\x04"
27+
b"ci=2\x04sf=0\x0bsh=b2CnzQ==",
28+
},
29+
{
30+
"name": "Master Bed TV CEDB27._hap._tcp.local.",
31+
"address": address,
32+
"port": 51830,
33+
"text": b"\x10md=Master Bed"
34+
b" TV\x06pv=1.0\x14id=9E:B7:44:CE:DB:27\x05c#=18\x04s#=1\x04ff=0\x05"
35+
b"ci=31\x04sf=0\x0bsh=CVj1kw==",
36+
},
37+
{
38+
"name": "Living Room TV 921B77._hap._tcp.local.",
39+
"address": address,
40+
"port": 51833,
41+
"text": b"\x11md=Living Room"
42+
b" TV\x06pv=1.0\x14id=11:61:E7:92:1B:77\x05c#=17\x04s#=1\x04ff=0\x05"
43+
b"ci=31\x04sf=0\x0bsh=qU77SQ==",
44+
},
45+
{
46+
"name": "HASS Bridge ZC8X FF413D._hap._tcp.local.",
47+
"address": address,
48+
"port": 51829,
49+
"text": b"\x13md=HASS Bridge"
50+
b" ZC8X\x06pv=1.0\x14id=96:14:45:FF:41:3D\x05c#=12\x04s#=1\x04ff=0\x04"
51+
b"ci=2\x04sf=0\x0bsh=b0QZlg==",
52+
},
53+
{
54+
"name": "HASS Bridge WLTF 4BE61F._hap._tcp.local.",
55+
"address": address,
56+
"port": 51837,
57+
"text": b"\x13md=HASS Bridge"
58+
b" WLTF\x06pv=1.0\x14id=E0:E7:98:4B:E6:1F\x04c#=2\x04s#=1\x04ff=0\x04"
59+
b"ci=2\x04sf=0\x0bsh=ahAISA==",
60+
},
61+
{
62+
"name": "FrontdoorCamera 8941D1._hap._tcp.local.",
63+
"address": address,
64+
"port": 54898,
65+
"text": b"\x12md=FrontdoorCamera\x06pv=1.0\x14id=9F:B7:DC:89:41:D1\x04c#=2\x04"
66+
b"s#=1\x04ff=0\x04ci=2\x04sf=0\x0bsh=0+MXmA==",
67+
},
68+
{
69+
"name": "HASS Bridge W9DN 5B5CC5._hap._tcp.local.",
70+
"address": address,
71+
"port": 51836,
72+
"text": b"\x13md=HASS Bridge"
73+
b" W9DN\x06pv=1.0\x14id=11:8E:DB:5B:5C:C5\x05c#=12\x04s#=1\x04ff=0\x04"
74+
b"ci=2\x04sf=0\x0bsh=6fLM5A==",
75+
},
76+
{
77+
"name": "HASS Bridge Y9OO EFF0A7._hap._tcp.local.",
78+
"address": address,
79+
"port": 51838,
80+
"text": b"\x13md=HASS Bridge"
81+
b" Y9OO\x06pv=1.0\x14id=D3:FE:98:EF:F0:A7\x04c#=2\x04s#=1\x04ff=0\x04"
82+
b"ci=2\x04sf=0\x0bsh=u3bdfw==",
83+
},
84+
{
85+
"name": "Snooze Room TV 6B89B0._hap._tcp.local.",
86+
"address": address,
87+
"port": 51835,
88+
"text": b"\x11md=Snooze Room"
89+
b" TV\x06pv=1.0\x14id=5F:D5:70:6B:89:B0\x05c#=17\x04s#=1\x04ff=0\x05"
90+
b"ci=31\x04sf=0\x0bsh=xNTqsg==",
91+
},
92+
{
93+
"name": "AlexanderHomeAssistant 74651D._hap._tcp.local.",
94+
"address": address,
95+
"port": 54811,
96+
"text": b"\x19md=AlexanderHomeAssistant\x06pv=1.0\x14id=59:8A:0B:74:65:1D\x05"
97+
b"c#=14\x04s#=1\x04ff=0\x04ci=2\x04sf=0\x0bsh=ccZLPA==",
98+
},
99+
{
100+
"name": "HASS Bridge OS95 39C053._hap._tcp.local.",
101+
"address": address,
102+
"port": 51831,
103+
"text": b"\x13md=HASS Bridge"
104+
b" OS95\x06pv=1.0\x14id=7E:8C:E6:39:C0:53\x05c#=12\x04s#=1\x04ff=0\x04ci=2"
105+
b"\x04sf=0\x0bsh=Xfe5LQ==",
106+
},
107+
]
108+
109+
out.add_answer_at_time(
110+
DNSText(
111+
"HASS Bridge W9DN 5B5CC5._hap._tcp.local.",
112+
const._TYPE_TXT,
113+
const._CLASS_IN | const._CLASS_UNIQUE,
114+
const._DNS_OTHER_TTL,
115+
b"\x13md=HASS Bridge W9DN\x06pv=1.0\x14id=11:8E:DB:5B:5C:C5\x05c#=12\x04s#=1"
116+
b"\x04ff=0\x04ci=2\x04sf=0\x0bsh=6fLM5A==",
117+
),
118+
0,
119+
)
120+
121+
for record in additionals:
122+
out.add_additional_answer(
123+
DNSService(
124+
record["name"], # type: ignore
125+
const._TYPE_SRV,
126+
const._CLASS_IN | const._CLASS_UNIQUE,
127+
const._DNS_HOST_TTL,
128+
0,
129+
0,
130+
record["port"], # type: ignore
131+
record["name"], # type: ignore
132+
)
133+
)
134+
out.add_additional_answer(
135+
DNSText(
136+
record["name"], # type: ignore
137+
const._TYPE_TXT,
138+
const._CLASS_IN | const._CLASS_UNIQUE,
139+
const._DNS_OTHER_TTL,
140+
record["text"], # type: ignore
141+
)
142+
)
143+
out.add_additional_answer(
144+
DNSAddress(
145+
record["name"], # type: ignore
146+
const._TYPE_A,
147+
const._CLASS_IN | const._CLASS_UNIQUE,
148+
const._DNS_HOST_TTL,
149+
record["address"], # type: ignore
150+
)
151+
)
152+
153+
return out

tests/benchmarks/test_outgoing.py

Lines changed: 3 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -1,165 +1,15 @@
11
"""Benchmark for DNSOutgoing."""
22

3-
import socket
4-
53
from pytest_codspeed import BenchmarkFixture
64

7-
from zeroconf import DNSAddress, DNSOutgoing, DNSService, DNSText, const
85
from zeroconf._protocol.outgoing import State
96

10-
11-
def generate_packets() -> DNSOutgoing:
12-
out = DNSOutgoing(const._FLAGS_QR_RESPONSE | const._FLAGS_AA)
13-
address = socket.inet_pton(socket.AF_INET, "192.168.208.5")
14-
15-
additionals = [
16-
{
17-
"name": "HASS Bridge ZJWH FF5137._hap._tcp.local.",
18-
"address": address,
19-
"port": 51832,
20-
"text": b"\x13md=HASS Bridge"
21-
b" ZJWH\x06pv=1.0\x14id=01:6B:30:FF:51:37\x05c#=12\x04s#=1\x04ff=0\x04"
22-
b"ci=2\x04sf=0\x0bsh=L0m/aQ==",
23-
},
24-
{
25-
"name": "HASS Bridge 3K9A C2582A._hap._tcp.local.",
26-
"address": address,
27-
"port": 51834,
28-
"text": b"\x13md=HASS Bridge"
29-
b" 3K9A\x06pv=1.0\x14id=E2:AA:5B:C2:58:2A\x05c#=12\x04s#=1\x04ff=0\x04"
30-
b"ci=2\x04sf=0\x0bsh=b2CnzQ==",
31-
},
32-
{
33-
"name": "Master Bed TV CEDB27._hap._tcp.local.",
34-
"address": address,
35-
"port": 51830,
36-
"text": b"\x10md=Master Bed"
37-
b" TV\x06pv=1.0\x14id=9E:B7:44:CE:DB:27\x05c#=18\x04s#=1\x04ff=0\x05"
38-
b"ci=31\x04sf=0\x0bsh=CVj1kw==",
39-
},
40-
{
41-
"name": "Living Room TV 921B77._hap._tcp.local.",
42-
"address": address,
43-
"port": 51833,
44-
"text": b"\x11md=Living Room"
45-
b" TV\x06pv=1.0\x14id=11:61:E7:92:1B:77\x05c#=17\x04s#=1\x04ff=0\x05"
46-
b"ci=31\x04sf=0\x0bsh=qU77SQ==",
47-
},
48-
{
49-
"name": "HASS Bridge ZC8X FF413D._hap._tcp.local.",
50-
"address": address,
51-
"port": 51829,
52-
"text": b"\x13md=HASS Bridge"
53-
b" ZC8X\x06pv=1.0\x14id=96:14:45:FF:41:3D\x05c#=12\x04s#=1\x04ff=0\x04"
54-
b"ci=2\x04sf=0\x0bsh=b0QZlg==",
55-
},
56-
{
57-
"name": "HASS Bridge WLTF 4BE61F._hap._tcp.local.",
58-
"address": address,
59-
"port": 51837,
60-
"text": b"\x13md=HASS Bridge"
61-
b" WLTF\x06pv=1.0\x14id=E0:E7:98:4B:E6:1F\x04c#=2\x04s#=1\x04ff=0\x04"
62-
b"ci=2\x04sf=0\x0bsh=ahAISA==",
63-
},
64-
{
65-
"name": "FrontdoorCamera 8941D1._hap._tcp.local.",
66-
"address": address,
67-
"port": 54898,
68-
"text": b"\x12md=FrontdoorCamera\x06pv=1.0\x14id=9F:B7:DC:89:41:D1\x04c#=2\x04"
69-
b"s#=1\x04ff=0\x04ci=2\x04sf=0\x0bsh=0+MXmA==",
70-
},
71-
{
72-
"name": "HASS Bridge W9DN 5B5CC5._hap._tcp.local.",
73-
"address": address,
74-
"port": 51836,
75-
"text": b"\x13md=HASS Bridge"
76-
b" W9DN\x06pv=1.0\x14id=11:8E:DB:5B:5C:C5\x05c#=12\x04s#=1\x04ff=0\x04"
77-
b"ci=2\x04sf=0\x0bsh=6fLM5A==",
78-
},
79-
{
80-
"name": "HASS Bridge Y9OO EFF0A7._hap._tcp.local.",
81-
"address": address,
82-
"port": 51838,
83-
"text": b"\x13md=HASS Bridge"
84-
b" Y9OO\x06pv=1.0\x14id=D3:FE:98:EF:F0:A7\x04c#=2\x04s#=1\x04ff=0\x04"
85-
b"ci=2\x04sf=0\x0bsh=u3bdfw==",
86-
},
87-
{
88-
"name": "Snooze Room TV 6B89B0._hap._tcp.local.",
89-
"address": address,
90-
"port": 51835,
91-
"text": b"\x11md=Snooze Room"
92-
b" TV\x06pv=1.0\x14id=5F:D5:70:6B:89:B0\x05c#=17\x04s#=1\x04ff=0\x05"
93-
b"ci=31\x04sf=0\x0bsh=xNTqsg==",
94-
},
95-
{
96-
"name": "AlexanderHomeAssistant 74651D._hap._tcp.local.",
97-
"address": address,
98-
"port": 54811,
99-
"text": b"\x19md=AlexanderHomeAssistant\x06pv=1.0\x14id=59:8A:0B:74:65:1D\x05"
100-
b"c#=14\x04s#=1\x04ff=0\x04ci=2\x04sf=0\x0bsh=ccZLPA==",
101-
},
102-
{
103-
"name": "HASS Bridge OS95 39C053._hap._tcp.local.",
104-
"address": address,
105-
"port": 51831,
106-
"text": b"\x13md=HASS Bridge"
107-
b" OS95\x06pv=1.0\x14id=7E:8C:E6:39:C0:53\x05c#=12\x04s#=1\x04ff=0\x04ci=2"
108-
b"\x04sf=0\x0bsh=Xfe5LQ==",
109-
},
110-
]
111-
112-
out.add_answer_at_time(
113-
DNSText(
114-
"HASS Bridge W9DN 5B5CC5._hap._tcp.local.",
115-
const._TYPE_TXT,
116-
const._CLASS_IN | const._CLASS_UNIQUE,
117-
const._DNS_OTHER_TTL,
118-
b"\x13md=HASS Bridge W9DN\x06pv=1.0\x14id=11:8E:DB:5B:5C:C5\x05c#=12\x04s#=1"
119-
b"\x04ff=0\x04ci=2\x04sf=0\x0bsh=6fLM5A==",
120-
),
121-
0,
122-
)
123-
124-
for record in additionals:
125-
out.add_additional_answer(
126-
DNSService(
127-
record["name"], # type: ignore
128-
const._TYPE_SRV,
129-
const._CLASS_IN | const._CLASS_UNIQUE,
130-
const._DNS_HOST_TTL,
131-
0,
132-
0,
133-
record["port"], # type: ignore
134-
record["name"], # type: ignore
135-
)
136-
)
137-
out.add_additional_answer(
138-
DNSText(
139-
record["name"], # type: ignore
140-
const._TYPE_TXT,
141-
const._CLASS_IN | const._CLASS_UNIQUE,
142-
const._DNS_OTHER_TTL,
143-
record["text"], # type: ignore
144-
)
145-
)
146-
out.add_additional_answer(
147-
DNSAddress(
148-
record["name"], # type: ignore
149-
const._TYPE_A,
150-
const._CLASS_IN | const._CLASS_UNIQUE,
151-
const._DNS_HOST_TTL,
152-
record["address"], # type: ignore
153-
)
154-
)
155-
156-
return out
157-
158-
159-
out = generate_packets()
7+
from .helpers import generate_packets
1608

1619

16210
def test_parse_outgoing_message(benchmark: BenchmarkFixture) -> None:
11+
out = generate_packets()
12+
16313
@benchmark
16414
def make_outgoing_message() -> None:
16515
out.packets()

tests/benchmarks/test_send.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""Benchmark for sending packets."""
2+
3+
import pytest
4+
from pytest_codspeed import BenchmarkFixture
5+
6+
from zeroconf.asyncio import AsyncZeroconf
7+
8+
from .helpers import generate_packets
9+
10+
11+
@pytest.mark.asyncio
12+
async def test_sending_packets(benchmark: BenchmarkFixture) -> None:
13+
"""Benchmark sending packets."""
14+
aiozc = AsyncZeroconf(interfaces=["127.0.0.1"])
15+
await aiozc.zeroconf.async_wait_for_start()
16+
out = generate_packets()
17+
18+
@benchmark
19+
def _send_packets() -> None:
20+
aiozc.zeroconf.async_send(out)
21+
22+
await aiozc.async_close()

0 commit comments

Comments
 (0)