Bitcoin Core  27.99.0
P2P Digital Currency
logging.cpp
Go to the documentation of this file.
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>
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.
20 
21  TestingSetup test_setup{
23  extra_args,
24  };
25 
26  bench.run([&] { log(); });
27 }
28 
30 {
31  Logging(bench, {"-logthreadnames=1", "-debug=net"}, [] {
32  LogPrintLevel(BCLog::NET, BCLog::Level::Error, "%s\n", "test"); });
33 }
34 
36 {
37  Logging(bench, {"-logthreadnames=0", "-debug=net"}, [] {
38  LogPrintLevel(BCLog::NET, BCLog::Level::Error, "%s\n", "test"); });
39 }
40 
42 {
43  Logging(bench, {"-logthreadnames=0", "-debug=net"}, [] { LogPrint(BCLog::NET, "%s\n", "test"); });
44 }
45 
47 {
48  Logging(bench, {"-logthreadnames=0", "-debug=0"}, [] { LogPrint(BCLog::NET, "%s\n", "test"); });
49 }
50 
52 {
53  Logging(bench, {"-logthreadnames=1", "-debug=net"}, [] {
54  LogPrintfCategory(BCLog::NET, "%s\n", "test");
55  });
56 }
57 
59 {
60  Logging(bench, {"-logthreadnames=0", "-debug=net"}, [] {
61  LogPrintfCategory(BCLog::NET, "%s\n", "test");
62  });
63 }
64 
66 {
67  Logging(bench, {"-logthreadnames=1"}, [] { LogPrintf("%s\n", "test"); });
68 }
69 
71 {
72  Logging(bench, {"-logthreadnames=0"}, [] { LogPrintf("%s\n", "test"); });
73 }
74 
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 
static void LogPrintfWithThreadNames(benchmark::Bench &bench)
Definition: logging.cpp:65
static void LogWithoutWriteToFile(benchmark::Bench &bench)
Definition: logging.cpp:75
static void LogPrintfCategoryWithoutThreadNames(benchmark::Bench &bench)
Definition: logging.cpp:58
static void LogPrintfWithoutThreadNames(benchmark::Bench &bench)
Definition: logging.cpp:70
static void LogPrintWithCategory(benchmark::Bench &bench)
Definition: logging.cpp:41
static void LogPrintLevelWithoutThreadNames(benchmark::Bench &bench)
Definition: logging.cpp:35
static void LogPrintfCategoryWithThreadNames(benchmark::Bench &bench)
Definition: logging.cpp:51
static void LogPrintLevelWithThreadNames(benchmark::Bench &bench)
Definition: logging.cpp:29
static void Logging(benchmark::Bench &bench, const std::vector< const char * > &extra_args, const std::function< void()> &log)
Definition: logging.cpp:16
static void LogPrintWithoutCategory(benchmark::Bench &bench)
Definition: logging.cpp:46
BENCHMARK(LogPrintLevelWithThreadNames, benchmark::PriorityLevel::HIGH)
void DisableCategory(LogFlags flag)
Definition: logging.cpp:108
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:627
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1234
BCLog::Logger & LogInstance()
Definition: logging.cpp:19
#define LogPrintLevel(category, level,...)
Definition: logging.h:251
#define LogPrint(category,...)
Definition: logging.h:263
#define LogPrintfCategory(category,...)
Definition: logging.h:245
#define LogPrintf(...)
Definition: logging.h:244
@ ALL
Definition: logging.h:72
@ NET
Definition: logging.h:41
@ HIGH
Definition: bench.h:47
Testing setup that configures a complete environment.
Definition: setup_common.h:83