chore: add socket unmarshall benchmark (#428)

* chore: add socket unmarshall benchmark

* chore: bench multiple messages
This commit is contained in:
J. Nick Koston 2025-03-22 14:35:37 -10:00 committed by GitHub
parent bfc8151005
commit 1802f35ce9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,4 +1,5 @@
import io
import socket
from pytest_codspeed import BenchmarkFixture
@ -25,7 +26,6 @@ def test_unmarshall_bluez_rssi_message(benchmark: BenchmarkFixture) -> None:
unmarshaller.unmarshall()
def test_unmarshall_bluez_properties_message(benchmark: BenchmarkFixture) -> None:
bluez_properties_message = (
b"l\4\1\0014\0\0\0\16Q\16\0\225\0\0\0\1\1o\0%\0\0\0/org/bluez/hci2/dev_08_3A_F2_1E_28_89\0\0\0\2\1s\0\37\0\0\0"
b"org.freedesktop.DBus.Properties\0\3\1s\0\21\0\0\0PropertiesChanged\0\0\0\0\0\0\0\10\1g\0\10sa{sv}as\0\0\0\7\1"
@ -56,6 +56,9 @@ def test_unmarshall_bluez_properties_message(benchmark: BenchmarkFixture) -> Non
b"Bus.Properties\0\3\1s\0\21\0\0\0PropertiesChanged\0\0\0\0\0\0\0\10\1g\0\10sa{sv}as\0\0\0\7\1s\0\4\0\0\0:1.5\0\0\0\0"
b"\21\0\0\0org.bluez.Device1\0\0\0\16\0\0\0\0\0\0\0\4\0\0\0RSSI\0\1n\0\306\377\0\0\0\0\0\0"
)
def test_unmarshall_bluez_properties_message(benchmark: BenchmarkFixture) -> None:
stream = io.BytesIO(bluez_properties_message)
unmarshaller = Unmarshaller(stream)
@ -64,3 +67,35 @@ def test_unmarshall_bluez_properties_message(benchmark: BenchmarkFixture) -> Non
def unmarshall_bluez_properties_message():
stream.seek(0)
unmarshaller.unmarshall()
def test_unmarshall_multiple_bluez_properties_message(
benchmark: BenchmarkFixture,
) -> None:
stream = io.BytesIO(bluez_properties_message)
unmarshaller = Unmarshaller(stream)
@benchmark
def unmarshall_bluez_properties_message():
stream.seek(0)
for _ in range(9):
unmarshaller.unmarshall()
def test_unmarshall_multiple_bluez_properties_message_socket(
benchmark: BenchmarkFixture,
) -> None:
sock1, sock2 = socket.socketpair()
sock1.setblocking(False)
sock2.setblocking(False)
unmarshaller = Unmarshaller(None, sock1, False)
@benchmark
def unmarshall_bluez_properties_message():
sock2.send(bluez_properties_message)
for _ in range(9):
unmarshaller.unmarshall()
sock1.close()
sock2.close()