Bitcoin ABC  0.26.3
P2P Digital Currency
bench_bitcoin.cpp
Go to the documentation of this file.
1 // Copyright (c) 2015-2019 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  SetupHelpOptions(argsman);
23 
24  argsman.AddArg("-asymptote=<n1,n2,n3,...>",
25  "Test asymptotic growth of the runtime of an algorithm, if "
26  "supported by the benchmark",
28  argsman.AddArg("-filter=<regex>",
29  strprintf("Regular expression filter to select benchmark by "
30  "name (default: %s)",
33  argsman.AddArg("-list", "List benchmarks without executing them",
35  argsman.AddArg(
36  "-min_time=<milliseconds>",
37  strprintf(
38  "Minimum runtime per benchmark, in milliseconds (default: %d)",
41  argsman.AddArg(
42  "-output_csv=<output.csv>",
43  "Generate CSV file with the most important benchmark results",
45  argsman.AddArg("-output_json=<output.json>",
46  "Generate JSON file with all benchmark results",
48 }
49 
50 // parses a comma separated list like "10,20,30,50"
51 static std::vector<double> parseAsymptote(const std::string &str) {
52  std::stringstream ss(str);
53  std::vector<double> numbers;
54  double d;
55  char c;
56  while (ss >> d) {
57  numbers.push_back(d);
58  ss >> c;
59  }
60  return numbers;
61 }
62 
63 int main(int argc, char **argv) {
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",
70  error);
71  return EXIT_FAILURE;
72  }
73 
74  if (HelpRequested(argsman)) {
75  std::cout
76  << "Usage: bitcoin-bench [options]\n"
77  "\n"
78  << argsman.GetHelpMessage()
79  << "Description:\n"
80  "\n"
81  " bitcoin-bench executes microbenchmarks. The quality of the "
82  "benchmark results\n"
83  " highly depend on the stability of the machine. It can "
84  "sometimes be difficult\n"
85  " to get stable, repeatable results, so here are a few tips:\n"
86  "\n"
87  " * Use pyperf [1] to disable frequency scaling, turbo boost "
88  "etc. For best\n"
89  " results, use CPU pinning and CPU isolation (see [2]).\n"
90  "\n"
91  " * Each call of run() should do exactly the same work. E.g. "
92  "inserting into\n"
93  " a std::vector doesn't do that as it will reallocate on "
94  "certain calls. Make\n"
95  " sure each run has exactly the same preconditions.\n"
96  "\n"
97  " * If results are still not reliable, increase runtime with "
98  "e.g.\n"
99  " -min_time=5000 to let a benchmark run for at least 5 "
100  "seconds.\n"
101  "\n"
102  " * bitcoin-bench uses nanobench [3] for which there is "
103  "extensive\n"
104  " documentation available online.\n"
105  "\n"
106  "Environment Variables:\n"
107  "\n"
108  " To attach a profiler you can run a benchmark in endless "
109  "mode. This can be\n"
110  " done with the environment variable NANOBENCH_ENDLESS. E.g. "
111  "like so:\n"
112  "\n"
113  " NANOBENCH_ENDLESS=MuHash ./bitcoin-bench -filter=MuHash\n"
114  "\n"
115  " In rare cases it can be useful to suppress stability "
116  "warnings. This can be\n"
117  " done with the environment variable "
118  "NANOBENCH_SUPPRESS_WARNINGS, e.g:\n"
119  "\n"
120  " NANOBENCH_SUPPRESS_WARNINGS=1 ./bitcoin-bench\n"
121  "\n"
122  "Notes:\n"
123  "\n"
124  " 1. pyperf\n"
125  " https://github.com/psf/pyperf\n"
126  "\n"
127  " 2. CPU pinning & isolation\n"
128  " https://pyperf.readthedocs.io/en/latest/system.html\n"
129  "\n"
130  " 3. nanobench\n"
131  " https://github.com/martinus/nanobench\n"
132  "\n";
133 
134  return EXIT_SUCCESS;
135  }
136 
137  benchmark::Args args;
138  args.asymptote = parseAsymptote(argsman.GetArg("-asymptote", ""));
139  args.is_list_only = argsman.GetBoolArg("-list", false);
140  args.min_time = std::chrono::milliseconds(
141  argsman.GetIntArg("-min_time", DEFAULT_MIN_TIME_MS));
142  args.output_csv = argsman.GetArg("-output_csv", "");
143  args.output_json = argsman.GetArg("-output_json", "");
144  args.regex_filter = argsman.GetArg("-filter", DEFAULT_BENCH_FILTER);
145 
147 
148  return EXIT_SUCCESS;
149 }
static constexpr int64_t DEFAULT_MIN_TIME_MS
int main(int argc, char **argv)
static std::vector< double > parseAsymptote(const std::string &str)
static const char * DEFAULT_BENCH_FILTER
static void SetupBenchArgs(ArgsManager &argsman)
@ ALLOW_ANY
Definition: system.h:152
@ ALLOW_INT
Definition: system.h:150
@ ALLOW_BOOL
Definition: system.h:149
bool ParseParameters(int argc, const char *const argv[], std::string &error)
Definition: system.cpp:329
std::string GetHelpMessage() const
Get the help string.
Definition: system.cpp:682
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
Definition: system.cpp:591
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:582
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:601
void AddArg(const std::string &name, const std::string &help, unsigned int flags, const OptionsCategory &cat)
Add argument.
Definition: system.cpp:649
static void RunAll(const Args &args)
Definition: bench.cpp:52
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:1111
std::string SHA256AutoDetect()
Autodetect the best available SHA256 implementation.
Definition: sha256.cpp:746
std::string output_csv
Definition: bench.h:47
std::string regex_filter
Definition: bench.h:49
std::vector< double > asymptote
Definition: bench.h:46
std::string output_json
Definition: bench.h:48
std::chrono::milliseconds min_time
Definition: bench.h:45
bool is_list_only
Definition: bench.h:44
bool HelpRequested(const ArgsManager &args)
Definition: system.cpp:761
void SetupHelpOptions(ArgsManager &args)
Add help options to the args manager.
Definition: system.cpp:766
bool error(const char *fmt, const Args &...args)
Definition: system.h:45
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201