/ src / bench / logging.cpp
logging.cpp
 1  // Copyright (c) 2020-2022 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  #include <util/chaintype.h>
 9  
10  // All but 2 of the benchmarks should have roughly similar performance:
11  //
12  // LogPrintWithoutCategory should be ~3 orders of magnitude faster, as nothing is logged.
13  //
14  // LogWithoutWriteToFile should be ~2 orders of magnitude faster, as it avoids disk writes.
15  
16  static void Logging(benchmark::Bench& bench, const std::vector<const char*>& extra_args, const std::function<void()>& log)
17  {
18      // Reset any enabled logging categories from a previous benchmark run.
19      LogInstance().DisableCategory(BCLog::LogFlags::ALL);
20  
21      TestingSetup test_setup{
22          ChainType::REGTEST,
23          extra_args,
24      };
25  
26      bench.run([&] { log(); });
27  }
28  
29  static void LogPrintLevelWithThreadNames(benchmark::Bench& bench)
30  {
31      Logging(bench, {"-logthreadnames=1", "-debug=net"}, [] {
32          LogPrintLevel(BCLog::NET, BCLog::Level::Error, "%s\n", "test"); });
33  }
34  
35  static void LogPrintLevelWithoutThreadNames(benchmark::Bench& bench)
36  {
37      Logging(bench, {"-logthreadnames=0", "-debug=net"}, [] {
38          LogPrintLevel(BCLog::NET, BCLog::Level::Error, "%s\n", "test"); });
39  }
40  
41  static void LogPrintWithCategory(benchmark::Bench& bench)
42  {
43      Logging(bench, {"-logthreadnames=0", "-debug=net"}, [] { LogPrint(BCLog::NET, "%s\n", "test"); });
44  }
45  
46  static void LogPrintWithoutCategory(benchmark::Bench& bench)
47  {
48      Logging(bench, {"-logthreadnames=0", "-debug=0"}, [] { LogPrint(BCLog::NET, "%s\n", "test"); });
49  }
50  
51  static void LogPrintfCategoryWithThreadNames(benchmark::Bench& bench)
52  {
53      Logging(bench, {"-logthreadnames=1", "-debug=net"}, [] {
54          LogPrintfCategory(BCLog::NET, "%s\n", "test");
55      });
56  }
57  
58  static void LogPrintfCategoryWithoutThreadNames(benchmark::Bench& bench)
59  {
60      Logging(bench, {"-logthreadnames=0", "-debug=net"}, [] {
61          LogPrintfCategory(BCLog::NET, "%s\n", "test");
62      });
63  }
64  
65  static void LogPrintfWithThreadNames(benchmark::Bench& bench)
66  {
67      Logging(bench, {"-logthreadnames=1"}, [] { LogPrintf("%s\n", "test"); });
68  }
69  
70  static void LogPrintfWithoutThreadNames(benchmark::Bench& bench)
71  {
72      Logging(bench, {"-logthreadnames=0"}, [] { LogPrintf("%s\n", "test"); });
73  }
74  
75  static void LogWithoutWriteToFile(benchmark::Bench& bench)
76  {
77      // Disable writing the log to a file, as used for unit tests and fuzzing in `MakeNoLogFileContext`.
78      Logging(bench, {"-nodebuglogfile", "-debug=1"}, [] {
79          LogPrintf("%s\n", "test");
80          LogPrint(BCLog::NET, "%s\n", "test");
81      });
82  }
83  
84  BENCHMARK(LogPrintLevelWithThreadNames, benchmark::PriorityLevel::HIGH);
85  BENCHMARK(LogPrintLevelWithoutThreadNames, benchmark::PriorityLevel::HIGH);
86  BENCHMARK(LogPrintWithCategory, benchmark::PriorityLevel::HIGH);
87  BENCHMARK(LogPrintWithoutCategory, benchmark::PriorityLevel::HIGH);
88  BENCHMARK(LogPrintfCategoryWithThreadNames, benchmark::PriorityLevel::HIGH);
89  BENCHMARK(LogPrintfCategoryWithoutThreadNames, benchmark::PriorityLevel::HIGH);
90  BENCHMARK(LogPrintfWithThreadNames, benchmark::PriorityLevel::HIGH);
91  BENCHMARK(LogPrintfWithoutThreadNames, benchmark::PriorityLevel::HIGH);
92  BENCHMARK(LogWithoutWriteToFile, benchmark::PriorityLevel::HIGH);