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 <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(ArgsManager& argsman)
21 {
22  SetupHelpOptions(argsman);
23 
24  argsman.AddArg("-list", "List benchmarks without executing them. Can be combined with -scaling and -filter", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
25  argsman.AddArg("-evals=<n>", strprintf("Number of measurement evaluations to perform. (default: %u)", DEFAULT_BENCH_EVALUATIONS), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
26  argsman.AddArg("-filter=<regex>", strprintf("Regular expression filter to select benchmark by name (default: %s)", DEFAULT_BENCH_FILTER), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
27  argsman.AddArg("-scaling=<n>", strprintf("Scaling factor for benchmark's runtime (default: %u)", DEFAULT_BENCH_SCALING), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
28  argsman.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  argsman.AddArg("-plot-plotlyurl=<uri>", strprintf("URL to use for plotly.js (default: %s)", DEFAULT_PLOT_PLOTLYURL), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
30  argsman.AddArg("-plot-width=<x>", strprintf("Plot width in pixel (default: %u)", DEFAULT_PLOT_WIDTH), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
31  argsman.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 {
36  ArgsManager argsman;
37  SetupBenchArgs(argsman);
38  std::string error;
39  if (!argsman.ParseParameters(argc, argv, error)) {
40  tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error);
41  return EXIT_FAILURE;
42  }
43 
44  if (HelpRequested(argsman)) {
45  std::cout << argsman.GetHelpMessage();
46 
47  return EXIT_SUCCESS;
48  }
49 
50  int64_t evaluations = argsman.GetArg("-evals", DEFAULT_BENCH_EVALUATIONS);
51  std::string regex_filter = argsman.GetArg("-filter", DEFAULT_BENCH_FILTER);
52  std::string scaling_str = argsman.GetArg("-scaling", DEFAULT_BENCH_SCALING);
53  bool is_list_only = argsman.GetBoolArg("-list", false);
54 
55  if (evaluations == 0) {
56  return EXIT_SUCCESS;
57  } else if (evaluations < 0) {
58  tfm::format(std::cerr, "Error parsing evaluations argument: %d\n", evaluations);
59  return EXIT_FAILURE;
60  }
61 
62  double scaling_factor;
63  if (!ParseDouble(scaling_str, &scaling_factor)) {
64  tfm::format(std::cerr, "Error parsing scaling factor as double: %s\n", scaling_str);
65  return EXIT_FAILURE;
66  }
67 
68  std::unique_ptr<benchmark::Printer> printer = MakeUnique<benchmark::ConsolePrinter>();
69  std::string printer_arg = argsman.GetArg("-printer", DEFAULT_BENCH_PRINTER);
70  if ("plot" == printer_arg) {
71  printer.reset(new benchmark::PlotlyPrinter(
72  argsman.GetArg("-plot-plotlyurl", DEFAULT_PLOT_PLOTLYURL),
73  argsman.GetArg("-plot-width", DEFAULT_PLOT_WIDTH),
74  argsman.GetArg("-plot-height", DEFAULT_PLOT_HEIGHT)));
75  }
76 
77  benchmark::BenchRunner::RunAll(*printer, evaluations, scaling_factor, regex_filter, is_list_only);
78 
79  return EXIT_SUCCESS;
80 }
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:283
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:392
std::string GetHelpMessage() const
Get the help string.
Definition: system.cpp:446
static const char * DEFAULT_BENCH_FILTER
static void SetupBenchArgs(ArgsManager &argsman)
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:420
bool HelpRequested(const ArgsManager &args)
Definition: system.cpp:515
static void RunAll(Printer &printer, uint64_t num_evals, double scaling, const std::string &filter, bool is_list_only)
Definition: bench.cpp:102
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:380
int main(int argc, char **argv)
void SetupHelpOptions(ArgsManager &args)
Add help options to the args manager.
Definition: system.cpp:520
bool error(const char *fmt, const Args &... args)
Definition: system.h:49