From 279686b1c28b5f12bb99842ef04fb4ea6b0e5530 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 2 Apr 2026 11:26:28 -1000 Subject: [PATCH] [scheduler] Add ExceedPool benchmark to measure pool overflow cliff Add Scheduler_SetTimeout_ExceedPool with batch size 10 (exceeding MAX_POOL_SIZE=5) to measure the performance impact when the recycling pool is exhausted and items must be malloc'd/freed each cycle. --- tests/benchmarks/core/bench_scheduler.cpp | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/benchmarks/core/bench_scheduler.cpp b/tests/benchmarks/core/bench_scheduler.cpp index 981edbe15d..c6cf7e874d 100644 --- a/tests/benchmarks/core/bench_scheduler.cpp +++ b/tests/benchmarks/core/bench_scheduler.cpp @@ -154,4 +154,29 @@ static void Scheduler_Defer(benchmark::State &state) { } BENCHMARK(Scheduler_Defer); +// --- Scheduler: set_timeout with batch size exceeding pool (cliff test) --- + +static void Scheduler_SetTimeout_ExceedPool(benchmark::State &state) { + Scheduler scheduler; + Component dummy_component; + + // Register 10 timeouts then call() — exceeds MAX_POOL_SIZE=5 to measure + // the performance cliff when the recycling pool is exhausted and items + // must be malloc'd/freed. + static constexpr int kBatchSize = 10; + for (auto _ : state) { + uint32_t now = millis(); + for (int i = 0; i < kInnerIterations; i++) { + scheduler.set_timeout(&dummy_component, static_cast(i % kBatchSize), 1000, []() {}); + if ((i + 1) % kBatchSize == 0) { + scheduler.call(++now); + } + } + scheduler.call(++now); + benchmark::DoNotOptimize(scheduler); + } + state.SetItemsProcessed(state.iterations() * kInnerIterations); +} +BENCHMARK(Scheduler_SetTimeout_ExceedPool); + } // namespace esphome::benchmarks