From d8033c13257611201c47e661c549fd31bb203eb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Fri, 14 Aug 2026 00:28:38 +0200 Subject: [PATCH] test: Use the range-based loop in all benchmarks Replace the 8 KeepRunningBatch() loops with the range-based one. The batch size never drove the loop body, it only told google/benchmark to divide the reported time by it, so the executed work is unchanged and only the Time column changes from per item to per loop iteration. The per-item time is now reported explicitly by the avg_time_per_item counter, and the precompiles report avg_gas_used per input to match it: avg_gas_used / avg_time_per_input is exactly gas_rate. Uniform loops also keep every benchmark measurable by the tools instrumenting the range-based loop only. --- test/internal_benchmarks/evmmax_bench.cpp | 15 ++++++++++++--- test/internal_benchmarks/find_jumpdest_bench.cpp | 15 ++++++++++++--- test/internal_benchmarks/lru_cache_bench.cpp | 5 ++++- test/precompiles_bench/precompiles_bench.cpp | 7 +++++-- 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/test/internal_benchmarks/evmmax_bench.cpp b/test/internal_benchmarks/evmmax_bench.cpp index 7f8f7b31d4..d16529f06d 100644 --- a/test/internal_benchmarks/evmmax_bench.cpp +++ b/test/internal_benchmarks/evmmax_bench.cpp @@ -19,11 +19,14 @@ void evmmax_add(benchmark::State& state) auto a = Mod / 2; auto b = Mod / 3; - while (state.KeepRunningBatch(2)) + for ([[maybe_unused]] auto _ : state) { a = m.add(a, b); b = m.add(b, a); } + state.counters["avg_time_per_item"] = + benchmark::Counter(2.0 * static_cast(state.iterations()), + benchmark::Counter::kIsRate | benchmark::Counter::kInvert); } template @@ -33,11 +36,14 @@ void evmmax_sub(benchmark::State& state) auto a = Mod / 2; auto b = Mod / 3; - while (state.KeepRunningBatch(2)) + for ([[maybe_unused]] auto _ : state) { a = m.sub(a, b); b = m.sub(b, a); } + state.counters["avg_time_per_item"] = + benchmark::Counter(2.0 * static_cast(state.iterations()), + benchmark::Counter::kIsRate | benchmark::Counter::kInvert); } template @@ -47,11 +53,14 @@ void evmmax_mul(benchmark::State& state) auto a = m.to_mont(Mod / 2); auto b = m.to_mont(Mod / 3); - while (state.KeepRunningBatch(2)) + for ([[maybe_unused]] auto _ : state) { a = m.mul(a, b); b = m.mul(b, a); } + state.counters["avg_time_per_item"] = + benchmark::Counter(2.0 * static_cast(state.iterations()), + benchmark::Counter::kIsRate | benchmark::Counter::kInvert); } } // namespace diff --git a/test/internal_benchmarks/find_jumpdest_bench.cpp b/test/internal_benchmarks/find_jumpdest_bench.cpp index 337991145b..377b17c0a2 100644 --- a/test/internal_benchmarks/find_jumpdest_bench.cpp +++ b/test/internal_benchmarks/find_jumpdest_bench.cpp @@ -159,7 +159,7 @@ void find_jumpdest_random(benchmark::State& state) const auto begin = map.data(); benchmark::ClobberMemory(); - while (state.KeepRunningBatch(indexes.size())) + for ([[maybe_unused]] auto _ : state) { for (auto i : indexes) { @@ -167,6 +167,9 @@ void find_jumpdest_random(benchmark::State& state) benchmark::DoNotOptimize(x); } } + state.counters["avg_time_per_item"] = benchmark::Counter( + static_cast(indexes.size()) * static_cast(state.iterations()), + benchmark::Counter::kIsRate | benchmark::Counter::kInvert); } BENCHMARK_TEMPLATE(find_jumpdest_random, int, linear); @@ -269,7 +272,7 @@ void find_jumpdest_split_random(benchmark::State& state) const auto value = map.value.data(); benchmark::ClobberMemory(); - while (state.KeepRunningBatch(indexes.size())) + for ([[maybe_unused]] auto _ : state) { for (auto i : indexes) { @@ -277,6 +280,9 @@ void find_jumpdest_split_random(benchmark::State& state) benchmark::DoNotOptimize(x); } } + state.counters["avg_time_per_item"] = benchmark::Counter( + static_cast(indexes.size()) * static_cast(state.iterations()), + benchmark::Counter::kIsRate | benchmark::Counter::kInvert); } BENCHMARK_TEMPLATE(find_jumpdest_split, uint16_t, lower_bound) ARGS; @@ -294,7 +300,7 @@ void find_jumpdest_hashmap_random(benchmark::State& state) const auto hashmap = std::unordered_map{map.begin(), map.end()}; benchmark::ClobberMemory(); - while (state.KeepRunningBatch(indexes.size())) + for ([[maybe_unused]] auto _ : state) { for (auto i : indexes) { @@ -303,6 +309,9 @@ void find_jumpdest_hashmap_random(benchmark::State& state) benchmark::DoNotOptimize(x); } } + state.counters["avg_time_per_item"] = benchmark::Counter( + static_cast(indexes.size()) * static_cast(state.iterations()), + benchmark::Counter::kIsRate | benchmark::Counter::kInvert); } BENCHMARK_TEMPLATE(find_jumpdest_hashmap_random, int); diff --git a/test/internal_benchmarks/lru_cache_bench.cpp b/test/internal_benchmarks/lru_cache_bench.cpp index 1e332e8bdc..35acda0b7a 100644 --- a/test/internal_benchmarks/lru_cache_bench.cpp +++ b/test/internal_benchmarks/lru_cache_bench.cpp @@ -108,7 +108,7 @@ void lru_cache_put_empty(benchmark::State& state) data[i] = static_cast(i); benchmark::ClobberMemory(); - while (state.KeepRunningBatch(static_cast(capacity))) + for ([[maybe_unused]] auto _ : state) { for (const auto& key : data) { @@ -118,6 +118,9 @@ void lru_cache_put_empty(benchmark::State& state) cache.clear(); state.ResumeTiming(); } + state.counters["avg_time_per_item"] = + benchmark::Counter(static_cast(capacity) * static_cast(state.iterations()), + benchmark::Counter::kIsRate | benchmark::Counter::kInvert); } BENCHMARK(lru_cache_put_empty)->Arg(5000); BENCHMARK(lru_cache_put_empty>)->Arg(5000); diff --git a/test/precompiles_bench/precompiles_bench.cpp b/test/precompiles_bench/precompiles_bench.cpp index 2df05ce2bd..6cd50b75c8 100644 --- a/test/precompiles_bench/precompiles_bench.cpp +++ b/test/precompiles_bench/precompiles_bench.cpp @@ -213,7 +213,7 @@ void precompile(benchmark::State& state) int64_t total_gas_used = 0; - while (state.KeepRunningBatch(inputs.size())) + for ([[maybe_unused]] auto _ : state) { for (const auto& input : inputs) { @@ -228,7 +228,10 @@ void precompile(benchmark::State& state) } using benchmark::Counter; - state.counters["gas_used"] = Counter(static_cast(batch_gas_cost)); + const auto num_inputs = static_cast(inputs.size()); + state.counters["avg_time_per_input"] = Counter( + num_inputs * static_cast(state.iterations()), Counter::kIsRate | Counter::kInvert); + state.counters["avg_gas_used"] = Counter(static_cast(batch_gas_cost) / num_inputs); state.counters["gas_rate"] = Counter(static_cast(total_gas_used), Counter::kIsRate); }