Compare commits

..
9 changed files with 343 additions and 188 deletions

No files matched your search

+4
View File
@@ -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
@@ -1,30 +0,0 @@
import esphome.codegen as cg
from esphome.components import remote_base
from esphome.core import CORE, ID
from esphome.types import ConfigType
from tests.testing_helpers import ComponentManifestOverride
def override_manifest(manifest: ComponentManifestOverride) -> None:
async def to_code(config: ConfigType) -> None:
# Called directly: validate_dumpers plus build_dumpers would leave the GenerateIDs unresolved
dumpers = [
await entry.coroutine_fun(
entry.schema({}),
ID(f"bench_dump_{name}", is_declaration=True, type=entry.type_id),
)
for name, entry in remote_base.DUMPER_REGISTRY.items()
]
CORE.add_global_after_includes(
cg.RawStatement(
"namespace esphome::benchmarks {\n"
f"extern remote_base::RemoteReceiverDumperBase *const DUMP_ALL[] = {cg.ArrayInitializer(*dumpers)};\n"
f"extern const size_t DUMP_ALL_COUNT = {len(dumpers)};\n"
"}"
)
)
# Set directly, not through add_dumper/add_listener: the C++ registers its dumper table and listeners itself
cg.add_define("REMOTE_BASE_DUMPER_COUNT", 1)
cg.add_define("REMOTE_BASE_LISTENER_COUNT", 8)
manifest.to_code = to_code
@@ -1,158 +0,0 @@
#include <benchmark/benchmark.h>
#include "esphome/components/remote_base/nec_protocol.h"
#include "esphome/components/remote_base/rc_switch_protocol.h"
#include "esphome/components/remote_base/remote_base.h"
namespace esphome::benchmarks {
// Every in-tree dumper, built by the codegen in __init__.py as `dump: all` does
extern remote_base::RemoteReceiverDumperBase *const DUMP_ALL[];
extern const size_t DUMP_ALL_COUNT;
using remote_base::RawTimings;
using remote_base::RemoteReceiveData;
// Amortizes CodSpeed's per-iteration instrumentation cost
static constexpr int kInnerIterations = 2000;
static constexpr uint32_t TOLERANCE = 25;
static constexpr auto TOLERANCE_MODE = remote_base::TOLERANCE_MODE_PERCENTAGE;
static constexpr uint16_t NEC_ADDRESS = 0x7F80;
static constexpr uint16_t NEC_COMMAND = 0xF20D;
static RawTimings nec_frame() {
remote_base::RemoteTransmitData dst;
remote_base::NECProtocol().encode(&dst, {.address = NEC_ADDRESS, .command = NEC_COMMAND, .command_repeats = 1});
return dst.get_data();
}
// Valid header and 31 bits, then a last bit space that is neither a one nor a zero
static RawTimings nec_near_miss_frame() {
RawTimings raw = nec_frame();
raw[raw.size() - 2] = -1100;
return raw;
}
static RawTimings rc_switch_frame() {
remote_base::RemoteTransmitData dst;
remote_base::rc_switch_protocol(1).transmit(&dst, 0x5A5A5A, 24);
return dst.get_data();
}
// 433 MHz style noise: 64 alternating marks and spaces of 100-1500 us from a fixed LCG
static RawTimings noise_frame() {
RawTimings raw;
uint32_t seed = 12345;
for (int i = 0; i < 64; i++) {
seed = seed * 1103515245 + 12345;
int32_t len = 100 + static_cast<int32_t>((seed >> 16) % 1400);
raw.push_back(i % 2 == 0 ? len : -len);
}
return raw;
}
template<typename P> static void decode_bench(benchmark::State &state, const RawTimings &raw, bool expect) {
P proto;
RemoteReceiveData check(raw, TOLERANCE, TOLERANCE_MODE);
if (proto.decode(check).has_value() != expect) {
state.SkipWithError("unexpected decode result");
return;
}
for (auto _ : state) {
for (int i = 0; i < kInnerIterations; i++) {
RemoteReceiveData src(raw, TOLERANCE, TOLERANCE_MODE);
auto res = proto.decode(src);
benchmark::DoNotOptimize(res);
}
}
state.SetItemsProcessed(state.iterations() * kInnerIterations);
}
// --- Single decoder ---
static void RemoteDecode_NEC_Match(benchmark::State &state) {
decode_bench<remote_base::NECProtocol>(state, nec_frame(), true);
}
BENCHMARK(RemoteDecode_NEC_Match);
// Rejected at the header, the common case
static void RemoteDecode_NEC_Noise(benchmark::State &state) {
decode_bench<remote_base::NECProtocol>(state, noise_frame(), false);
}
BENCHMARK(RemoteDecode_NEC_Noise);
// Rejected at the last bit, the most work a failed decode does
static void RemoteDecode_NEC_NearMiss(benchmark::State &state) {
decode_bench<remote_base::NECProtocol>(state, nec_near_miss_frame(), false);
}
BENCHMARK(RemoteDecode_NEC_NearMiss);
static void RemoteDecode_RCSwitch_Match(benchmark::State &state) {
decode_bench<remote_base::RCSwitchBase>(state, rc_switch_frame(), true);
}
BENCHMARK(RemoteDecode_RCSwitch_Match);
// Tries all eight RCSwitch timings before giving up
static void RemoteDecode_RCSwitch_Noise(benchmark::State &state) {
decode_bench<remote_base::RCSwitchBase>(state, noise_frame(), false);
}
BENCHMARK(RemoteDecode_RCSwitch_Noise);
// --- Receiver fan-out ---
class BenchReceiver : public remote_base::RemoteReceiverBase {
public:
BenchReceiver() : RemoteReceiverBase(nullptr) {}
void run(benchmark::State &state, const RawTimings &raw) {
const remote_base::RawTimingsView view(raw);
for (auto _ : state) {
for (int i = 0; i < kInnerIterations; i++)
this->call_listeners_dumpers_(view);
}
state.SetItemsProcessed(state.iterations() * kInnerIterations);
}
};
// Log formatting is measured: DEBUG level, and the Logger at baud 0 formats without writing to a console
static void dump_all_bench(benchmark::State &state, const RawTimings &raw) {
BenchReceiver receiver;
receiver.set_dumpers(DUMP_ALL, DUMP_ALL_COUNT);
receiver.run(state, raw);
}
// Every dumper tries the frame; the matching ones log it
static void RemoteDumpAll_NEC(benchmark::State &state) { dump_all_bench(state, nec_frame()); }
BENCHMARK(RemoteDumpAll_NEC);
// Pronto always decodes, so the raw fallback never runs; Pronto's text and logging are measured
static void RemoteDumpAll_Noise(benchmark::State &state) { dump_all_bench(state, noise_frame()); }
BENCHMARK(RemoteDumpAll_Noise);
// Every sensor shares the NEC address; sensor 0 has the frame's command
static void listeners_bench(benchmark::State &state, const RawTimings &raw, bool match) {
// A broken NEC round trip would otherwise quietly time eight rejections
RemoteReceiveData check(raw, TOLERANCE, TOLERANCE_MODE);
const auto decoded = remote_base::NECProtocol().decode(check);
if ((decoded.has_value() && decoded->command == NEC_COMMAND) != match) {
state.SkipWithError("unexpected decode result");
return;
}
BenchReceiver receiver;
remote_base::NECBinarySensor sensors[REMOTE_BASE_LISTENER_COUNT];
for (uint16_t i = 0; i < REMOTE_BASE_LISTENER_COUNT; i++) {
const uint16_t command = i == 0 ? NEC_COMMAND : i;
sensors[i].set_data({.address = NEC_ADDRESS, .command = command, .command_repeats = 1});
receiver.register_listener(&sensors[i]);
}
receiver.run(state, raw);
}
// Eight NEC binary sensors, none matching the noise
static void RemoteListeners_NEC8_Noise(benchmark::State &state) { listeners_bench(state, noise_frame(), false); }
BENCHMARK(RemoteListeners_NEC8_Noise);
// Every sensor decodes the full frame; one matches and pulses its state on and off
static void RemoteListeners_NEC8_Match(benchmark::State &state) { listeners_bench(state, nec_frame(), true); }
BENCHMARK(RemoteListeners_NEC8_Match);
} // namespace esphome::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
+44
View File
@@ -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