From: Nathan Moin Vaziri Date: Wed, 8 Apr 2026 01:10:20 +0000 (-0700) Subject: Add --benchmark_cooldown flag to mitigate thermal throttling X-Git-Url: http://git.ipfire.org/index.cgi?a=commitdiff_plain;ds=sidebyside;p=thirdparty%2Fzlib-ng.git Add --benchmark_cooldown flag to mitigate thermal throttling Adds a --benchmark_cooldown= flag that inserts a sleep between benchmark families. This helps produce consistent results on systems where sustained workloads cause thermal throttling and CPU frequency scaling. Uses a wrapping BenchmarkReporter that sleeps before forwarding results to the default display reporter. --- diff --git a/test/benchmarks/README.md b/test/benchmarks/README.md index 08ccea233..403aee9a0 100644 --- a/test/benchmarks/README.md +++ b/test/benchmarks/README.md @@ -5,7 +5,7 @@ These benchmarks are written using [Google Benchmark](https://github.com/google/ To increase the number of times each benchmark iteration is run use: -``` +```sh --benchmark_repetitions=20 ``` @@ -13,10 +13,18 @@ To increase the number of times each benchmark iteration is run use: To filter out which benchmarks are performed use: -``` +```sh --benchmark_filter="adler32*" ``` +*Cooldown* + +To insert a cooldown sleep between benchmark families to mitigate thermal throttling: + +```sh +--benchmark_cooldown=3 +``` + There are two different benchmarks, micro and macro. ### Benchmark benchmark_zlib diff --git a/test/benchmarks/benchmark_main.cc b/test/benchmarks/benchmark_main.cc index f3c227bdf..90c949d96 100644 --- a/test/benchmarks/benchmark_main.cc +++ b/test/benchmarks/benchmark_main.cc @@ -1,9 +1,19 @@ /* benchmark_main.cc -- benchmark suite main entry point - * Copyright (C) 2022 Nathan Moinvaziri + * Copyright (C) 2026 Nathan Moinvaziri * For conditions of distribution and use, see copyright notice in zlib.h */ +#include #include +#include +#include + +#ifdef _WIN32 +# define NOMINMAX +# include +#else +# include +#endif #include @@ -18,15 +28,69 @@ extern "C" { } #endif +/* Inserts a cooldown sleep between different benchmark families to mitigate + thermal throttling, but not between repetitions of the same benchmark. */ +class CooldownReporter : public benchmark::BenchmarkReporter { +public: + CooldownReporter(BenchmarkReporter *wrapped, uint32_t cooldown_secs) + : wrapped_(wrapped), cooldown_secs_(cooldown_secs), first_report_(true) {} + + bool ReportContext(const Context &context) override { + return wrapped_->ReportContext(context); + } + + void ReportRunsConfig(double min_time, bool has_explicit_iters, + benchmark::IterationCount iters) override { + wrapped_->ReportRunsConfig(min_time, has_explicit_iters, iters); + } + + void ReportRuns(const std::vector &report) override { + if (!first_report_) { +#ifdef _WIN32 + Sleep(cooldown_secs_ * 1000); +#else + sleep(cooldown_secs_); +#endif + } + first_report_ = false; + wrapped_->ReportRuns(report); + } + + void Finalize() override { + wrapped_->Finalize(); + } + +private: + BenchmarkReporter *wrapped_; + uint32_t cooldown_secs_; + bool first_report_; +}; + int main(int argc, char** argv) { + uint32_t cooldown_secs = 0; + #ifndef BUILD_ALT # ifndef DISABLE_RUNTIME_CPU_DETECTION cpu_check_features(&test_cpu_features); # endif #endif + for (int i = 1; i < argc; i++) { + if (strncmp(argv[i], "--benchmark_cooldown=", 21) == 0) { + cooldown_secs = strtoul(argv[i] + 21, nullptr, 10); + break; + } + } + ::benchmark::Initialize(&argc, argv); - ::benchmark::RunSpecifiedBenchmarks(); + + if (cooldown_secs > 0) { + benchmark::BenchmarkReporter *display = benchmark::CreateDefaultDisplayReporter(); + CooldownReporter cooldown_display(display, cooldown_secs); + ::benchmark::RunSpecifiedBenchmarks(&cooldown_display); + } else { + ::benchmark::RunSpecifiedBenchmarks(); + } return EXIT_SUCCESS; }