chore: enable codspeed benchmarks (#1456) · python-zeroconf/python-zeroconf@9f6af54

1+

"""Benchmark for DNSIncoming."""

2+3+

import socket

4+

from typing import List

5+6+

from pytest_codspeed import BenchmarkFixture

7+8+

from zeroconf import (

9+

DNSAddress,

10+

DNSIncoming,

11+

DNSNsec,

12+

DNSOutgoing,

13+

DNSService,

14+

DNSText,

15+

const,

16+

)

17+18+19+

def generate_packets() -> List[bytes]:

20+

out = DNSOutgoing(const._FLAGS_QR_RESPONSE | const._FLAGS_AA)

21+

address = socket.inet_pton(socket.AF_INET, "192.168.208.5")

22+23+

additionals = [

24+

{

25+

"name": "HASS Bridge ZJWH FF5137._hap._tcp.local.",

26+

"address": address,

27+

"port": 51832,

28+

"text": b"\x13md=HASS Bridge"

29+

b" ZJWH\x06pv=1.0\x14id=01:6B:30:FF:51:37\x05c#=12\x04s#=1\x04ff=0\x04"

30+

b"ci=2\x04sf=0\x0bsh=L0m/aQ==",

31+

},

32+

{

33+

"name": "HASS Bridge 3K9A C2582A._hap._tcp.local.",

34+

"address": address,

35+

"port": 51834,

36+

"text": b"\x13md=HASS Bridge"

37+

b" 3K9A\x06pv=1.0\x14id=E2:AA:5B:C2:58:2A\x05c#=12\x04s#=1\x04ff=0\x04"

38+

b"ci=2\x04sf=0\x0bsh=b2CnzQ==",

39+

},

40+

{

41+

"name": "Master Bed TV CEDB27._hap._tcp.local.",

42+

"address": address,

43+

"port": 51830,

44+

"text": b"\x10md=Master Bed"

45+

b" TV\x06pv=1.0\x14id=9E:B7:44:CE:DB:27\x05c#=18\x04s#=1\x04ff=0\x05"

46+

b"ci=31\x04sf=0\x0bsh=CVj1kw==",

47+

},

48+

{

49+

"name": "Living Room TV 921B77._hap._tcp.local.",

50+

"address": address,

51+

"port": 51833,

52+

"text": b"\x11md=Living Room"

53+

b" TV\x06pv=1.0\x14id=11:61:E7:92:1B:77\x05c#=17\x04s#=1\x04ff=0\x05"

54+

b"ci=31\x04sf=0\x0bsh=qU77SQ==",

55+

},

56+

{

57+

"name": "HASS Bridge ZC8X FF413D._hap._tcp.local.",

58+

"address": address,

59+

"port": 51829,

60+

"text": b"\x13md=HASS Bridge"

61+

b" ZC8X\x06pv=1.0\x14id=96:14:45:FF:41:3D\x05c#=12\x04s#=1\x04ff=0\x04"

62+

b"ci=2\x04sf=0\x0bsh=b0QZlg==",

63+

},

64+

{

65+

"name": "HASS Bridge WLTF 4BE61F._hap._tcp.local.",

66+

"address": address,

67+

"port": 51837,

68+

"text": b"\x13md=HASS Bridge"

69+

b" WLTF\x06pv=1.0\x14id=E0:E7:98:4B:E6:1F\x04c#=2\x04s#=1\x04ff=0\x04"

70+

b"ci=2\x04sf=0\x0bsh=ahAISA==",

71+

},

72+

{

73+

"name": "FrontdoorCamera 8941D1._hap._tcp.local.",

74+

"address": address,

75+

"port": 54898,

76+

"text": b"\x12md=FrontdoorCamera\x06pv=1.0\x14id=9F:B7:DC:89:41:D1\x04c#=2\x04"

77+

b"s#=1\x04ff=0\x04ci=2\x04sf=0\x0bsh=0+MXmA==",

78+

},

79+

{

80+

"name": "HASS Bridge W9DN 5B5CC5._hap._tcp.local.",

81+

"address": address,

82+

"port": 51836,

83+

"text": b"\x13md=HASS Bridge"

84+

b" W9DN\x06pv=1.0\x14id=11:8E:DB:5B:5C:C5\x05c#=12\x04s#=1\x04ff=0\x04"

85+

b"ci=2\x04sf=0\x0bsh=6fLM5A==",

86+

},

87+

{

88+

"name": "HASS Bridge Y9OO EFF0A7._hap._tcp.local.",

89+

"address": address,

90+

"port": 51838,

91+

"text": b"\x13md=HASS Bridge"

92+

b" Y9OO\x06pv=1.0\x14id=D3:FE:98:EF:F0:A7\x04c#=2\x04s#=1\x04ff=0\x04"

93+

b"ci=2\x04sf=0\x0bsh=u3bdfw==",

94+

},

95+

{

96+

"name": "Snooze Room TV 6B89B0._hap._tcp.local.",

97+

"address": address,

98+

"port": 51835,

99+

"text": b"\x11md=Snooze Room"

100+

b" TV\x06pv=1.0\x14id=5F:D5:70:6B:89:B0\x05c#=17\x04s#=1\x04ff=0\x05"

101+

b"ci=31\x04sf=0\x0bsh=xNTqsg==",

102+

},

103+

{

104+

"name": "AlexanderHomeAssistant 74651D._hap._tcp.local.",

105+

"address": address,

106+

"port": 54811,

107+

"text": b"\x19md=AlexanderHomeAssistant\x06pv=1.0\x14id=59:8A:0B:74:65:1D\x05"

108+

b"c#=14\x04s#=1\x04ff=0\x04ci=2\x04sf=0\x0bsh=ccZLPA==",

109+

},

110+

{

111+

"name": "HASS Bridge OS95 39C053._hap._tcp.local.",

112+

"address": address,

113+

"port": 51831,

114+

"text": b"\x13md=HASS Bridge"

115+

b" OS95\x06pv=1.0\x14id=7E:8C:E6:39:C0:53\x05c#=12\x04s#=1\x04ff=0\x04ci=2"

116+

b"\x04sf=0\x0bsh=Xfe5LQ==",

117+

},

118+

]

119+120+

out.add_answer_at_time(

121+

DNSText(

122+

"HASS Bridge W9DN 5B5CC5._hap._tcp.local.",

123+

const._TYPE_TXT,

124+

const._CLASS_IN | const._CLASS_UNIQUE,

125+

const._DNS_OTHER_TTL,

126+

b"\x13md=HASS Bridge W9DN\x06pv=1.0\x14id=11:8E:DB:5B:5C:C5\x05c#=12\x04s#=1"

127+

b"\x04ff=0\x04ci=2\x04sf=0\x0bsh=6fLM5A==",

128+

),

129+

0,

130+

)

131+132+

for record in additionals:

133+

out.add_additional_answer(

134+

DNSService(

135+

record["name"], # type: ignore

136+

const._TYPE_SRV,

137+

const._CLASS_IN | const._CLASS_UNIQUE,

138+

const._DNS_HOST_TTL,

139+

0,

140+

0,

141+

record["port"], # type: ignore

142+

record["name"], # type: ignore

143+

)

144+

)

145+

out.add_additional_answer(

146+

DNSText(

147+

record["name"], # type: ignore

148+

const._TYPE_TXT,

149+

const._CLASS_IN | const._CLASS_UNIQUE,

150+

const._DNS_OTHER_TTL,

151+

record["text"], # type: ignore

152+

)

153+

)

154+

out.add_additional_answer(

155+

DNSAddress(

156+

record["name"], # type: ignore

157+

const._TYPE_A,

158+

const._CLASS_IN | const._CLASS_UNIQUE,

159+

const._DNS_HOST_TTL,

160+

record["address"], # type: ignore

161+

)

162+

)

163+

out.add_additional_answer(

164+

DNSNsec(

165+

record["name"], # type: ignore

166+

const._TYPE_NSEC,

167+

const._CLASS_IN | const._CLASS_UNIQUE,

168+

const._DNS_OTHER_TTL,

169+

record["name"], # type: ignore

170+

[const._TYPE_TXT, const._TYPE_SRV],

171+

)

172+

)

173+174+

return out.packets()

175+176+177+

packets = generate_packets()

178+179+180+

def test_parse_incoming_message(benchmark: BenchmarkFixture) -> None:

181+

@benchmark

182+

def parse_incoming_message() -> None:

183+

for packet in packets:

184+

DNSIncoming(packet).answers # noqa: B018

185+

break