[core] Add CodSpeed C++ benchmarks for protobuf and main loop

Add automated benchmarks using Google Benchmark to prevent performance
regressions in the API protobuf encoding/decoding and core loop paths.

Benchmarks cover:
- Protobuf encode: SensorState, BinarySensorState, HelloResponse,
  LightState, DeviceInfoResponse (20 nested devices + 20 areas)
- Protobuf decode: HelloRequest, SwitchCommand, LightCommand
- Protobuf calculate_size and full calc+encode send path
- Varint parse/encode/size for various value ranges
- Scheduler call/next_schedule_in with idle and active timers
- Application loop component dispatch and blocking guard overhead

Infrastructure:
- Extract shared build logic from cpp_unit_test.py into test_helpers.py
- Add cpp_benchmark.py mirroring the unit test build pattern
- Support benchmark.yaml per component dir for declaring dependencies
- Add CodSpeed CI workflow triggered on api/core changes
- Fix ProtoMessage protected destructor on host platform
This commit is contained in:
J. Nick Koston
2026-03-16 19:25:46 -10:00
parent bba11b3b1e
commit 38ff332fec
13 changed files with 1280 additions and 214 deletions
+5
View File
@@ -0,0 +1,5 @@
# Gitignore settings for ESPHome
# This is an example and may include too much for your use-case.
# You can modify this file to suit your needs.
/.esphome/
/secrets.yaml
@@ -0,0 +1,50 @@
#include <benchmark/benchmark.h>
#include "esphome/core/application.h"
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
namespace esphome::benchmarks {
// A minimal component with an empty loop for benchmarking dispatch overhead
class NoopComponent : public Component {
public:
void loop() override {}
float get_setup_priority() const override { return 0.0f; }
// Expose protected method for benchmarking
void set_loop_state() { this->set_component_state_(COMPONENT_STATE_LOOP); }
};
// --- WarnIfComponentBlockingGuard overhead ---
static void BM_WarnIfComponentBlockingGuard(benchmark::State &state) {
NoopComponent component;
uint32_t now = millis();
for (auto _ : state) {
WarnIfComponentBlockingGuard guard{&component, now};
now = guard.finish();
benchmark::DoNotOptimize(now);
}
}
BENCHMARK(BM_WarnIfComponentBlockingGuard);
// --- Component virtual dispatch overhead ---
static void BM_ComponentDispatch(benchmark::State &state) {
NoopComponent component;
component.set_loop_state();
uint32_t now = millis();
for (auto _ : state) {
{
WarnIfComponentBlockingGuard guard{&component, now};
component.loop();
now = guard.finish();
}
benchmark::DoNotOptimize(now);
}
}
BENCHMARK(BM_ComponentDispatch);
} // namespace esphome::benchmarks
@@ -0,0 +1,119 @@
#include <benchmark/benchmark.h>
#include "esphome/components/api/api_pb2.h"
#include "esphome/components/api/api_buffer.h"
namespace esphome::api::benchmarks {
// --- HelloRequest decode (string + varint fields) ---
static void BM_Decode_HelloRequest(benchmark::State &state) {
// Manually encoded HelloRequest:
// field 1 (string): "aioesphomeapi"
// field 2 (varint): 1 (api_version_major)
// field 3 (varint): 10 (api_version_minor)
uint8_t encoded[] = {
0x0A, 0x0D, // field 1, length 13
'a', 'i', 'o', 'e', 's', 'p', 'h', 'o', 'm', 'e', 'a', 'p', 'i', // "aioesphomeapi"
0x10, 0x01, // field 2, value 1
0x18, 0x0A, // field 3, value 10
};
for (auto _ : state) {
HelloRequest msg;
msg.decode(encoded, sizeof(encoded));
benchmark::DoNotOptimize(msg.api_version_major);
}
}
BENCHMARK(BM_Decode_HelloRequest);
// --- SwitchCommandRequest decode (simple command) ---
static void BM_Decode_SwitchCommandRequest(benchmark::State &state) {
// field 1 (fixed32): key = 0x12345678
// field 2 (varint): state = true
uint8_t encoded[] = {
0x0D, 0x78, 0x56, 0x34, 0x12, // field 1, fixed32
0x10, 0x01, // field 2, varint true
};
for (auto _ : state) {
SwitchCommandRequest msg;
msg.decode(encoded, sizeof(encoded));
benchmark::DoNotOptimize(msg.state);
}
}
BENCHMARK(BM_Decode_SwitchCommandRequest);
// --- LightCommandRequest decode (complex command with many fields) ---
static void BM_Decode_LightCommandRequest(benchmark::State &state) {
uint8_t encoded[] = {
// field 1: key (fixed32) = 0x11223344
0x0D,
0x44,
0x33,
0x22,
0x11,
// field 2: has_state (varint) = true
0x10,
0x01,
// field 3: state (varint) = true
0x18,
0x01,
// field 4: has_brightness (varint) = true
0x20,
0x01,
// field 5: brightness (fixed32/float) = 0.8
0x2D,
0xCD,
0xCC,
0x4C,
0x3F,
// field 9: has_rgb (varint) = true
0x48,
0x01,
// field 10: red (fixed32/float) = 1.0
0x55,
0x00,
0x00,
0x80,
0x3F,
// field 11: green (fixed32/float) = 0.5
0x5D,
0x00,
0x00,
0x00,
0x3F,
// field 12: blue (fixed32/float) = 0.2
0x65,
0xCD,
0xCC,
0x4C,
0x3E,
// field 20: has_effect (varint) = true
0xA0,
0x01,
0x01,
// field 21: effect (string) = "rainbow"
0xAA,
0x01,
0x07,
'r',
'a',
'i',
'n',
'b',
'o',
'w',
};
for (auto _ : state) {
LightCommandRequest msg;
msg.decode(encoded, sizeof(encoded));
benchmark::DoNotOptimize(msg.brightness);
}
}
BENCHMARK(BM_Decode_LightCommandRequest);
} // namespace esphome::api::benchmarks
@@ -0,0 +1,210 @@
#include <benchmark/benchmark.h>
#include "esphome/components/api/api_pb2.h"
#include "esphome/components/api/api_buffer.h"
namespace esphome::api::benchmarks {
// --- SensorStateResponse (highest frequency message) ---
static void BM_Encode_SensorStateResponse(benchmark::State &state) {
APIBuffer buffer;
SensorStateResponse msg;
msg.key = 0x12345678;
msg.state = 23.5f;
msg.missing_state = false;
uint32_t size = msg.calculate_size();
buffer.resize(size);
for (auto _ : state) {
ProtoWriteBuffer writer(&buffer, 0);
msg.encode(writer);
benchmark::DoNotOptimize(buffer.data());
}
}
BENCHMARK(BM_Encode_SensorStateResponse);
static void BM_CalculateSize_SensorStateResponse(benchmark::State &state) {
SensorStateResponse msg;
msg.key = 0x12345678;
msg.state = 23.5f;
msg.missing_state = false;
for (auto _ : state) {
benchmark::DoNotOptimize(msg.calculate_size());
}
}
BENCHMARK(BM_CalculateSize_SensorStateResponse);
static void BM_CalcAndEncode_SensorStateResponse(benchmark::State &state) {
APIBuffer buffer;
SensorStateResponse msg;
msg.key = 0x12345678;
msg.state = 23.5f;
msg.missing_state = false;
for (auto _ : state) {
uint32_t size = msg.calculate_size();
buffer.resize(size);
ProtoWriteBuffer writer(&buffer, 0);
msg.encode(writer);
benchmark::DoNotOptimize(buffer.data());
}
}
BENCHMARK(BM_CalcAndEncode_SensorStateResponse);
// --- BinarySensorStateResponse ---
static void BM_Encode_BinarySensorStateResponse(benchmark::State &state) {
APIBuffer buffer;
BinarySensorStateResponse msg;
msg.key = 0xAABBCCDD;
msg.state = true;
msg.missing_state = false;
uint32_t size = msg.calculate_size();
buffer.resize(size);
for (auto _ : state) {
ProtoWriteBuffer writer(&buffer, 0);
msg.encode(writer);
benchmark::DoNotOptimize(buffer.data());
}
}
BENCHMARK(BM_Encode_BinarySensorStateResponse);
// --- HelloResponse (string fields) ---
static void BM_Encode_HelloResponse(benchmark::State &state) {
APIBuffer buffer;
HelloResponse msg;
msg.api_version_major = 1;
msg.api_version_minor = 10;
msg.server_info = StringRef::from_lit("esphome v2026.3.0");
msg.name = StringRef::from_lit("living-room-sensor");
uint32_t size = msg.calculate_size();
buffer.resize(size);
for (auto _ : state) {
ProtoWriteBuffer writer(&buffer, 0);
msg.encode(writer);
benchmark::DoNotOptimize(buffer.data());
}
}
BENCHMARK(BM_Encode_HelloResponse);
// --- LightStateResponse (complex multi-field message) ---
static void BM_Encode_LightStateResponse(benchmark::State &state) {
APIBuffer buffer;
LightStateResponse msg;
msg.key = 0x11223344;
msg.state = true;
msg.brightness = 0.8f;
msg.color_mode = enums::COLOR_MODE_RGB_WHITE;
msg.color_brightness = 1.0f;
msg.red = 1.0f;
msg.green = 0.5f;
msg.blue = 0.2f;
msg.white = 0.0f;
msg.color_temperature = 4000.0f;
msg.cold_white = 0.0f;
msg.warm_white = 0.0f;
msg.effect = StringRef::from_lit("rainbow");
uint32_t size = msg.calculate_size();
buffer.resize(size);
for (auto _ : state) {
ProtoWriteBuffer writer(&buffer, 0);
msg.encode(writer);
benchmark::DoNotOptimize(buffer.data());
}
}
BENCHMARK(BM_Encode_LightStateResponse);
static void BM_CalculateSize_LightStateResponse(benchmark::State &state) {
LightStateResponse msg;
msg.key = 0x11223344;
msg.state = true;
msg.brightness = 0.8f;
msg.color_mode = enums::COLOR_MODE_RGB_WHITE;
msg.color_brightness = 1.0f;
msg.red = 1.0f;
msg.green = 0.5f;
msg.blue = 0.2f;
msg.white = 0.0f;
msg.color_temperature = 4000.0f;
msg.cold_white = 0.0f;
msg.warm_white = 0.0f;
msg.effect = StringRef::from_lit("rainbow");
for (auto _ : state) {
benchmark::DoNotOptimize(msg.calculate_size());
}
}
BENCHMARK(BM_CalculateSize_LightStateResponse);
// --- DeviceInfoResponse (nested submessages: 20 devices + 20 areas) ---
static DeviceInfoResponse make_device_info_response() {
DeviceInfoResponse msg;
msg.name = StringRef::from_lit("living-room-sensor");
msg.mac_address = StringRef::from_lit("AA:BB:CC:DD:EE:FF");
msg.esphome_version = StringRef::from_lit("2026.3.0");
msg.compilation_time = StringRef::from_lit("Mar 16 2026, 12:00:00");
msg.model = StringRef::from_lit("esp32-poe-iso");
msg.manufacturer = StringRef::from_lit("Olimex");
msg.friendly_name = StringRef::from_lit("Living Room Sensor");
#ifdef USE_DEVICES
for (uint32_t i = 0; i < ESPHOME_DEVICE_COUNT && i < 20; i++) {
msg.devices[i].device_id = i + 1;
msg.devices[i].name = StringRef::from_lit("device");
msg.devices[i].area_id = (i % 20) + 1;
}
#endif
#ifdef USE_AREAS
for (uint32_t i = 0; i < ESPHOME_AREA_COUNT && i < 20; i++) {
msg.areas[i].area_id = i + 1;
msg.areas[i].name = StringRef::from_lit("area");
}
#endif
return msg;
}
static void BM_CalculateSize_DeviceInfoResponse(benchmark::State &state) {
auto msg = make_device_info_response();
for (auto _ : state) {
benchmark::DoNotOptimize(msg.calculate_size());
}
}
BENCHMARK(BM_CalculateSize_DeviceInfoResponse);
static void BM_Encode_DeviceInfoResponse(benchmark::State &state) {
auto msg = make_device_info_response();
APIBuffer buffer;
uint32_t total_size = msg.calculate_size();
buffer.resize(total_size);
for (auto _ : state) {
ProtoWriteBuffer writer(&buffer, 0);
msg.encode(writer);
benchmark::DoNotOptimize(buffer.data());
}
}
BENCHMARK(BM_Encode_DeviceInfoResponse);
static void BM_CalcAndEncode_DeviceInfoResponse(benchmark::State &state) {
auto msg = make_device_info_response();
APIBuffer buffer;
for (auto _ : state) {
uint32_t size = msg.calculate_size();
buffer.resize(size);
ProtoWriteBuffer writer(&buffer, 0);
msg.encode(writer);
benchmark::DoNotOptimize(buffer.data());
}
}
BENCHMARK(BM_CalcAndEncode_DeviceInfoResponse);
} // namespace esphome::api::benchmarks
@@ -0,0 +1,99 @@
#include <benchmark/benchmark.h>
#include "esphome/components/api/proto.h"
#include "esphome/components/api/api_buffer.h"
namespace esphome::api::benchmarks {
// --- ProtoVarInt::parse() benchmarks ---
static void BM_ProtoVarInt_Parse_SingleByte(benchmark::State &state) {
// Single-byte varint (0-127) — the most common case (fast path)
uint8_t buf[] = {0x42}; // value = 66
for (auto _ : state) {
auto result = ProtoVarInt::parse(buf, sizeof(buf));
benchmark::DoNotOptimize(result);
}
}
BENCHMARK(BM_ProtoVarInt_Parse_SingleByte);
static void BM_ProtoVarInt_Parse_TwoByte(benchmark::State &state) {
// Two-byte varint (128-16383)
uint8_t buf[] = {0x80, 0x01}; // value = 128
for (auto _ : state) {
auto result = ProtoVarInt::parse(buf, sizeof(buf));
benchmark::DoNotOptimize(result);
}
}
BENCHMARK(BM_ProtoVarInt_Parse_TwoByte);
static void BM_ProtoVarInt_Parse_FiveByte(benchmark::State &state) {
// Five-byte varint (max uint32 = 4294967295)
uint8_t buf[] = {0xFF, 0xFF, 0xFF, 0xFF, 0x0F};
for (auto _ : state) {
auto result = ProtoVarInt::parse(buf, sizeof(buf));
benchmark::DoNotOptimize(result);
}
}
BENCHMARK(BM_ProtoVarInt_Parse_FiveByte);
// --- Varint encoding benchmarks ---
static void BM_Encode_Varint_Small(benchmark::State &state) {
// Value < 128 — single byte fast path
APIBuffer buffer;
buffer.resize(16);
for (auto _ : state) {
ProtoWriteBuffer writer(&buffer, 0);
writer.encode_varint_raw(42);
benchmark::DoNotOptimize(buffer.data());
}
}
BENCHMARK(BM_Encode_Varint_Small);
static void BM_Encode_Varint_Large(benchmark::State &state) {
// Value > 128 — multi-byte slow path
APIBuffer buffer;
buffer.resize(16);
for (auto _ : state) {
ProtoWriteBuffer writer(&buffer, 0);
writer.encode_varint_raw(300);
benchmark::DoNotOptimize(buffer.data());
}
}
BENCHMARK(BM_Encode_Varint_Large);
static void BM_Encode_Varint_MaxUint32(benchmark::State &state) {
APIBuffer buffer;
buffer.resize(16);
for (auto _ : state) {
ProtoWriteBuffer writer(&buffer, 0);
writer.encode_varint_raw(0xFFFFFFFF);
benchmark::DoNotOptimize(buffer.data());
}
}
BENCHMARK(BM_Encode_Varint_MaxUint32);
// --- ProtoSize::varint() benchmarks ---
static void BM_ProtoSize_Varint_Small(benchmark::State &state) {
for (auto _ : state) {
benchmark::DoNotOptimize(ProtoSize::varint(42));
}
}
BENCHMARK(BM_ProtoSize_Varint_Small);
static void BM_ProtoSize_Varint_Large(benchmark::State &state) {
for (auto _ : state) {
benchmark::DoNotOptimize(ProtoSize::varint(0xFFFFFFFF));
}
}
BENCHMARK(BM_ProtoSize_Varint_Large);
} // namespace esphome::api::benchmarks
@@ -0,0 +1,63 @@
#include <benchmark/benchmark.h>
#include "esphome/core/scheduler.h"
#include "esphome/core/hal.h"
namespace esphome::benchmarks {
// --- Scheduler fast path: no work to do ---
static void BM_Scheduler_Call_NoWork(benchmark::State &state) {
Scheduler scheduler;
uint32_t now = millis();
for (auto _ : state) {
scheduler.call(now);
benchmark::DoNotOptimize(now);
}
}
BENCHMARK(BM_Scheduler_Call_NoWork);
// --- Scheduler with timers: call() when timers exist but aren't due ---
static void BM_Scheduler_Call_TimersNotDue(benchmark::State &state) {
Scheduler scheduler;
Component dummy_component;
// Add some timeouts far in the future
for (int i = 0; i < 10; i++) {
scheduler.set_timeout(&dummy_component, static_cast<uint32_t>(i), 1000000, []() {});
}
scheduler.process_to_add();
uint32_t now = millis();
for (auto _ : state) {
scheduler.call(now);
benchmark::DoNotOptimize(now);
}
}
BENCHMARK(BM_Scheduler_Call_TimersNotDue);
// --- Scheduler: next_schedule_in() calculation ---
static void BM_Scheduler_NextScheduleIn(benchmark::State &state) {
Scheduler scheduler;
Component dummy_component;
// Add some timeouts
for (int i = 0; i < 10; i++) {
scheduler.set_timeout(&dummy_component, static_cast<uint32_t>(i), 1000 * (i + 1), []() {});
}
scheduler.process_to_add();
uint32_t now = millis();
for (auto _ : state) {
auto result = scheduler.next_schedule_in(now);
benchmark::DoNotOptimize(result);
}
}
BENCHMARK(BM_Scheduler_NextScheduleIn);
} // namespace esphome::benchmarks
@@ -0,0 +1,114 @@
# Components needed for API protobuf benchmarks.
# Merged into the base config before validation so all
# dependencies get proper defaults.
#
# esphome: sub-keys are merged into the base config.
esphome:
areas:
- id: area_1
name: "Area 1"
- id: area_2
name: "Area 2"
- id: area_3
name: "Area 3"
- id: area_4
name: "Area 4"
- id: area_5
name: "Area 5"
- id: area_6
name: "Area 6"
- id: area_7
name: "Area 7"
- id: area_8
name: "Area 8"
- id: area_9
name: "Area 9"
- id: area_10
name: "Area 10"
- id: area_11
name: "Area 11"
- id: area_12
name: "Area 12"
- id: area_13
name: "Area 13"
- id: area_14
name: "Area 14"
- id: area_15
name: "Area 15"
- id: area_16
name: "Area 16"
- id: area_17
name: "Area 17"
- id: area_18
name: "Area 18"
- id: area_19
name: "Area 19"
- id: area_20
name: "Area 20"
devices:
- id: device_1
name: "Device 1"
area_id: area_1
- id: device_2
name: "Device 2"
area_id: area_2
- id: device_3
name: "Device 3"
area_id: area_3
- id: device_4
name: "Device 4"
area_id: area_4
- id: device_5
name: "Device 5"
area_id: area_5
- id: device_6
name: "Device 6"
area_id: area_6
- id: device_7
name: "Device 7"
area_id: area_7
- id: device_8
name: "Device 8"
area_id: area_8
- id: device_9
name: "Device 9"
area_id: area_9
- id: device_10
name: "Device 10"
area_id: area_10
- id: device_11
name: "Device 11"
area_id: area_11
- id: device_12
name: "Device 12"
area_id: area_12
- id: device_13
name: "Device 13"
area_id: area_13
- id: device_14
name: "Device 14"
area_id: area_14
- id: device_15
name: "Device 15"
area_id: area_15
- id: device_16
name: "Device 16"
area_id: area_16
- id: device_17
name: "Device 17"
area_id: area_17
- id: device_18
name: "Device 18"
area_id: area_18
- id: device_19
name: "Device 19"
area_id: area_19
- id: device_20
name: "Device 20"
area_id: area_20
api:
sensor:
binary_sensor:
light:
switch:
+38
View File
@@ -0,0 +1,38 @@
#include <benchmark/benchmark.h>
#include "esphome/components/logger/logger.h"
/*
This special main.cpp provides the entry point for Google Benchmark.
It replaces the default ESPHome main with a benchmark runner.
See the codspeed_plan.md for more information.
*/
// Auto generated code by esphome
// ========== AUTO GENERATED INCLUDE BLOCK BEGIN ===========
// ========== AUTO GENERATED INCLUDE BLOCK END ==========="
void original_setup() {
// This function won't be run.
// ========== AUTO GENERATED CODE BEGIN ===========
// =========== AUTO GENERATED CODE END ============
}
void setup() {
// Log functions call global_logger->log_vprintf_() without a null check,
// so we must set up a Logger before any test that triggers logging.
static esphome::logger::Logger test_logger(0);
test_logger.set_log_level(ESPHOME_LOG_LEVEL);
test_logger.pre_setup();
int argc = 1;
char arg0[] = "benchmark";
char *argv[] = {arg0, nullptr};
::benchmark::Initialize(&argc, argv);
::benchmark::RunSpecifiedBenchmarks();
::benchmark::Shutdown();
exit(0);
}
void loop() {}