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