Bitcoin Core  27.99.0
P2P Digital Currency
bench_bitcoin.cpp
Go to the documentation of this file.
1 // Copyright (c) 2015-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 
7 #include <clientversion.h>
8 #include <common/args.h>
9 #include <crypto/sha256.h>
10 #include <util/fs.h>
11 #include <util/strencodings.h>
12 
13 #include <chrono>
14 #include <cstdint>
15 #include <iostream>
16 #include <sstream>
17 #include <vector>
18 
19 using util::SplitString;
20 
21 static const char* DEFAULT_BENCH_FILTER = ".*";
22 static constexpr int64_t DEFAULT_MIN_TIME_MS{10};
24 static const std::string DEFAULT_PRIORITY{"all"};
25 
26 static void SetupBenchArgs(ArgsManager& argsman)
27 {
28  SetupHelpOptions(argsman);
29 
30  argsman.AddArg("-asymptote=<n1,n2,n3,...>", "Test asymptotic growth of the runtime of an algorithm, if supported by the benchmark", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
31  argsman.AddArg("-filter=<regex>", strprintf("Regular expression filter to select benchmark by name (default: %s)", DEFAULT_BENCH_FILTER), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
32  argsman.AddArg("-list", "List benchmarks without executing them", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
33  argsman.AddArg("-min-time=<milliseconds>", strprintf("Minimum runtime per benchmark, in milliseconds (default: %d)", DEFAULT_MIN_TIME_MS), ArgsManager::ALLOW_ANY | ArgsManager::DISALLOW_NEGATION, OptionsCategory::OPTIONS);
34  argsman.AddArg("-output-csv=<output.csv>", "Generate CSV file with the most important benchmark results", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
35  argsman.AddArg("-output-json=<output.json>", "Generate JSON file with all benchmark results", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
36  argsman.AddArg("-sanity-check", "Run benchmarks for only one iteration with no output", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
37  argsman.AddArg("-priority-level=<l1,l2,l3>", strprintf("Run benchmarks of one or multiple priority level(s) (%s), default: '%s'",
39 }
40 
41 // parses a comma separated list like "10,20,30,50"
42 static std::vector<double> parseAsymptote(const std::string& str) {
43  std::stringstream ss(str);
44  std::vector<double> numbers;
45  double d;
46  char c;
47  while (ss >> d) {
48  numbers.push_back(d);
49  ss >> c;
50  }
51  return numbers;
52 }
53 
54 static uint8_t parsePriorityLevel(const std::string& str) {
55  uint8_t levels{0};
56  for (const auto& level: SplitString(str, ',')) {
57  levels |= benchmark::StringToPriority(level);
58  }
59  return levels;
60 }
61 
62 int main(int argc, char** argv)
63 {
64  ArgsManager argsman;
65  SetupBenchArgs(argsman);
67  std::string error;
68  if (!argsman.ParseParameters(argc, argv, error)) {
69  tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error);
70  return EXIT_FAILURE;
71  }
72 
73  if (HelpRequested(argsman)) {
74  std::cout << "Usage: bench_bitcoin [options]\n"
75  "\n"
76  << argsman.GetHelpMessage()
77  << "Description:\n"
78  "\n"
79  " bench_bitcoin executes microbenchmarks. The quality of the benchmark results\n"
80  " highly depend on the stability of the machine. It can sometimes be difficult\n"
81  " to get stable, repeatable results, so here are a few tips:\n"
82  "\n"
83  " * Use pyperf [1] to disable frequency scaling, turbo boost etc. For best\n"
84  " results, use CPU pinning and CPU isolation (see [2]).\n"
85  "\n"
86  " * Each call of run() should do exactly the same work. E.g. inserting into\n"
87  " a std::vector doesn't do that as it will reallocate on certain calls. Make\n"
88  " sure each run has exactly the same preconditions.\n"
89  "\n"
90  " * If results are still not reliable, increase runtime with e.g.\n"
91  " -min-time=5000 to let a benchmark run for at least 5 seconds.\n"
92  "\n"
93  " * bench_bitcoin uses nanobench [3] for which there is extensive\n"
94  " documentation available online.\n"
95  "\n"
96  "Environment Variables:\n"
97  "\n"
98  " To attach a profiler you can run a benchmark in endless mode. This can be\n"
99  " done with the environment variable NANOBENCH_ENDLESS. E.g. like so:\n"
100  "\n"
101  " NANOBENCH_ENDLESS=MuHash ./bench_bitcoin -filter=MuHash\n"
102  "\n"
103  " In rare cases it can be useful to suppress stability warnings. This can be\n"
104  " done with the environment variable NANOBENCH_SUPPRESS_WARNINGS, e.g:\n"
105  "\n"
106  " NANOBENCH_SUPPRESS_WARNINGS=1 ./bench_bitcoin\n"
107  "\n"
108  "Notes:\n"
109  "\n"
110  " 1. pyperf\n"
111  " https://github.com/psf/pyperf\n"
112  "\n"
113  " 2. CPU pinning & isolation\n"
114  " https://pyperf.readthedocs.io/en/latest/system.html\n"
115  "\n"
116  " 3. nanobench\n"
117  " https://github.com/martinus/nanobench\n"
118  "\n";
119 
120  return EXIT_SUCCESS;
121  }
122 
123  try {
125  args.asymptote = parseAsymptote(argsman.GetArg("-asymptote", ""));
126  args.is_list_only = argsman.GetBoolArg("-list", false);
127  args.min_time = std::chrono::milliseconds(argsman.GetIntArg("-min-time", DEFAULT_MIN_TIME_MS));
128  args.output_csv = argsman.GetPathArg("-output-csv");
129  args.output_json = argsman.GetPathArg("-output-json");
130  args.regex_filter = argsman.GetArg("-filter", DEFAULT_BENCH_FILTER);
131  args.sanity_check = argsman.GetBoolArg("-sanity-check", false);
132  args.priority = parsePriorityLevel(argsman.GetArg("-priority-level", DEFAULT_PRIORITY));
133 
135 
136  return EXIT_SUCCESS;
137  } catch (const std::exception& e) {
138  tfm::format(std::cerr, "Error: %s\n", e.what());
139  return EXIT_FAILURE;
140  }
141 }
bool HelpRequested(const ArgsManager &args)
Definition: args.cpp:659
void SetupHelpOptions(ArgsManager &args)
Add help options to the args manager.
Definition: args.cpp:664
static constexpr int64_t DEFAULT_MIN_TIME_MS
static const std::string DEFAULT_PRIORITY
Priority level default value, run "all" priority levels.
int main(int argc, char **argv)
static std::vector< double > parseAsymptote(const std::string &str)
static uint8_t parsePriorityLevel(const std::string &str)
static const char * DEFAULT_BENCH_FILTER
static void SetupBenchArgs(ArgsManager &argsman)
return EXIT_SUCCESS
ArgsManager & args
Definition: bitcoind.cpp:270
@ ALLOW_ANY
disable validation
Definition: args.h:104
@ DISALLOW_NEGATION
disallow -nofoo syntax
Definition: args.h:109
bool ParseParameters(int argc, const char *const argv[], std::string &error)
Definition: args.cpp:177
std::string GetHelpMessage() const
Get the help string.
Definition: args.cpp:590
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
Definition: args.cpp:480
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: args.cpp:455
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: args.cpp:505
void AddArg(const std::string &name, const std::string &help, unsigned int flags, const OptionsCategory &cat)
Add argument.
Definition: args.cpp:562
fs::path GetPathArg(std::string arg, const fs::path &default_value={}) const
Return path argument or default value.
Definition: args.cpp:270
static void RunAll(const Args &args)
Definition: bench.cpp:83
std::string ListPriorities()
Definition: bench.cpp:57
uint8_t StringToPriority(const std::string &str)
Definition: bench.cpp:65
void format(std::ostream &out, const char *fmt, const Args &... args)
Format list of arguments to the stream according to given format string.
Definition: tinyformat.h:1059
std::vector< std::string > SplitString(std::string_view str, char sep)
Definition: string.h:59
std::string SHA256AutoDetect(sha256_implementation::UseImplementation use_implementation)
Autodetect the best available SHA256 implementation.
Definition: sha256.cpp:586
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1161