Skip to content

Commit d3c6f8b

Browse files
committed
bench: introduce -min_time argument
When it is not easily possible to stabilize benchmark machine and code the argument -min_time can be used to specify a minimum duration that a benchmark should take. E.g. choose -min_time=1000 if you are willing to wait about 1 second for each benchmark result. The default is now set to 10ms instead of 0, which should make runs on fast machines more stable with negligible slowdown.
1 parent 9fef832 commit d3c6f8b

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

src/bench/bench.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ void benchmark::BenchRunner::RunAll(const Args& args)
6161

6262
Bench bench;
6363
bench.name(p.first);
64+
if (args.min_time > 0ms) {
65+
// convert to nanos before dividing to reduce rounding errors
66+
std::chrono::nanoseconds min_time_ns = args.min_time;
67+
bench.minEpochTime(min_time_ns / bench.epochs());
68+
}
69+
6470
if (args.asymptote.empty()) {
6571
p.second(bench);
6672
} else {

src/bench/bench.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ typedef std::function<void(Bench&)> BenchFunction;
4343
struct Args {
4444
std::string regex_filter;
4545
bool is_list_only;
46+
std::chrono::milliseconds min_time;
4647
std::vector<double> asymptote;
4748
std::string output_csv;
4849
std::string output_json;

src/bench/bench_bitcoin.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <memory>
1212

1313
static const char* DEFAULT_BENCH_FILTER = ".*";
14+
static constexpr int64_t DEFAULT_MIN_TIME_MS{10};
1415

1516
static void SetupBenchArgs(ArgsManager& argsman)
1617
{
@@ -19,6 +20,7 @@ static void SetupBenchArgs(ArgsManager& argsman)
1920
argsman.AddArg("-asymptote=n1,n2,n3,...", "Test asymptotic growth of the runtime of an algorithm, if supported by the benchmark", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
2021
argsman.AddArg("-filter=<regex>", strprintf("Regular expression filter to select benchmark by name (default: %s)", DEFAULT_BENCH_FILTER), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
2122
argsman.AddArg("-list", "List benchmarks without executing them", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
23+
argsman.AddArg("-min_time=<milliseconds>", strprintf("Minimum runtime per benchmark, in milliseconds (default: %d)", DEFAULT_MIN_TIME_MS), ArgsManager::ALLOW_INT, OptionsCategory::OPTIONS);
2224
argsman.AddArg("-output_csv=<output.csv>", "Generate CSV file with the most important benchmark results", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
2325
argsman.AddArg("-output_json=<output.json>", "Generate JSON file with all benchmark results", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
2426
}
@@ -57,6 +59,7 @@ int main(int argc, char** argv)
5759
args.regex_filter = argsman.GetArg("-filter", DEFAULT_BENCH_FILTER);
5860
args.is_list_only = argsman.GetBoolArg("-list", false);
5961
args.asymptote = parseAsymptote(argsman.GetArg("-asymptote", ""));
62+
args.min_time = std::chrono::milliseconds(argsman.GetArg("-min_time", DEFAULT_MIN_TIME_MS));
6063
args.output_csv = argsman.GetArg("-output_csv", "");
6164
args.output_json = argsman.GetArg("-output_json", "");
6265

0 commit comments

Comments
 (0)