Bitcoin Core  0.20.99
P2P Digital Currency
bench_bitcoin.cpp
Go to the documentation of this file.
1 // Copyright (c) 2015-2020 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 <crypto/sha256.h>
8 #include <util/strencodings.h>
9 #include <util/system.h>
10 
11 #include <memory>
12 
13 static const char* DEFAULT_BENCH_FILTER = ".*";
14 
15 static void SetupBenchArgs(ArgsManager& argsman)
16 {
17  SetupHelpOptions(argsman);
18 
19  argsman.AddArg("-list", "List benchmarks without executing them", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
20  argsman.AddArg("-filter=<regex>", strprintf("Regular expression filter to select benchmark by name (default: %s)", DEFAULT_BENCH_FILTER), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
21  argsman.AddArg("-asymptote=n1,n2,n3,...", strprintf("Test asymptotic growth of the runtime of an algorithm, if supported by the benchmark"), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
22  argsman.AddArg("-output_csv=<output.csv>", "Generate CSV file with the most important benchmark results.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
23  argsman.AddArg("-output_json=<output.json>", "Generate JSON file with all benchmark results.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
24 }
25 
26 // parses a comma separated list like "10,20,30,50"
27 static std::vector<double> parseAsymptote(const std::string& str) {
28  std::stringstream ss(str);
29  std::vector<double> numbers;
30  double d;
31  char c;
32  while (ss >> d) {
33  numbers.push_back(d);
34  ss >> c;
35  }
36  return numbers;
37 }
38 
39 int main(int argc, char** argv)
40 {
41  ArgsManager argsman;
42  SetupBenchArgs(argsman);
44  std::string error;
45  if (!argsman.ParseParameters(argc, argv, error)) {
46  tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error);
47  return EXIT_FAILURE;
48  }
49 
50  if (HelpRequested(argsman)) {
51  std::cout << argsman.GetHelpMessage();
52 
53  return EXIT_SUCCESS;
54  }
55 
56  benchmark::Args args;
57  args.regex_filter = argsman.GetArg("-filter", DEFAULT_BENCH_FILTER);
58  args.is_list_only = argsman.GetBoolArg("-list", false);
59  args.asymptote = parseAsymptote(argsman.GetArg("-asymptote", ""));
60  args.output_csv = argsman.GetArg("-output_csv", "");
61  args.output_json = argsman.GetArg("-output_json", "");
62 
64 
65  return EXIT_SUCCESS;
66 }
std::string output_json
Definition: bench.h:45
std::vector< double > asymptote
Definition: bench.h:43
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
std::string output_csv
Definition: bench.h:44
static std::vector< double > parseAsymptote(const std::string &str)
NODISCARD bool ParseParameters(int argc, const char *const argv[], std::string &error)
Definition: system.cpp:283
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:478
std::string SHA256AutoDetect()
Autodetect the best available SHA256 implementation.
Definition: sha256.cpp:562
std::string GetHelpMessage() const
Get the help string.
Definition: system.cpp:532
static const char * DEFAULT_BENCH_FILTER
static void SetupBenchArgs(ArgsManager &argsman)
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:506
bool HelpRequested(const ArgsManager &args)
Definition: system.cpp:601
static void RunAll(const Args &args)
Definition: bench.cpp:46
bool is_list_only
Definition: bench.h:42
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:466
int main(int argc, char **argv)
void SetupHelpOptions(ArgsManager &args)
Add help options to the args manager.
Definition: system.cpp:606
std::string regex_filter
Definition: bench.h:41
bool error(const char *fmt, const Args &... args)
Definition: system.h:52