Skip to content

Commit 8b1bdf4

Browse files
Add performance guide
1 parent 660a1db commit 8b1bdf4

File tree

1 file changed

+92
-14
lines changed

1 file changed

+92
-14
lines changed
Lines changed: 92 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,110 @@
11
# Optimising application for throughput and power consumption
22

3+
BLE devices are usually battery powered so performance might mean different things in different applications. You have
4+
to decide what is more important in yours - minimising power consumption on one or both sides of the communication or
5+
maximising throughput and/or latency. Some optimisation steps can in fact achieve both.
36

7+
This guide will discuss some trade-offs that should be considered and best practices that improve performance on all
8+
fronts.
49

5-
## Advertising and Scanning
10+
## Power consumtpion
611

7-
## Connection vs periodic advertising
12+
Any radio activity will consume power. Depending on what the stack is doing you have to power the radio even when no
13+
data is being sent. It is important to understand when radio is active.
814

9-
## Modulation schemes
15+
### Connections
1016

11-
When supported by the host and controller, you can select different modulation schemes:
17+
The most intuitive power consumption rate to understand is when using connections. Each device needs to send a packet at
18+
below supervision timeout frequency to maintain a connection regardless of data transfer. This is the absolute minimum
19+
power consumption. Additionally, normally power is consumed at every connection event (exchange of packets between
20+
central and peripheral).
1221

13-
- LE 1M PHY.
14-
- LE 2M PHY.
15-
- LE coded PHY.
22+
It's worth considering if keeping the connection active is worth it. Connection in BLE is extremely fast and if you plan
23+
to only send a quick burst of data every minute it is better to connect and disconnect each time.
1624

17-
These provide different compromises between bandwidth, power usage and error resiliency (see BLUETOOTH SPECIFICATION Version 5.0 Vol 1, Part A - 1.2).
25+
The cost of the connection is proportionate to the negotiable connection interval. This can be set during `connect` or
26+
later through `updateConnectionParameters`. The higher the interval the more often radio is active. This is especially
27+
important for the peripheral which needs to enable the radio to receive packets.
1828

19-
You may set preferred PHYs (separately for RX and TX) using `setPreferredPhys()`. You may also set the currently used PHYs on a selected connection using `setPhy()`. Both of these settings are only advisory. The controller is allowed to make its own decision on the best PHY to use based on your request, the peer's supported features and the connection's physical conditions.
29+
This can be further helped through setting a high `slaveLatency` parameter. This allows the peripheral to skip
30+
connection events and save power not just by not sending any packets but by not even listening. This is not free for
31+
central as it increases latency of data transmission. Central may have to attempt sending data multiple times before the
32+
peripheral accepts the transmission.
2033

21-
You may query the currently used PHY using `readPhy()`, which returns the result through a call to the registered event handler. You may register the handler with `setEventHandler()`. The events inform about the currently used PHY and of any changes to PHYs, which the controller or the peer may trigger autonomously.
34+
### Advertising and scanning
2235

23-
## Data length (over-the-air MTU)
36+
Power draw during advertising is proportional to the advertising interval. Additionally, if the advertising is
37+
connectable or scannable it means the advertiser needs to listen on the radio after each advertisement for potential
38+
connection of scan requests.
2439

25-
In addition to modulation schemes, Maximum Transmission Unit (MTU) size also strongly affects throughput. Newer controllers allow you to negotiate bigger MTUs. Because each packet contains overhead, bigger packets maximize throughput.
40+
Scanning power draw is proportional to time spent scanning. The interaction between scanning an advertising means that
41+
the less power the advertiser spends advertising, the more power the scanner will have to spend to see the advertising
42+
packets. The decision on balance will be dictated by your design of your devices (which one is more constrained).
2643

27-
There are two separate MTUs to consider: the `ATT_MTU` (maximum attribute size) and data length. `GattServer` and `GattClient` affect `ATT_MTU`. `Gap` only deals with data length, which is the maximum size of the packet that carries attributes that are fragmented across many such packets.
44+
### Connection vs advertising
2845

