From 5b0d9d024aa2e2b2378bc801607c63d6ca7b6bbe Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 9 Dec 2022 08:37:38 -1000 Subject: [PATCH] chore: add passive unmarshall benchmark (#185) --- bench/unmarshall_passive.py | 29 +++++++++++++++++++++++++++++ tests/test_marshaller.py | 20 ++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 bench/unmarshall_passive.py diff --git a/bench/unmarshall_passive.py b/bench/unmarshall_passive.py new file mode 100644 index 0000000..ff45bf4 --- /dev/null +++ b/bench/unmarshall_passive.py @@ -0,0 +1,29 @@ +import io +import timeit + +from dbus_fast._private.unmarshaller import Unmarshaller + +bluez_passive_message = ( + b"l\1\1\1*\0\0\0\205D\267\3\215\0\0\0\1\1o\0\35\0\0\0/org/bleak/61/281472597302272\0\0\0\6\1s\0\7\0\0" + b"\0:1.1450\0\2\1s\0\37\0\0\0org.bluez.AdvertisementMonitor1\0\3\1s\0\v\0\0\0DeviceFound\0\0\0\0\0\10" + b"\1g\0\1o\0\0\7\1s\0\4\0\0\0:1.4\0\0\0\0%\0\0\0/org/bluez/hci0/dev_58_D3_49_E6_02_6E\0l\1\1\1*\0\0\0" + b"\206D\267\3\215\0\0\0\1\1o\0\35\0\0\0/org/bleak/61/281472593362560\0\0\0\6\1s\0\7\0\0\0:1.1450\0\2" + b"\1s\0\37\0\0\0org.bluez.AdvertisementMonitor1\0\3\1s\0\v\0\0\0DeviceFound\0\0\0\0\0\10\1g\0\1o\0\0" + b"\7\1s\0\4\0\0\0:1.4\0\0\0\0%\0\0\0/org/bluez/hci1/dev_58_D3_49_E6_02_6E\0" +) + + +stream = io.BytesIO(bluez_passive_message) + +unmarshaller = Unmarshaller(stream) + + +def unmarhsall_bluez_rssi_message(): + stream.seek(0) + unmarshaller.reset() + unmarshaller.unmarshall() + + +count = 3000000 +time = timeit.Timer(unmarhsall_bluez_rssi_message).timeit(count) +print(f"Unmarshalling {count} bluetooth passive messages took {time} seconds") diff --git a/tests/test_marshaller.py b/tests/test_marshaller.py index 290f5f3..ae69eae 100644 --- a/tests/test_marshaller.py +++ b/tests/test_marshaller.py @@ -587,3 +587,23 @@ def test_marshalling_enum(): marshalled = msg._marshall(False) unmarshalled_msg = Unmarshaller(io.BytesIO(marshalled)).unmarshall() assert unpack_variants(unmarshalled_msg.body)[0] == RaucState.GOOD.value + + +def test_unmarshall_bluez_passive_message(): + """Test we can unmarshall a bluez passive message.""" + + bluez_passive_message = ( + b"l\1\1\1*\0\0\0\205D\267\3\215\0\0\0\1\1o\0\35\0\0\0/org/bleak/61/281472597302272\0\0\0\6\1s\0\7\0\0" + b"\0:1.1450\0\2\1s\0\37\0\0\0org.bluez.AdvertisementMonitor1\0\3\1s\0\v\0\0\0DeviceFound\0\0\0\0\0\10" + b"\1g\0\1o\0\0\7\1s\0\4\0\0\0:1.4\0\0\0\0%\0\0\0/org/bluez/hci0/dev_58_D3_49_E6_02_6E\0l\1\1\1*\0\0\0" + b"\206D\267\3\215\0\0\0\1\1o\0\35\0\0\0/org/bleak/61/281472593362560\0\0\0\6\1s\0\7\0\0\0:1.1450\0\2" + b"\1s\0\37\0\0\0org.bluez.AdvertisementMonitor1\0\3\1s\0\v\0\0\0DeviceFound\0\0\0\0\0\10\1g\0\1o\0\0" + b"\7\1s\0\4\0\0\0:1.4\0\0\0\0%\0\0\0/org/bluez/hci1/dev_58_D3_49_E6_02_6E\0" + ) + + stream = io.BytesIO(bluez_passive_message) + unmarshaller = Unmarshaller(stream) + unmarshaller.unmarshall() + message = unmarshaller.message + unpacked = unpack_variants(message.body) + assert unpacked == ["/org/bluez/hci0/dev_58_D3_49_E6_02_6E"]