mirror of
https://github.com/esphome/esphome.git
synced 2026-11-15 09:36:04 +00:00
Compare commits
2
Commits
dev
...
bench-uart-frames
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
13dcacfc33 | ||
|
|
76ad7a838e |
No files matched your search
@@ -25,6 +25,9 @@ CORE_BENCHMARKS_DIR: Path = Path(root_path) / "tests" / "benchmarks" / "core"
|
||||
# allow benchmarks to compile on the host platform.
|
||||
STUBS_DIR: Path = Path(root_path) / "tests" / "benchmarks" / "stubs"
|
||||
|
||||
# Shared test doubles, reached by -I so only the benchmarks that include them pull in their headers
|
||||
HELPERS_DIR: Path = Path(root_path) / "tests" / "benchmarks" / "helpers"
|
||||
|
||||
BUILD_FLAGS = [
|
||||
"-Os", # match firmware optimization level (detects inlining regressions)
|
||||
"-g", # debug symbols for profiling
|
||||
@@ -32,6 +35,7 @@ BUILD_FLAGS = [
|
||||
"-fdata-sections", # required for dead-code stripping with -Os
|
||||
"-DUSE_BENCHMARK", # disable WarnIfComponentBlockingGuard in finish()
|
||||
f"-I{STUBS_DIR}", # stub headers for ESP32-only components
|
||||
f"-I{HELPERS_DIR}", # shared test doubles such as fake_uart.h
|
||||
]
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
#include <benchmark/benchmark.h>
|
||||
|
||||
#include "esphome/components/binary_sensor/binary_sensor.h"
|
||||
#include "esphome/components/ld2450/ld2450.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "fake_uart.h"
|
||||
|
||||
namespace esphome::ld2450::benchmarks {
|
||||
|
||||
using esphome::benchmarks::FakeUART;
|
||||
|
||||
// Inner iteration count to amortize CodSpeed instrumentation overhead.
|
||||
static constexpr int kInnerIterations = 2000;
|
||||
static constexpr size_t FRAME_SIZE = 30;
|
||||
|
||||
// Periodic frames with one moving target at two positions
|
||||
static constexpr uint8_t FRAME_A[FRAME_SIZE] = {0xAA, 0xFF, 0x03, 0x00, 0x0E, 0x03, 0xB1, 0x86, 0x10, 0x00,
|
||||
0x40, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xCC};
|
||||
static constexpr uint8_t FRAME_B[FRAME_SIZE] = {0xAA, 0xFF, 0x03, 0x00, 0x2A, 0x03, 0xC8, 0x86, 0x10, 0x00,
|
||||
0x40, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xCC};
|
||||
|
||||
// --- loop() parsing one periodic frame, no entities attached ---
|
||||
|
||||
static void LD2450_PeriodicFrame(benchmark::State &state) {
|
||||
FakeUART uart;
|
||||
LD2450Component radar;
|
||||
radar.set_uart_parent(&uart);
|
||||
radar.set_link_timeout(60000);
|
||||
// Bring the link up outside the timed loop
|
||||
uart.load(FRAME_A, FRAME_SIZE);
|
||||
radar.loop();
|
||||
|
||||
for (auto _ : state) {
|
||||
for (int i = 0; i < kInnerIterations; i++) {
|
||||
uart.load(FRAME_A, FRAME_SIZE);
|
||||
radar.loop();
|
||||
}
|
||||
benchmark::DoNotOptimize(uart.available());
|
||||
}
|
||||
state.SetItemsProcessed(state.iterations() * kInnerIterations);
|
||||
}
|
||||
BENCHMARK(LD2450_PeriodicFrame);
|
||||
|
||||
// --- loop() parsing a moving target and publishing its sensors ---
|
||||
|
||||
static void LD2450_PeriodicFrameSensors(benchmark::State &state) {
|
||||
FakeUART uart;
|
||||
LD2450Component radar;
|
||||
radar.set_uart_parent(&uart);
|
||||
radar.set_link_timeout(60000);
|
||||
sensor::Sensor x, y, speed, distance, angle, count;
|
||||
binary_sensor::BinarySensor presence;
|
||||
radar.set_move_x_sensor(0, &x);
|
||||
radar.set_move_y_sensor(0, &y);
|
||||
radar.set_move_speed_sensor(0, &speed);
|
||||
radar.set_move_distance_sensor(0, &distance);
|
||||
radar.set_move_angle_sensor(0, &angle);
|
||||
radar.set_target_count_sensor(&count);
|
||||
radar.set_target_binary_sensor(&presence);
|
||||
uart.load(FRAME_A, FRAME_SIZE);
|
||||
radar.loop();
|
||||
|
||||
for (auto _ : state) {
|
||||
for (int i = 0; i < kInnerIterations; i++) {
|
||||
uart.load((i & 1) ? FRAME_B : FRAME_A, FRAME_SIZE);
|
||||
radar.loop();
|
||||
}
|
||||
benchmark::DoNotOptimize(x.state);
|
||||
}
|
||||
state.SetItemsProcessed(state.iterations() * kInnerIterations);
|
||||
}
|
||||
BENCHMARK(LD2450_PeriodicFrameSensors);
|
||||
|
||||
} // namespace esphome::ld2450::benchmarks
|
||||
@@ -0,0 +1,2 @@
|
||||
sensor:
|
||||
binary_sensor:
|
||||
@@ -0,0 +1,67 @@
|
||||
#include <benchmark/benchmark.h>
|
||||
|
||||
#include "esphome/components/modbus/modbus.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "fake_uart.h"
|
||||
|
||||
namespace esphome::modbus::benchmarks {
|
||||
|
||||
using esphome::benchmarks::FakeUART;
|
||||
|
||||
// Inner iteration count to amortize CodSpeed instrumentation overhead.
|
||||
static constexpr int kInnerIterations = 2000;
|
||||
|
||||
static constexpr uint8_t ADDRESS = 1;
|
||||
static constexpr uint8_t REGISTER_COUNT = 10;
|
||||
// Read 10 holding registers from address 0, as an energy meter poll does
|
||||
static constexpr uint8_t REQUEST_PDU[] = {0x03, 0x00, 0x00, 0x00, REGISTER_COUNT};
|
||||
// Address + function + byte count + 20 data bytes + CRC
|
||||
static constexpr size_t RESPONSE_SIZE = 3 + REGISTER_COUNT * 2 + 2;
|
||||
|
||||
class SumDevice : public ModbusClientDevice {
|
||||
public:
|
||||
using ModbusClientDevice::ModbusClientDevice;
|
||||
void on_read_holding_registers(uint16_t start_address, std::span<const uint16_t> registers,
|
||||
ResponseStatus status) override {
|
||||
for (uint16_t value : registers)
|
||||
this->sum += value;
|
||||
}
|
||||
uint32_t sum{0};
|
||||
};
|
||||
|
||||
class NoDelayHub : public ModbusClientHub {
|
||||
protected:
|
||||
// No wait before a send, so every loop() can send the next poll
|
||||
int32_t tx_delay_remaining() override { return 0; }
|
||||
};
|
||||
|
||||
// --- One continuous poll cycle per loop(): receive and CRC check the response, decode it, requeue and resend ---
|
||||
|
||||
static void Modbus_ReadHoldingPollCycle(benchmark::State &state) {
|
||||
// Address 1, function 3, byte count 20, 10 registers; the CRC goes in the last two bytes
|
||||
uint8_t response[RESPONSE_SIZE] = {0x01, 0x03, 0x14, 0x09, 0x1F, 0x00, 0x00, 0x01, 0xF4, 0x00, 0x00, 0x13,
|
||||
0x88, 0x00, 0x32, 0x03, 0xE8, 0x00, 0x00, 0x00, 0x64, 0x12, 0x34};
|
||||
put_le16(&response[RESPONSE_SIZE - 2], crc16(response, RESPONSE_SIZE - 2));
|
||||
|
||||
FakeUART uart;
|
||||
uart.set_baud_rate(9600);
|
||||
NoDelayHub hub;
|
||||
hub.set_uart_parent(&uart);
|
||||
hub.setup();
|
||||
SumDevice device(&hub, ADDRESS);
|
||||
hub.queue_pdu(ADDRESS, REQUEST_PDU, &device, {.continuous = true});
|
||||
// The first loop() sends the poll; every send rewinds the response, so the device answers each one
|
||||
hub.loop();
|
||||
uart.load(response, RESPONSE_SIZE);
|
||||
|
||||
for (auto _ : state) {
|
||||
for (int i = 0; i < kInnerIterations; i++) {
|
||||
hub.loop();
|
||||
}
|
||||
benchmark::DoNotOptimize(device.sum);
|
||||
}
|
||||
state.SetItemsProcessed(state.iterations() * kInnerIterations);
|
||||
}
|
||||
BENCHMARK(Modbus_ReadHoldingPollCycle);
|
||||
|
||||
} // namespace esphome::modbus::benchmarks
|
||||
@@ -0,0 +1,57 @@
|
||||
#include <benchmark/benchmark.h>
|
||||
|
||||
#include "esphome/components/uart/uart.h"
|
||||
#include "fake_uart.h"
|
||||
|
||||
namespace esphome::uart::benchmarks {
|
||||
|
||||
using esphome::benchmarks::FakeUART;
|
||||
|
||||
// Inner iteration count to amortize CodSpeed instrumentation overhead.
|
||||
static constexpr int kInnerIterations = 2000;
|
||||
|
||||
// One second of a GPS module's NMEA output
|
||||
static constexpr char NMEA_BURST[] = "$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47\r\n"
|
||||
"$GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A\r\n"
|
||||
"$GPGSA,A,3,04,05,,09,12,,,24,,,,,2.5,1.3,2.1*39\r\n"
|
||||
"$GPGSV,2,1,08,01,40,083,46,02,17,308,41,12,07,344,39,14,22,228,45*75\r\n"
|
||||
"$GPVTG,054.7,T,034.4,M,005.5,N,010.2,K*48\r\n";
|
||||
|
||||
// --- read_available(): drain a whole NMEA burst through a byte-sum callback ---
|
||||
|
||||
static void UART_ReadAvailable_NmeaBurst(benchmark::State &state) {
|
||||
FakeUART uart;
|
||||
UARTDevice dev(&uart);
|
||||
uint32_t sum = 0;
|
||||
|
||||
for (auto _ : state) {
|
||||
for (int i = 0; i < kInnerIterations; i++) {
|
||||
uart.load(reinterpret_cast<const uint8_t *>(NMEA_BURST), sizeof(NMEA_BURST) - 1);
|
||||
dev.read_available([&](uint8_t b) { sum += b; });
|
||||
}
|
||||
benchmark::DoNotOptimize(sum);
|
||||
}
|
||||
state.SetItemsProcessed(state.iterations() * kInnerIterations);
|
||||
}
|
||||
BENCHMARK(UART_ReadAvailable_NmeaBurst);
|
||||
|
||||
// --- discard_available(): drop a 256 byte backlog ---
|
||||
|
||||
// Two virtual calls per byte, available() and read_byte(): this measures that per-byte call overhead
|
||||
static void UART_DiscardAvailable(benchmark::State &state) {
|
||||
uint8_t backlog[256]{};
|
||||
FakeUART uart;
|
||||
UARTDevice dev(&uart);
|
||||
|
||||
for (auto _ : state) {
|
||||
for (int i = 0; i < kInnerIterations; i++) {
|
||||
uart.load(backlog, sizeof(backlog));
|
||||
dev.discard_available();
|
||||
}
|
||||
benchmark::DoNotOptimize(uart.available());
|
||||
}
|
||||
state.SetItemsProcessed(state.iterations() * kInnerIterations);
|
||||
}
|
||||
BENCHMARK(UART_DiscardAvailable);
|
||||
|
||||
} // namespace esphome::uart::benchmarks
|
||||
@@ -0,0 +1,93 @@
|
||||
#include <benchmark/benchmark.h>
|
||||
|
||||
#include "esphome/core/frame_reader.h"
|
||||
#include "esphome/core/line_reader.h"
|
||||
|
||||
namespace esphome::benchmarks {
|
||||
|
||||
// Inner iteration count to amortize CodSpeed instrumentation overhead.
|
||||
static constexpr int kInnerIterations = 2000;
|
||||
|
||||
static constexpr char NMEA_GGA[] = "$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47\r\n";
|
||||
|
||||
// A 30 byte frame with a 4 byte header and a 2 byte tail
|
||||
static constexpr uint8_t FRAME_30[30] = {0xAA, 0xFF, 0x03, 0x00, 0x0E, 0x03, 0xB1, 0x86, 0x10, 0x00,
|
||||
0x40, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xCC};
|
||||
|
||||
// Four A02YYUW distance frames among noise bytes; the doubled 0xFF fails the checksum and resyncs onto the
|
||||
// frame behind it. Ends on a whole frame, so every pass starts clean
|
||||
static constexpr uint8_t ULTRASONIC_NOISY[] = {0x12, 0xFF, 0x07, 0xA1, 0xA7, 0x34, 0xFF, 0xFF, 0x07, 0xA1, 0xA7,
|
||||
0x00, 0xFF, 0x07, 0xA1, 0xA7, 0x56, 0xFF, 0x07, 0xA1, 0xA7};
|
||||
|
||||
// --- LineReader: one GGA sentence fed byte by byte ---
|
||||
|
||||
static void LineReader_NmeaLine(benchmark::State &state) {
|
||||
LineReader<96> reader;
|
||||
const auto *bytes = reinterpret_cast<const uint8_t *>(NMEA_GGA);
|
||||
size_t total = 0;
|
||||
|
||||
for (auto _ : state) {
|
||||
benchmark::DoNotOptimize(bytes);
|
||||
for (int i = 0; i < kInnerIterations; i++) {
|
||||
for (size_t j = 0; j < sizeof(NMEA_GGA) - 1; j++) {
|
||||
size_t len;
|
||||
if (reader.feed(bytes[j], len) != nullptr)
|
||||
total += len;
|
||||
}
|
||||
}
|
||||
benchmark::DoNotOptimize(total);
|
||||
}
|
||||
state.SetItemsProcessed(state.iterations() * kInnerIterations);
|
||||
}
|
||||
BENCHMARK(LineReader_NmeaLine);
|
||||
|
||||
// --- FrameReader: a 30 byte frame behind a 4 byte header ---
|
||||
|
||||
static void FrameReader_30ByteFrame(benchmark::State &state) {
|
||||
FrameReader<sizeof(FRAME_30), 0xAA, 0xFF, 0x03, 0x00> reader;
|
||||
const uint8_t *bytes = FRAME_30;
|
||||
uint32_t frames = 0;
|
||||
|
||||
for (auto _ : state) {
|
||||
benchmark::DoNotOptimize(bytes);
|
||||
for (int i = 0; i < kInnerIterations; i++) {
|
||||
for (size_t j = 0; j < sizeof(FRAME_30); j++) {
|
||||
if (reader.feed(bytes[j]) != nullptr)
|
||||
frames++;
|
||||
}
|
||||
}
|
||||
benchmark::DoNotOptimize(frames);
|
||||
}
|
||||
state.SetItemsProcessed(state.iterations() * kInnerIterations);
|
||||
}
|
||||
BENCHMARK(FrameReader_30ByteFrame);
|
||||
|
||||
// --- FrameReader: 4 byte checksummed frames, skipping noise and resyncing on a bad checksum ---
|
||||
|
||||
static void FrameReader_UltrasonicNoisy(benchmark::State &state) {
|
||||
FrameReader<4, 0xFF> reader;
|
||||
const uint8_t *bytes = ULTRASONIC_NOISY;
|
||||
uint32_t frames = 0;
|
||||
|
||||
for (auto _ : state) {
|
||||
benchmark::DoNotOptimize(bytes);
|
||||
for (int i = 0; i < kInnerIterations; i++) {
|
||||
for (size_t j = 0; j < sizeof(ULTRASONIC_NOISY); j++) {
|
||||
const uint8_t *frame = reader.feed(bytes[j]);
|
||||
if (frame == nullptr)
|
||||
continue;
|
||||
if (frame[3] == static_cast<uint8_t>(frame[0] + frame[1] + frame[2])) {
|
||||
frames++;
|
||||
} else {
|
||||
reader.resync();
|
||||
}
|
||||
}
|
||||
}
|
||||
benchmark::DoNotOptimize(frames);
|
||||
}
|
||||
state.SetItemsProcessed(state.iterations() * kInnerIterations);
|
||||
}
|
||||
BENCHMARK(FrameReader_UltrasonicNoisy);
|
||||
|
||||
} // namespace esphome::benchmarks
|
||||
@@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
|
||||
#include "esphome/components/uart/uart_component.h"
|
||||
|
||||
namespace esphome::benchmarks {
|
||||
|
||||
// Serves a fixed byte stream as received data. Every write rewinds it, so the stream answers each request.
|
||||
class FakeUART : public uart::UARTComponent {
|
||||
public:
|
||||
void load(const uint8_t *data, size_t len) {
|
||||
this->data_ = data;
|
||||
this->len_ = len;
|
||||
this->pos_ = 0;
|
||||
}
|
||||
void rewind() { this->pos_ = 0; }
|
||||
|
||||
void write_array(const uint8_t *data, size_t len) override { this->rewind(); }
|
||||
bool peek_byte(uint8_t *data) override {
|
||||
if (this->pos_ >= this->len_)
|
||||
return false;
|
||||
*data = this->data_[this->pos_];
|
||||
return true;
|
||||
}
|
||||
bool read_array(uint8_t *data, size_t len) override {
|
||||
if (len > this->len_ - this->pos_)
|
||||
return false;
|
||||
std::memcpy(data, this->data_ + this->pos_, len);
|
||||
this->pos_ += len;
|
||||
return true;
|
||||
}
|
||||
size_t available() override { return this->len_ - this->pos_; }
|
||||
uart::UARTFlushResult flush() override { return uart::UARTFlushResult::UART_FLUSH_RESULT_ASSUMED_SUCCESS; }
|
||||
void check_logger_conflict() override {}
|
||||
|
||||
protected:
|
||||
const uint8_t *data_{nullptr};
|
||||
size_t len_{0};
|
||||
size_t pos_{0};
|
||||
};
|
||||
|
||||
} // namespace esphome::benchmarks
|
||||
Reference in new issue
Block a user