Files
esphome/tests/benchmarks/components/core/bench_scheduler.cpp
T
J. Nick Koston 9c148da76a Add core benchmarks dir, fix core pseudo-component, use simulation mode
- Move scheduler/loop/helpers benchmarks to tests/benchmarks/components/core/
- Add random_float and random_uint32 benchmarks (from ol.yaml)
- Fix core pseudo-component crash: skip components where get_component()
  returns None when adding dependencies to config
- Use CodSpeed simulation mode (CPU instruction counting) for reproducible
  CI results instead of walltime
2026-03-16 20:49:48 -10:00

64 lines
1.5 KiB
C++

#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