Bitcoin Core  0.19.99
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 <util/strencodings.h>
8 #include <util/system.h>
9 
10 #include <memory>
11 
12 static const int64_t DEFAULT_BENCH_EVALUATIONS = 5;
13 static const char* DEFAULT_BENCH_FILTER = ".*";
14 static const char* DEFAULT_BENCH_SCALING = "1.0";
15 static const char* DEFAULT_BENCH_PRINTER = "console";
16 static const char* DEFAULT_PLOT_PLOTLYURL = "https://cdn.plot.ly/plotly-latest.min.js";
17 static const int64_t DEFAULT_PLOT_WIDTH = 1024;
18 static const int64_t DEFAULT_PLOT_HEIGHT = 768;
19 
20 static void SetupBenchArgs()
21 {
23 
24  gArgs.AddArg("-list", "List benchmarks without executing them. Can be combined with -scaling and -filter", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
25  gArgs.AddArg("-evals=<n>", strprintf("Number of measurement evaluations to perform. (default: %u)", DEFAULT_BENCH_EVALUATIONS), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
26  gArgs.AddArg("-filter=<regex>", strprintf("Regular expression filter to select benchmark by name (default: %s)", DEFAULT_BENCH_FILTER), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
27  gArgs.AddArg("-scaling=<n>", strprintf("Scaling factor for benchmark's runtime (default: %u)", DEFAULT_BENCH_SCALING), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
28  gArgs.AddArg("-printer=(console|plot)", strprintf("Choose printer format. console: print data to console. plot: Print results as HTML graph (default: %s)", DEFAULT_BENCH_PRINTER), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
29  gArgs.AddArg("-plot-plotlyurl=<uri>", strprintf("URL to use for plotly.js (default: %s)", DEFAULT_PLOT_PLOTLYURL), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
30  gArgs.AddArg("-plot-width=<x>", strprintf("Plot width in pixel (default: %u)", DEFAULT_PLOT_WIDTH), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
31  gArgs.AddArg("-plot-height=<x>", strprintf("Plot height in pixel (default: %u)", DEFAULT_PLOT_HEIGHT), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
32 }
33 
34 int main(int argc, char** argv)
35 {
37  std::string error;
38  if (!gArgs.ParseParameters(argc, argv, error)) {
39  tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error);
40  return EXIT_FAILURE;
41  }
42 
43  if (HelpRequested(gArgs)) {
44  std::cout << gArgs.GetHelpMessage();
45 
46  return EXIT_SUCCESS;
47  }
48 
49  int64_t evaluations = gArgs.GetArg("-evals", DEFAULT_BENCH_EVALUATIONS);
50  std::string regex_filter = gArgs.GetArg("-filter", DEFAULT_BENCH_FILTER);
51  std::string scaling_str = gArgs.GetArg("-scaling", DEFAULT_BENCH_SCALING);
52  bool is_list_only = gArgs.GetBoolArg("-list", false);
53 
54  if (evaluations == 0) {
55  return EXIT_SUCCESS;
56  } else if (evaluations < 0) {
57  tfm::format(std::cerr, "Error parsing evaluations argument: %d\n", evaluations);
58  return EXIT_FAILURE;
59  }
60 
61  double scaling_factor;
62  if (!ParseDouble(scaling_str, &scaling_factor)) {
63  tfm::format(std::cerr, "Error parsing scaling factor as double: %s\n", scaling_str);
64  return EXIT_FAILURE;
65  }
66 
67  std::unique_ptr<benchmark::Printer> printer = MakeUnique<benchmark::ConsolePrinter>();
68  std::string printer_arg = gArgs.GetArg("-printer", DEFAULT_BENCH_PRINTER);
69  if ("plot" == printer_arg) {
70  printer.reset(new benchmark::PlotlyPrinter(
71  gArgs.GetArg("-plot-plotlyurl", DEFAULT_PLOT_PLOTLYURL),
72  gArgs.GetArg("-plot-width", DEFAULT_PLOT_WIDTH),
73  gArgs.GetArg("-plot-height", DEFAULT_PLOT_HEIGHT)));
74  }
75 
76  benchmark::BenchRunner::RunAll(*printer, evaluations, scaling_factor, regex_filter, is_list_only);
77 
78  return EXIT_SUCCESS;
79 }
static const char * DEFAULT_BENCH_SCALING
static const char * DEFAULT_BENCH_PRINTER
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
NODISCARD bool ParseParameters(int argc, const char *const argv[], std::string &error)
Definition: system.cpp:275
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:384
std::string GetHelpMessage() const
Get the help string.
Definition: system.cpp:438
static const char * DEFAULT_BENCH_FILTER
static const int64_t DEFAULT_PLOT_HEIGHT
static const int64_t DEFAULT_PLOT_WIDTH
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:412
bool HelpRequested(const ArgsManager &args)
Definition: system.cpp:507
static void RunAll(Printer &printer, uint64_t num_evals, double scaling, const std::string &filter, bool is_list_only)
Definition: bench.cpp:103
bool ParseDouble(const std::string &str, double *out)
Convert string to double with strict parse error feedback.
static const char * DEFAULT_PLOT_PLOTLYURL
static const int64_t DEFAULT_BENCH_EVALUATIONS
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:372
int main(int argc, char **argv)
ArgsManager gArgs
Definition: system.cpp:76
void SetupHelpOptions(ArgsManager &args)
Add help options to the args manager.
Definition: system.cpp:512
static void SetupBenchArgs()
bool error(const char *fmt, const Args &... args)
Definition: system.h:49