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