Skip to content

Commit 4228259

Browse files
committed
Merge bitcoin#31000: bench: add support for custom data directory
fa66e08 bench: add support for custom data directory (furszy) ad9c2cc test, bench: specialize working directory name (furszy) Pull request description: 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. A good use case is bitcoin#28574, where we are benchmarking the wallet migration process on an HDD. ACKs for top commit: maflcko: re-ACK fa66e08 achow101: ACK fa66e08 tdb3: re ACK fa66e08 hodlinator: re-ACK fa66e08 pablomartin4btc: re-ACK fa66e08 Tree-SHA512: 4e87206c07e26fe193c07074ae9eb0cc9c70a58aeea8cf27d18fb5425d77e4b00dbe0e6d6a75c17b427744e9066458b9a84e5ef7b0420f02a4fccb9c5ef4dacc
2 parents 36f5eff + fa66e08 commit 4228259

File tree

5 files changed

+53
-12
lines changed

5 files changed

+53
-12
lines changed

src/bench/bench.cpp

+28-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,26 @@ 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

32-
const std::function<std::string()> G_TEST_GET_FULL_NAME{};
40+
/**
41+
* Retrieve the name of the currently in-use benchmark.
42+
* This is applicable only to benchmarks that utilize the unit test
43+
* framework context setup (e.g. ones using 'MakeNoLogFileContext<TestingSetup>()').
44+
* It places the datadir of each benchmark run within their respective benchmark name.
45+
*/
46+
static std::string g_running_benchmark_name;
47+
const std::function<std::string()> G_TEST_GET_FULL_NAME = []() {
48+
return g_running_benchmark_name;
49+
};
3350

3451
namespace {
3552

@@ -94,6 +111,14 @@ void BenchRunner::RunAll(const Args& args)
94111
std::cout << "Running with -sanity-check option, output is being suppressed as benchmark results will be useless." << std::endl;
95112
}
96113

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+
97122
std::vector<ankerl::nanobench::Result> benchmarkResults;
98123
for (const auto& [name, bench_func] : benchmarks()) {
99124
const auto& [func, priority_level] = bench_func;
@@ -117,6 +142,7 @@ void BenchRunner::RunAll(const Args& args)
117142
bench.output(nullptr);
118143
}
119144
bench.name(name);
145+
g_running_benchmark_name = name;
120146
if (args.min_time > 0ms) {
121147
// convert to nanos before dividing to reduce rounding errors
122148
std::chrono::nanoseconds min_time_ns = args.min_time;

src/bench/bench.h

+1
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

+15
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

+6-10
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ using node::VerifyLoadedChainstate;
7575
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
7676

7777
constexpr inline auto TEST_DIR_PATH_ELEMENT{"test_common bitcoin"}; // Includes a space to catch possible path escape issues.
78-
/** Random context to get unique temp data dirs. Separate from m_rng, which can be seeded from a const env var */
79-
static FastRandomContext g_rng_temp_path;
8078

8179
struct NetworkSetup
8280
{
@@ -87,8 +85,7 @@ struct NetworkSetup
8785
};
8886
static NetworkSetup g_networksetup_instance;
8987

90-
/** Register test-only arguments */
91-
static void SetupUnitTestArgs(ArgsManager& argsman)
88+
void SetupCommonTestArgs(ArgsManager& argsman)
9289
{
9390
argsman.AddArg("-testdatadir", strprintf("Custom data directory (default: %s<random_string>)", fs::PathToString(fs::temp_directory_path() / TEST_DIR_PATH_ELEMENT / "")),
9491
ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
@@ -127,7 +124,7 @@ BasicTestingSetup::BasicTestingSetup(const ChainType chainType, TestOpts opts)
127124
gArgs.ClearPathCache();
128125
{
129126
SetupServerArgs(*m_node.args);
130-
SetupUnitTestArgs(*m_node.args);
127+
SetupCommonTestArgs(*m_node.args);
131128
std::string error;
132129
if (!m_node.args->ParseParameters(arguments.size(), arguments.data(), error)) {
133130
m_node.args->ClearArgs();
@@ -139,10 +136,10 @@ BasicTestingSetup::BasicTestingSetup(const ChainType chainType, TestOpts opts)
139136
// data directories use a random name that doesn't overlap with other tests.
140137
SeedRandomForTest(SeedRand::FIXED_SEED);
141138

139+
const std::string test_name{G_TEST_GET_FULL_NAME ? G_TEST_GET_FULL_NAME() : ""};
142140
if (!m_node.args->IsArgSet("-testdatadir")) {
143-
// By default, the data directory has a random name
144-
const auto rand_str{g_rng_temp_path.rand256().ToString()};
145-
m_path_root = fs::temp_directory_path() / TEST_DIR_PATH_ELEMENT / rand_str;
141+
const auto now{TicksSinceEpoch<std::chrono::nanoseconds>(SystemClock::now())};
142+
m_path_root = fs::temp_directory_path() / TEST_DIR_PATH_ELEMENT / test_name / util::ToString(now);
146143
TryCreateDirectories(m_path_root);
147144
} else {
148145
// Custom data directory
@@ -151,8 +148,7 @@ BasicTestingSetup::BasicTestingSetup(const ChainType chainType, TestOpts opts)
151148
if (root_dir.empty()) ExitFailure("-testdatadir argument is empty, please specify a path");
152149

153150
root_dir = fs::absolute(root_dir);
154-
const std::string test_path{G_TEST_GET_FULL_NAME ? G_TEST_GET_FULL_NAME() : ""};
155-
m_path_lock = root_dir / TEST_DIR_PATH_ELEMENT / fs::PathFromString(test_path);
151+
m_path_lock = root_dir / TEST_DIR_PATH_ELEMENT / fs::PathFromString(test_name);
156152
m_path_root = m_path_lock / "datadir";
157153

158154
// Try to obtain the lock; if unsuccessful don't disturb the existing test.

src/test/util/setup_common.h

+3
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)