Skip to content

Commit

Permalink
Merge pull request #394 from google/gbg/hci-latency
Browse files Browse the repository at this point in the history
add support for HCI latency probing
  • Loading branch information
barbibulle authored Jan 8, 2024
2 parents caacc0c + 5f377c0 commit 56eb5a9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
1 change: 1 addition & 0 deletions apps/bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ async def run(self):
await self.packet_io.send_packet(bytes([PacketType.RESET]))

self.current_packet_index = 0
self.latencies = []
await self.send_next_ping()

await self.done.wait()
Expand Down
34 changes: 30 additions & 4 deletions apps/controller_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
import asyncio
import os
import logging
import time

import click
from bumble.company_ids import COMPANY_IDENTIFIERS

from bumble.company_ids import COMPANY_IDENTIFIERS
from bumble.colors import color
from bumble.core import name_or_number
from bumble.hci import (
Expand Down Expand Up @@ -48,6 +50,7 @@
HCI_LE_Read_Maximum_Advertising_Data_Length_Command,
HCI_LE_READ_SUGGESTED_DEFAULT_DATA_LENGTH_COMMAND,
HCI_LE_Read_Suggested_Default_Data_Length_Command,
HCI_Read_Local_Version_Information_Command,
)
from bumble.host import Host
from bumble.transport import open_transport_or_link
Expand Down Expand Up @@ -166,14 +169,31 @@ async def get_acl_flow_control_info(host: Host) -> None:


# -----------------------------------------------------------------------------
async def async_main(transport):
async def async_main(latency_probes, transport):
print('<<< connecting to HCI...')
async with await open_transport_or_link(transport) as (hci_source, hci_sink):
print('<<< connected')

host = Host(hci_source, hci_sink)
await host.reset()

# Measure the latency if requested
latencies = []
if latency_probes:
for _ in range(latency_probes):
start = time.time()
await host.send_command(HCI_Read_Local_Version_Information_Command())
latencies.append(1000 * (time.time() - start))
print(
color('HCI Command Latency:', 'yellow'),
(
f'min={min(latencies):.2f}, '
f'max={max(latencies):.2f}, '
f'average={sum(latencies)/len(latencies):.2f}'
),
'\n',
)

# Print version
print(color('Version:', 'yellow'))
print(
Expand Down Expand Up @@ -209,10 +229,16 @@ async def async_main(transport):

# -----------------------------------------------------------------------------
@click.command()
@click.option(
'--latency-probes',
metavar='N',
type=int,
help='Send N commands to measure HCI transport latency statistics',
)
@click.argument('transport')
def main(transport):
def main(latency_probes, transport):
logging.basicConfig(level=os.environ.get('BUMBLE_LOGLEVEL', 'WARNING').upper())
asyncio.run(async_main(transport))
asyncio.run(async_main(latency_probes, transport))


# -----------------------------------------------------------------------------
Expand Down

0 comments on commit 56eb5a9

Please sign in to comment.