From 135c599561765f0a7fa4e3b74aae885b0b52fcd3 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 21 Mar 2026 08:48:15 -1000 Subject: [PATCH] [benchmark] Pre-init APIBuffer to 1460 bytes in plaintext frame benchmarks Avoid benchmarking heap allocation by pre-reserving the buffer to typical TCP MSS size and reusing it across iterations, matching real-world usage where the buffer persists across writes. --- .../components/api/bench_plaintext_frame.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tests/benchmarks/components/api/bench_plaintext_frame.cpp b/tests/benchmarks/components/api/bench_plaintext_frame.cpp index 54e9ac8409..391361fb24 100644 --- a/tests/benchmarks/components/api/bench_plaintext_frame.cpp +++ b/tests/benchmarks/components/api/bench_plaintext_frame.cpp @@ -54,9 +54,14 @@ static void PlaintextFrame_WriteSensorState(benchmark::State &state) { auto [helper, read_fd] = create_plaintext_helper(); uint8_t padding = helper->frame_header_padding(); + // Pre-init buffer to typical TCP MSS size to avoid benchmarking + // heap allocation — in real use the buffer is reused across writes. + APIBuffer buffer; + buffer.reserve(1460); + for (auto _ : state) { for (int i = 0; i < kInnerIterations; i++) { - APIBuffer buffer; + buffer.clear(); SensorStateResponse msg; msg.key = 0x12345678; msg.state = 23.5f; @@ -89,9 +94,14 @@ static void PlaintextFrame_WriteBatch5(benchmark::State &state) { uint8_t padding = helper->frame_header_padding(); uint8_t footer = helper->frame_footer_size(); + // Pre-init buffer to typical TCP MSS size to avoid benchmarking + // heap allocation — in real use the buffer is reused across writes. + APIBuffer buffer; + buffer.reserve(1460); + for (auto _ : state) { for (int i = 0; i < kInnerIterations; i++) { - APIBuffer buffer; + buffer.clear(); MessageInfo messages[5] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}}; for (int j = 0; j < 5; j++) {