elements/src/bench/logging.cpp

65 lines
2.1 KiB
C++
Raw Normal View History

// Copyright (c) 2020-2022 The Bitcoin Core developers
2020-04-29 15:05:41 +02:00
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <bench/bench.h>
#include <logging.h>
#include <test/util/setup_common.h>
2024-08-26 17:27:54 +02:00
#include <functional>
#include <vector>
2020-04-29 15:05:41 +02:00
// All but 2 of the benchmarks should have roughly similar performance:
//
2024-08-08 10:56:58 +02:00
// LogWithoutDebug should be ~3 orders of magnitude faster, as nothing is logged.
//
// LogWithoutWriteToFile should be ~2 orders of magnitude faster, as it avoids disk writes.
2020-04-29 15:05:41 +02:00
static void Logging(benchmark::Bench& bench, const std::vector<const char*>& extra_args, const std::function<void()>& log)
{
// Reset any enabled logging categories from a previous benchmark run.
LogInstance().DisableCategory(BCLog::LogFlags::ALL);
2020-04-29 15:05:41 +02:00
TestingSetup test_setup{
ChainType::REGTEST,
2024-07-08 15:45:56 +02:00
{.extra_args = extra_args},
2020-04-29 15:05:41 +02:00
};
bench.run([&] { log(); });
}
2024-08-08 10:56:58 +02:00
static void LogWithDebug(benchmark::Bench& bench)
2020-04-29 15:05:41 +02:00
{
Logging(bench, {"-logthreadnames=0", "-debug=net"}, [] { LogDebug(BCLog::NET, "%s\n", "test"); });
2020-04-29 15:05:41 +02:00
}
2024-08-08 10:56:58 +02:00
static void LogWithoutDebug(benchmark::Bench& bench)
2020-04-29 15:05:41 +02:00
{
Logging(bench, {"-logthreadnames=0", "-debug=0"}, [] { LogDebug(BCLog::NET, "%s\n", "test"); });
2020-04-29 15:05:41 +02:00
}
static void LogWithThreadNames(benchmark::Bench& bench)
{
Logging(bench, {"-logthreadnames=1"}, [] { LogInfo("%s\n", "test"); });
}
static void LogWithoutThreadNames(benchmark::Bench& bench)
{
Logging(bench, {"-logthreadnames=0"}, [] { LogInfo("%s\n", "test"); });
}
static void LogWithoutWriteToFile(benchmark::Bench& bench)
2020-04-29 15:05:41 +02:00
{
// Disable writing the log to a file, as used for unit tests and fuzzing in `MakeNoLogFileContext`.
2020-04-29 15:05:41 +02:00
Logging(bench, {"-nodebuglogfile", "-debug=1"}, [] {
LogInfo("%s\n", "test");
LogDebug(BCLog::NET, "%s\n", "test");
2020-04-29 15:05:41 +02:00
});
}
2024-08-08 10:56:58 +02:00
BENCHMARK(LogWithDebug, benchmark::PriorityLevel::HIGH);
BENCHMARK(LogWithoutDebug, benchmark::PriorityLevel::HIGH);
BENCHMARK(LogWithThreadNames, benchmark::PriorityLevel::HIGH);
BENCHMARK(LogWithoutThreadNames, benchmark::PriorityLevel::HIGH);
BENCHMARK(LogWithoutWriteToFile, benchmark::PriorityLevel::HIGH);