Skip to content

Commit

Permalink
chore: add simple scanner benchmark (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Dec 22, 2024
1 parent 3dee0a0 commit 28f9fa5
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 6 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,25 @@ jobs:
with:
token: ${{ secrets.CODECOV_TOKEN }}

benchmarks:
name: Run benchmarks
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.13"
cache: "pip" # caching pip dependencies
- uses: snok/install-poetry@v1.3.4
- name: Install Dependencies
run: poetry install
shell: bash
- name: Run benchmarks
uses: CodSpeedHQ/action@v3
with:
token: ${{ secrets.CODSPEED_TOKEN }}
run: poetry run pytest tests/ --codspeed

release:
needs:
- test
Expand Down
52 changes: 50 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ lru-dict = ">=1.2.0"
[tool.poetry.group.dev.dependencies]
pytest = "^7.0"
pytest-cov = ">=3,<7"
pytest-codspeed = "^3.1.0"

[tool.poetry.group.docs]
optional = true
Expand Down
36 changes: 32 additions & 4 deletions tests/backend/test_scanner.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from habluetooth import (
BaseHaRemoteScanner,
HaBluetoothConnector,
from aioesphomeapi import (
BluetoothLERawAdvertisement,
BluetoothLERawAdvertisementsResponse,
)
from habluetooth import BaseHaRemoteScanner, HaBluetoothConnector
from pytest_codspeed import BenchmarkFixture

from bleak_esphome.backend.client import ESPHomeClientData
from bleak_esphome.backend.scanner import ESPHomeScanner
Expand All @@ -10,7 +12,33 @@
ESP_NAME = "proxy"


def test_scanner():
def test_scanner() -> None:
connector = HaBluetoothConnector(ESPHomeClientData, ESP_MAC_ADDRESS, lambda: True)
scanner = ESPHomeScanner(ESP_MAC_ADDRESS, ESP_NAME, connector, True)
assert isinstance(scanner, BaseHaRemoteScanner)


def test_scanner_async_on_advertisement(benchmark: BenchmarkFixture) -> None:
connector = HaBluetoothConnector(ESPHomeClientData, ESP_MAC_ADDRESS, lambda: True)
scanner = ESPHomeScanner(ESP_MAC_ADDRESS, ESP_NAME, connector, True)
adv = BluetoothLERawAdvertisementsResponse(
advertisements=[
BluetoothLERawAdvertisement(
address=261602360644300,
rssi=-96,
address_type=1,
data=b"\x02\x01\x04\x03\x03\x07\xfe\x18\xff\x97\x05\x06\x00\x16p%\x00\xca\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x02\n\x00",
),
BluetoothLERawAdvertisement(
address=246965243285491,
rssi=-88,
address_type=1,
data=b"\x02\x01\x1a\x1b\xffu\x00B\x04\x01\x01o\xe0\x8d\x17\xe7\x0f\xf3\xe2\x8d\x17\xe7\x0f\xf2(\x00\x00\x00\x00\x00\x00",
),
]
)

@benchmark
def _benchmark():
for _ in range(1000):
scanner.async_on_raw_advertisements(adv)
42 changes: 42 additions & 0 deletions tests/backend/test_scanner_benchmarks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from aioesphomeapi import (
BluetoothLERawAdvertisement,
BluetoothLERawAdvertisementsResponse,
)
from habluetooth import (
BaseHaRemoteScanner,
HaBluetoothConnector,
)

from bleak_esphome.backend.client import ESPHomeClientData
from bleak_esphome.backend.scanner import ESPHomeScanner

ESP_MAC_ADDRESS = "AA:BB:CC:DD:EE:FF"
ESP_NAME = "proxy"


def test_scanner() -> None:
connector = HaBluetoothConnector(ESPHomeClientData, ESP_MAC_ADDRESS, lambda: True)
scanner = ESPHomeScanner(ESP_MAC_ADDRESS, ESP_NAME, connector, True)
assert isinstance(scanner, BaseHaRemoteScanner)


def test_scanner_async_on_advertisement() -> None:
connector = HaBluetoothConnector(ESPHomeClientData, ESP_MAC_ADDRESS, lambda: True)
scanner = ESPHomeScanner(ESP_MAC_ADDRESS, ESP_NAME, connector, True)
adv = BluetoothLERawAdvertisementsResponse(
advertisements=[
BluetoothLERawAdvertisement(
address=261602360644300,
rssi=-96,
address_type=1,
data=b"\x02\x01\x04\x03\x03\x07\xfe\x18\xff\x97\x05\x06\x00\x16p%\x00\xca\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x02\n\x00",
),
BluetoothLERawAdvertisement(
address=246965243285491,
rssi=-88,
address_type=1,
data=b"\x02\x01\x1a\x1b\xffu\x00B\x04\x01\x01o\xe0\x8d\x17\xe7\x0f\xf3\xe2\x8d\x17\xe7\x0f\xf2(\x00\x00\x00\x00\x00\x00",
),
]
)
scanner.async_on_raw_advertisements(adv)

0 comments on commit 28f9fa5

Please sign in to comment.