Skip to content

Commit fa66e08

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 ad9c2cc commit fa66e08

File tree

5 files changed

+38
-4
lines changed

5 files changed

+38
-4
lines changed

src/bench/bench.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@ 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+
const std::function<std::vector<const char*>()> G_TEST_COMMAND_LINE_ARGUMENTS = []() {
37+
return g_bench_command_line_args();
38+
};
3139

3240
/**
3341
* Retrieve the name of the currently in-use benchmark.
@@ -103,6 +111,14 @@ void BenchRunner::RunAll(const Args& args)
103111
std::cout << "Running with -sanity-check option, output is being suppressed as benchmark results will be useless." << std::endl;
104112
}
105113

114+
// Load inner test setup args
115+
g_bench_command_line_args = [&args]() {
116+
std::vector<const char*> ret;
117+
ret.reserve(args.setup_args.size());
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <tinyformat.h>
99
#include <util/fs.h>
1010
#include <util/string.h>
11+
#include <test/util/setup_common.h>
1112

1213
#include <chrono>
1314
#include <cstdint>
@@ -27,6 +28,7 @@ static const std::string DEFAULT_PRIORITY{"all"};
2728
static void SetupBenchArgs(ArgsManager& argsman)
2829
{
2930
SetupHelpOptions(argsman);
31+
SetupCommonTestArgs(argsman);
3032

3133
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);
3234
argsman.AddArg("-filter=<regex>", strprintf("Regular expression filter to select benchmark by name (default: %s)", DEFAULT_BENCH_FILTER), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
@@ -60,6 +62,18 @@ static uint8_t parsePriorityLevel(const std::string& str) {
6062
return levels;
6163
}
6264

65+
static std::vector<std::string> parseTestSetupArgs(const ArgsManager& argsman)
66+
{
67+
// Parses unit test framework arguments supported by the benchmark framework.
68+
std::vector<std::string> args;
69+
static std::vector<std::string> AVAILABLE_ARGS = {"-testdatadir"};
70+
for (const std::string& arg_name : AVAILABLE_ARGS) {
71+
auto op_arg = argsman.GetArg(arg_name);
72+
if (op_arg) args.emplace_back(strprintf("%s=%s", arg_name, *op_arg));
73+
}
74+
return args;
75+
}
76+
6377
int main(int argc, char** argv)
6478
{
6579
ArgsManager argsman;
@@ -131,6 +145,7 @@ int main(int argc, char** argv)
131145
args.regex_filter = argsman.GetArg("-filter", DEFAULT_BENCH_FILTER);
132146
args.sanity_check = argsman.GetBoolArg("-sanity-check", false);
133147
args.priority = parsePriorityLevel(argsman.GetArg("-priority-level", DEFAULT_PRIORITY));
148+
args.setup_args = parseTestSetupArgs(argsman);
134149

135150
benchmark::BenchRunner::RunAll(args);
136151

src/test/util/setup_common.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ struct NetworkSetup
8585
};
8686
static NetworkSetup g_networksetup_instance;
8787

88-
/** Register test-only arguments */
89-
static void SetupUnitTestArgs(ArgsManager& argsman)
88+
void SetupCommonTestArgs(ArgsManager& argsman)
9089
{
9190
argsman.AddArg("-testdatadir", strprintf("Custom data directory (default: %s<random_string>)", fs::PathToString(fs::temp_directory_path() / TEST_DIR_PATH_ELEMENT / "")),
9291
ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
@@ -125,7 +124,7 @@ BasicTestingSetup::BasicTestingSetup(const ChainType chainType, TestOpts opts)
125124
gArgs.ClearPathCache();
126125
{
127126
SetupServerArgs(*m_node.args);
128-
SetupUnitTestArgs(*m_node.args);
127+
SetupCommonTestArgs(*m_node.args);
129128
std::string error;
130129
if (!m_node.args->ParseParameters(arguments.size(), arguments.data(), error)) {
131130
m_node.args->ClearArgs();

src/test/util/setup_common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ extern const std::function<std::string()> G_TEST_GET_FULL_NAME;
4545

4646
static constexpr CAmount CENT{1000000};
4747

48+
/** Register common test args. Shared across binaries that rely on the test framework. */
49+
void SetupCommonTestArgs(ArgsManager& argsman);
50+
4851
struct TestOpts {
4952
std::vector<const char*> extra_args{};
5053
bool coins_db_in_memory{true};

0 commit comments

Comments
 (0)