/ src / bench / logging.cpp
logging.cpp
 1  // Copyright (c) 2020-present The Bitcoin Core developers
 2  // Distributed under the MIT software license, see the accompanying
 3  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
 4  
 5  #include <bench/bench.h>
 6  #include <logging.h>
 7  #include <test/util/setup_common.h>
 8  
 9  #include <functional>
10  #include <vector>
11  
12  // All but 2 of the benchmarks should have roughly similar performance:
13  //
14  // LogWithoutDebug should be ~3 orders of magnitude faster, as nothing is logged.
15  //
16  // LogWithoutWriteToFile should be ~2 orders of magnitude faster, as it avoids disk writes.
17  
18  static void Logging(benchmark::Bench& bench, const std::vector<const char*>& extra_args, const std::function<void()>& log)
19  {
20      // Reset any enabled logging categories from a previous benchmark run.
21      LogInstance().DisableCategory(BCLog::LogFlags::ALL);
22  
23      TestingSetup test_setup{
24          ChainType::REGTEST,
25          {.extra_args = extra_args},
26      };
27  
28      bench.run([&] { log(); });
29  }
30  
31  static void LogWithDebug(benchmark::Bench& bench)
32  {
33      Logging(bench, {"-logthreadnames=0", "-debug=net"}, [] { LogDebug(BCLog::NET, "%s\n", "test"); });
34  }
35  
36  static void LogWithoutDebug(benchmark::Bench& bench)
37  {
38      Logging(bench, {"-logthreadnames=0", "-debug=0"}, [] { LogDebug(BCLog::NET, "%s\n", "test"); });
39  }
40  
41  static void LogWithThreadNames(benchmark::Bench& bench)
42  {
43      Logging(bench, {"-logthreadnames=1"}, [] { LogInfo("%s\n", "test"); });
44  }
45  
46  static void LogWithoutThreadNames(benchmark::Bench& bench)
47  {
48      Logging(bench, {"-logthreadnames=0"}, [] { LogInfo("%s\n", "test"); });
49  }
50  
51  static void LogWithoutWriteToFile(benchmark::Bench& bench)
52  {
53      // Disable writing the log to a file, as used for unit tests and fuzzing in `MakeNoLogFileContext`.
54      Logging(bench, {"-nodebuglogfile", "-debug=1"}, [] {
55          LogInfo("%s\n", "test");
56          LogDebug(BCLog::NET, "%s\n", "test");
57      });
58  }
59  
60  BENCHMARK(LogWithDebug);
61  BENCHMARK(LogWithoutDebug);
62  BENCHMARK(LogWithThreadNames);
63  BENCHMARK(LogWithoutThreadNames);
64  BENCHMARK(LogWithoutWriteToFile);