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