Bitcoin ABC  0.22.12
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 =
17  "https://cdn.plot.ly/plotly-latest.min.js";
18 static const int64_t DEFAULT_PLOT_WIDTH = 1024;
19 static const int64_t DEFAULT_PLOT_HEIGHT = 768;
20 
21 static void SetupBenchArgs(ArgsManager &argsman) {
22  SetupHelpOptions(argsman);
23 
24  argsman.AddArg("-list",
25  "List benchmarks without executing them. Can be combined "
26  "with -scaling and -filter",
28  argsman.AddArg(
29  "-evals=<n>",
30  strprintf("Number of measurement evaluations to perform. (default: %u)",
33  argsman.AddArg("-filter=<regex>",
34  strprintf("Regular expression filter to select benchmark by "
35  "name (default: %s)",
38  argsman.AddArg(
39  "-scaling=<n>",
40  strprintf("Scaling factor for benchmark's runtime (default: %u)",
43  argsman.AddArg(
44  "-printer=(console|junit|plot)",
45  strprintf("Choose printer format. console: print data to console. "
46  "junit: print results as a Junit compliant XML."
47  "plot: print results as HTML graph (default: %s)",
50  argsman.AddArg("-plot-plotlyurl=<uri>",
51  strprintf("URL to use for plotly.js (default: %s)",
54  argsman.AddArg(
55  "-plot-width=<x>",
56  strprintf("Plot width in pixel (default: %u)", DEFAULT_PLOT_WIDTH),
58  argsman.AddArg(
59  "-plot-height=<x>",
60  strprintf("Plot height in pixel (default: %u)", DEFAULT_PLOT_HEIGHT),
62 }
63 
64 int main(int argc, char **argv) {
65  ArgsManager argsman;
66  SetupBenchArgs(argsman);
67  std::string error;
68  if (!argsman.ParseParameters(argc, argv, error)) {
69  tfm::format(std::cerr, "Error parsing command line arguments: %s\n",
70  error);
71  return EXIT_FAILURE;
72  }
73 
74  if (HelpRequested(argsman)) {
75  std::cout << argsman.GetHelpMessage();
76  return EXIT_SUCCESS;
77  }
78 
79  int64_t evaluations = argsman.GetArg("-evals", DEFAULT_BENCH_EVALUATIONS);
80  std::string regex_filter = argsman.GetArg("-filter", DEFAULT_BENCH_FILTER);
81  std::string scaling_str = argsman.GetArg("-scaling", DEFAULT_BENCH_SCALING);
82  bool is_list_only = argsman.GetBoolArg("-list", false);
83 
84  if (evaluations == 0) {
85  return EXIT_SUCCESS;
86  } else if (evaluations < 0) {
87  tfm::format(std::cerr, "Error parsing evaluations argument: %d\n",
88  evaluations);
89  return EXIT_FAILURE;
90  }
91 
92  double scaling_factor;
93  if (!ParseDouble(scaling_str, &scaling_factor)) {
94  tfm::format(std::cerr, "Error parsing scaling factor as double: %s\n",
95  scaling_str);
96  return EXIT_FAILURE;
97  }
98 
99  std::unique_ptr<benchmark::Printer> printer =
100  std::make_unique<benchmark::ConsolePrinter>();
101  std::string printer_arg = argsman.GetArg("-printer", DEFAULT_BENCH_PRINTER);
102  if ("plot" == printer_arg) {
103  printer.reset(new benchmark::PlotlyPrinter(
104  argsman.GetArg("-plot-plotlyurl", DEFAULT_PLOT_PLOTLYURL),
105  argsman.GetArg("-plot-width", DEFAULT_PLOT_WIDTH),
106  argsman.GetArg("-plot-height", DEFAULT_PLOT_HEIGHT)));
107  }
108  if ("junit" == printer_arg) {
109  printer.reset(new benchmark::JunitPrinter());
110  }
111 
112  // gArgs no longer needed. Clear it here to avoid interactions with the
113  // testing setup in the benches
114  gArgs.ClearArgs();
115 
116  benchmark::BenchRunner::RunAll(*printer, evaluations, scaling_factor,
117  regex_filter, is_list_only);
118 
119  return EXIT_SUCCESS;
120 }
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:1201
NODISCARD bool ParseParameters(int argc, const char *const argv[], std::string &error)
Definition: system.cpp:318
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:498
std::string GetHelpMessage() const
Get the help string.
Definition: system.cpp:579
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:1111
void AddArg(const std::string &name, const std::string &help, unsigned int flags, const OptionsCategory &cat)
Add argument.
Definition: system.cpp:546
bool HelpRequested(const ArgsManager &args)
Definition: system.cpp:652
void ClearArgs()
Clear available arguments.
Definition: system.h:345
static void RunAll(Printer &printer, uint64_t num_evals, double scaling, const std::string &filter, bool is_list_only)
Definition: bench.cpp:135
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:479
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:657
bool error(const char *fmt, const Args &... args)
Definition: system.h:47