BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
bench_bitcoin.cpp
Go to the documentation of this file.
1 // Copyright (c) 2015-2018 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 <key.h>
9 #include <random.h>
10 #include <util.h>
11 #include <utilstrencodings.h>
12 #include <validation.h>
13 
14 #include <memory>
15 
16 const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
17 
18 static const int64_t DEFAULT_BENCH_EVALUATIONS = 5;
19 static const char* DEFAULT_BENCH_FILTER = ".*";
20 static const char* DEFAULT_BENCH_SCALING = "1.0";
21 static const char* DEFAULT_BENCH_PRINTER = "console";
22 static const char* DEFAULT_PLOT_PLOTLYURL = "https://cdn.plot.ly/plotly-latest.min.js";
23 static const int64_t DEFAULT_PLOT_WIDTH = 1024;
24 static const int64_t DEFAULT_PLOT_HEIGHT = 768;
25 
26 static void SetupBenchArgs()
27 {
28  gArgs.AddArg("-?", "Print this help message and exit", false, OptionsCategory::OPTIONS);
29  gArgs.AddArg("-list", "List benchmarks without executing them. Can be combined with -scaling and -filter", false, OptionsCategory::OPTIONS);
30  gArgs.AddArg("-evals=<n>", strprintf("Number of measurement evaluations to perform. (default: %u)", DEFAULT_BENCH_EVALUATIONS), false, OptionsCategory::OPTIONS);
31  gArgs.AddArg("-filter=<regex>", strprintf("Regular expression filter to select benchmark by name (default: %s)", DEFAULT_BENCH_FILTER), false, OptionsCategory::OPTIONS);
32  gArgs.AddArg("-scaling=<n>", strprintf("Scaling factor for benchmark's runtime (default: %u)", DEFAULT_BENCH_SCALING), false, OptionsCategory::OPTIONS);
33  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), false, OptionsCategory::OPTIONS);
34  gArgs.AddArg("-plot-plotlyurl=<uri>", strprintf("URL to use for plotly.js (default: %s)", DEFAULT_PLOT_PLOTLYURL), false, OptionsCategory::OPTIONS);
35  gArgs.AddArg("-plot-width=<x>", strprintf("Plot width in pixel (default: %u)", DEFAULT_PLOT_WIDTH), false, OptionsCategory::OPTIONS);
36  gArgs.AddArg("-plot-height=<x>", strprintf("Plot height in pixel (default: %u)", DEFAULT_PLOT_HEIGHT), false, OptionsCategory::OPTIONS);
37 
38  // Hidden
39  gArgs.AddArg("-h", "", false, OptionsCategory::HIDDEN);
40  gArgs.AddArg("-help", "", false, OptionsCategory::HIDDEN);
41 }
42 
43 static fs::path SetDataDir()
44 {
45  fs::path ret = fs::temp_directory_path() / "bench_bitcoin" / fs::unique_path();
46  fs::create_directories(ret);
47  gArgs.ForceSetArg("-datadir", ret.string());
48  return ret;
49 }
50 
51 int main(int argc, char** argv)
52 {
53  SetupBenchArgs();
54  std::string error;
55  if (!gArgs.ParseParameters(argc, argv, error)) {
56  fprintf(stderr, "Error parsing command line arguments: %s\n", error.c_str());
57  return EXIT_FAILURE;
58  }
59 
60  if (HelpRequested(gArgs)) {
61  std::cout << gArgs.GetHelpMessage();
62 
63  return EXIT_SUCCESS;
64  }
65 
66  // Set the datadir after parsing the bench options
67  const fs::path bench_datadir{SetDataDir()};
68 
70  RandomInit();
71  ECC_Start();
73 
74  int64_t evaluations = gArgs.GetArg("-evals", DEFAULT_BENCH_EVALUATIONS);
75  std::string regex_filter = gArgs.GetArg("-filter", DEFAULT_BENCH_FILTER);
76  std::string scaling_str = gArgs.GetArg("-scaling", DEFAULT_BENCH_SCALING);
77  bool is_list_only = gArgs.GetBoolArg("-list", false);
78 
79  double scaling_factor;
80  if (!ParseDouble(scaling_str, &scaling_factor)) {
81  fprintf(stderr, "Error parsing scaling factor as double: %s\n", scaling_str.c_str());
82  return EXIT_FAILURE;
83  }
84 
85  std::unique_ptr<benchmark::Printer> printer = MakeUnique<benchmark::ConsolePrinter>();
86  std::string printer_arg = gArgs.GetArg("-printer", DEFAULT_BENCH_PRINTER);
87  if ("plot" == printer_arg) {
88  printer.reset(new benchmark::PlotlyPrinter(
89  gArgs.GetArg("-plot-plotlyurl", DEFAULT_PLOT_PLOTLYURL),
90  gArgs.GetArg("-plot-width", DEFAULT_PLOT_WIDTH),
91  gArgs.GetArg("-plot-height", DEFAULT_PLOT_HEIGHT)));
92  }
93 
94  benchmark::BenchRunner::RunAll(*printer, evaluations, scaling_factor, regex_filter, is_list_only);
95 
96  fs::remove_all(bench_datadir);
97 
98  ECC_Stop();
99 
100  return EXIT_SUCCESS;
101 }
void RandomInit()
Initialize the RNG.
Definition: random.cpp:466
bool HelpRequested(const ArgsManager &args)
Definition: util.cpp:662
void ECC_Start()
Initialize the elliptic curve support.
Definition: key.cpp:343
#define strprintf
Definition: tinyformat.h:1066
UniValue ret(UniValue::VARR)
Definition: rpcwallet.cpp:1140
const std::function< std::string(const char *)> G_TRANSLATION_FUN
Translate a message to the native language of the user.
bool ParseParameters(int argc, const char *const argv[], std::string &error)
Definition: util.cpp:411
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: util.cpp:542
std::string SHA256AutoDetect()
Autodetect the best available SHA256 implementation.
Definition: sha256.cpp:573
void ForceSetArg(const std::string &strArg, const std::string &strValue)
Definition: util.cpp:566
bool ParseDouble(const std::string &str, double *out)
Convert string to double with strict parse error feedback.
std::string GetHelpMessage() const
Get the help string.
Definition: util.cpp:593
void ECC_Stop()
Deinitialize the elliptic curve support.
Definition: key.cpp:360
static void RunAll(Printer &printer, uint64_t num_evals, double scaling, const std::string &filter, bool is_list_only)
Definition: bench.cpp:96
ArgsManager gArgs
Definition: util.cpp:88
void AddArg(const std::string &name, const std::string &help, const bool debug_only, const OptionsCategory &cat)
Add argument.
Definition: util.cpp:572
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: util.cpp:526
int main(int argc, char **argv)
bool error(const char *fmt, const Args &... args)
Definition: util.h:59
void SetupEnvironment()
Definition: util.cpp:1184