Skip to content

Commit fb5be46

Browse files
committed
bench: add support for custom data directory
Expands the benchmark framework with the existing '-testdatadir' arg, enabling the ability to change the benchmark data directory. This is useful for running benchmarks on different storage devices, and not just under the OS /tmp/ directory.
1 parent 221410b commit fb5be46

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

src/bench/bench.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,16 @@ using util::Join;
2727

2828
const std::function<void(const std::string&)> G_TEST_LOG_FUN{};
2929

30-
const std::function<std::vector<const char*>()> G_TEST_COMMAND_LINE_ARGUMENTS{};
30+
/**
31+
* Retrieves the available test setup command line arguments that may be used
32+
* in the benchmark. They will be used only if the benchmark utilizes a
33+
* 'BasicTestingSetup' or any child of it.
34+
*/
35+
static std::function<std::vector<const char*>()> g_bench_command_line_args{};
36+
37+
const std::function<std::vector<const char*>()> G_TEST_COMMAND_LINE_ARGUMENTS = []() {
38+
return g_bench_command_line_args();
39+
};
3140

3241
/**
3342
* Retrieve the name of the currently in-use benchmark.
@@ -103,6 +112,13 @@ void BenchRunner::RunAll(const Args& args)
103112
std::cout << "Running with -sanity-check option, output is being suppressed as benchmark results will be useless." << std::endl;
104113
}
105114

115+
// Load inner test setup args
116+
g_bench_command_line_args = [&args]() {
117+
std::vector<const char*> ret;
118+
for (const auto& arg : args.setup_args) ret.emplace_back(arg.c_str());
119+
return ret;
120+
};
121+
106122
std::vector<ankerl::nanobench::Result> benchmarkResults;
107123
for (const auto& [name, bench_func] : benchmarks()) {
108124
const auto& [func, priority_level] = bench_func;

src/bench/bench.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ struct Args {
6161
fs::path output_json;
6262
std::string regex_filter;
6363
uint8_t priority;
64+
std::vector<std::string> setup_args;
6465
};
6566

6667
class BenchRunner

src/bench/bench_bitcoin.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ static void SetupBenchArgs(ArgsManager& argsman)
3737
argsman.AddArg("-sanity-check", "Run benchmarks for only one iteration with no output", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
3838
argsman.AddArg("-priority-level=<l1,l2,l3>", strprintf("Run benchmarks of one or multiple priority level(s) (%s), default: '%s'",
3939
benchmark::ListPriorities(), DEFAULT_PRIORITY), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
40+
argsman.AddArg("-testdatadir", "Run benchmarks on a custom directory (default: </tmp/>)", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
4041
}
4142

4243
// parses a comma separated list like "10,20,30,50"
@@ -60,6 +61,17 @@ static uint8_t parsePriorityLevel(const std::string& str) {
6061
return levels;
6162
}
6263

64+
// Parses test setup related arguments
65+
static std::vector<std::string> parseTestSetupArgs(const ArgsManager& argsman) {
66+
std::vector<std::string> args;
67+
68+
// Test data directory
69+
fs::path test_datadir = argsman.GetPathArg("-testdatadir");
70+
if (!test_datadir.empty()) args.emplace_back(strprintf("-testdatadir=%s", PathToString(test_datadir)));
71+
72+
return args;
73+
}
74+
6375
int main(int argc, char** argv)
6476
{
6577
ArgsManager argsman;
@@ -131,6 +143,7 @@ int main(int argc, char** argv)
131143
args.regex_filter = argsman.GetArg("-filter", DEFAULT_BENCH_FILTER);
132144
args.sanity_check = argsman.GetBoolArg("-sanity-check", false);
133145
args.priority = parsePriorityLevel(argsman.GetArg("-priority-level", DEFAULT_PRIORITY));
146+
args.setup_args = parseTestSetupArgs(argsman);
134147

135148
benchmark::BenchRunner::RunAll(args);
136149

0 commit comments

Comments
 (0)