29-
The default value of data length supported by all controllers is 23 octets. If both controllers support data length extension and a higher value is negotiated, the BLE stack will call `onDataLengthChange` in the `Gap::EventHandler` registered by the user.
46+
Instead of connecting to the device you can consider transferring data in advertising packets. This depends on the
47+
nature of the data.
3048

49+
A transfer over a connection will allow you to use the ATT protocol, this can handle acknowledgement for you. This might
50+
be a good choice if you're sending data that must get through reliably.
51+
52+
If your data is non-critical then advertising might be cheaper. You might have to accept less reliability and no built
53+
in acknowledgment. Additional benefit is that multiple devices may receive the data and each scanner may make their own
54+
decisions about power consumption.
55+
56+
### Periodic advertising
57+
58+
Periodic advertising allows you to get best of both worlds by having the power characteristics of advertising for the
59+
peripheral but also saving power for the scanner. After finding periodic advertising through `createSync` the scanner
60+
will only have to turn on the radio when the expected packet is due.
61+
62+
## Increasing throughput
63+
64+
### Modulation schemes
65+
66+
Depending on controller support different modulation schemes are available in BLE through `setPreferredPhys()` and
67+
`setPhy()`. While the coded PHY will increase reliability in noisy environments and increase range 2M PHY will increase
68+
the throughput saving power ber bit. If both devices support it and the signal quality is good then this is recommended
69+
to be enabled.
70+
71+
### Data length and ATT_MTU
72+
73+
Packet overhead strongly affects throughput. Newer controllers allow you to negotiate bigger MTUs thus decreasing the
74+
fragmentation overhead.
75+
76+
There are two separate MTUs to consider: the `ATT_MTU` (which affects ATT protocol operations) and data length extension
77+
(which affects transport packets). Increasing the sizes will increase memory usage but greatly increase throughput.
3178
`ATT_MTU` and data length are independent of each other.
3279

80+
The size of ATT_MTU doesn't have any other overhead than memory and should only be limited by your biggest attribute
81+
and available memory.
82+
83+
The default value of data length supported by all controllers is 23 octets. If both controllers support data length
84+
extension and a higher value is negotiated, the BLE stack will call `onDataLengthChange` in the `Gap::EventHandler`
85+
registered by the user. The supported length is set in the link layer with a maximum of 251. For Cordio Link Layer it
86+
is derived from the config option `cordio_ll.max-acl-size`.
87+
88+
Larger data length greatly increases throughput (although diminishing returns quickly set in above 80). The only
89+
potential drawback is in noisy environments where longer packets may cause slower effective transfer due to
90+
retransmissions (this is only related to data length, ATT_MTU does not affect this).
91+
92+
### Packet timings
93+
94+
If you're not constrained by battery power it might be tempting to use maximum/minimum values where possible.
95+
Advertising at maximum frequency and scanning continuously will speed up connecting. Setting intervals on connections
96+
will minimise latency and maximise number of connection events.
97+
98+
One key thing to consider when setting the connection interval low is that you are creating a boundary between which a
99+
sequence of packets must fit. This means that the last transfer must end before the next connection event starts. This
100+
dead time may become significant if the connection interval is short and packet length is long.
101+
102+
The connection interval shouldn't be shorter than what your data requires in terms of latency.
103+
104+
# Test and measure
105+
106+
Due to complexity of the stack the only reliable way to truly maximise performance is to test your application with
107+
representative data and measure the throughput and power usage. It's important to keep it mind that tweaking
108+
parameters by trial and error and fine tuning them will only be reliable as long as you control both devices (and even
109+
then the environment can change). If your device needs to communicate with an unknown device your fine-tuning should
110+
give way to sound principles since stack behaviour varies and you cannot test against all stacks.

0 commit comments

Comments
 (0)