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