Skip to content

Commit ad9c2cc

Browse files
committed
test, bench: specialize working directory name
Since G_TEST_GET_FULL_NAME is not initialized in the benchmark framework, benchmarks using the unit test setup run in the same directory without any clear distinction between them. This poses an extra complication for locating any specific benchmark directory during a failure. In master, unit tests and benchmarks run in the following path: /<OS_tmp_dir>/test_common bitcoin/<random_uint256>/ After this commit, unit tests and benchmarks are contained within its own directory: /<OS_tmp_dir>/test_common bitcoin/<test_name>/<time_in_nanoseconds>/ This makes it easier to find any benchmark run when a failure occurs.
1 parent 2c90f8e commit ad9c2cc

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

src/bench/bench.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,16 @@ const std::function<void(const std::string&)> G_TEST_LOG_FUN{};
2929

3030
const std::function<std::vector<const char*>()> G_TEST_COMMAND_LINE_ARGUMENTS{};
3131

32-
const std::function<std::string()> G_TEST_GET_FULL_NAME{};
32+
/**
33+
* Retrieve the name of the currently in-use benchmark.
34+
* This is applicable only to benchmarks that utilize the unit test
35+
* framework context setup (e.g. ones using 'MakeNoLogFileContext<TestingSetup>()').
36+
* It places the datadir of each benchmark run within their respective benchmark name.
37+
*/
38+
static std::string g_running_benchmark_name;
39+
const std::function<std::string()> G_TEST_GET_FULL_NAME = []() {
40+
return g_running_benchmark_name;
41+
};
3342

3443
namespace {
3544

@@ -117,6 +126,7 @@ void BenchRunner::RunAll(const Args& args)
117126
bench.output(nullptr);
118127
}
119128
bench.name(name);
129+
g_running_benchmark_name = name;
120130
if (args.min_time > 0ms) {
121131
// convert to nanos before dividing to reduce rounding errors
122132
std::chrono::nanoseconds min_time_ns = args.min_time;

src/test/util/setup_common.cpp

Lines changed: 4 additions & 7 deletions
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
{
@@ -139,10 +137,10 @@ BasicTestingSetup::BasicTestingSetup(const ChainType chainType, TestOpts opts)
139137
// data directories use a random name that doesn't overlap with other tests.
140138
SeedRandomForTest(SeedRand::FIXED_SEED);
141139

140+
const std::string test_name{G_TEST_GET_FULL_NAME ? G_TEST_GET_FULL_NAME() : ""};
142141
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;
142+
const auto now{TicksSinceEpoch<std::chrono::nanoseconds>(SystemClock::now())};
143+
m_path_root = fs::temp_directory_path() / TEST_DIR_PATH_ELEMENT / test_name / util::ToString(now);
146144
TryCreateDirectories(m_path_root);
147145
} else {
148146
// Custom data directory
@@ -151,8 +149,7 @@ BasicTestingSetup::BasicTestingSetup(const ChainType chainType, TestOpts opts)
151149
if (root_dir.empty()) ExitFailure("-testdatadir argument is empty, please specify a path");
152150

153151
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);
152+
m_path_lock = root_dir / TEST_DIR_PATH_ELEMENT / fs::PathFromString(test_name);
156153
m_path_root = m_path_lock / "datadir";
157154

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

0 commit comments

Comments
 (0)