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 <util/system.h>
10 #include <util/strencodings.h>
11 #include <validation.h>
12 
13 #include <memory>
14 
15 const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
16 
17 static const int64_t DEFAULT_BENCH_EVALUATIONS = 5;
18 static const char* DEFAULT_BENCH_FILTER = ".*";
19 static const char* DEFAULT_BENCH_SCALING = "1.0";
20 static const char* DEFAULT_BENCH_PRINTER = "console";
21 static const char* DEFAULT_PLOT_PLOTLYURL = "https://cdn.plot.ly/plotly-latest.min.js";
22 static const int64_t DEFAULT_PLOT_WIDTH = 1024;
23 static const int64_t DEFAULT_PLOT_HEIGHT = 768;
24 
SetupBenchArgs()25 static void SetupBenchArgs()
26 {
27     SetupHelpOptions(gArgs);
28 
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 
SetDataDir()39 static fs::path SetDataDir()
40 {
41     fs::path ret = fs::temp_directory_path() / "bench_litecoin" / fs::unique_path();
42     fs::create_directories(ret);
43     gArgs.ForceSetArg("-datadir", ret.string());
44     return ret;
45 }
46 
main(int argc,char ** argv)47 int main(int argc, char** argv)
48 {
49     SetupBenchArgs();
50     std::string error;
51     if (!gArgs.ParseParameters(argc, argv, error)) {
52         tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error.c_str());
53         return EXIT_FAILURE;
54     }
55 
56     if (HelpRequested(gArgs)) {
57         std::cout << gArgs.GetHelpMessage();
58 
59         return EXIT_SUCCESS;
60     }
61 
62     // Set the datadir after parsing the bench options
63     const fs::path bench_datadir{SetDataDir()};
64 
65     SHA256AutoDetect();
66     ECC_Start();
67     SetupEnvironment();
68 
69     int64_t evaluations = gArgs.GetArg("-evals", DEFAULT_BENCH_EVALUATIONS);
70     std::string regex_filter = gArgs.GetArg("-filter", DEFAULT_BENCH_FILTER);
71     std::string scaling_str = gArgs.GetArg("-scaling", DEFAULT_BENCH_SCALING);
72     bool is_list_only = gArgs.GetBoolArg("-list", false);
73 
74     double scaling_factor;
75     if (!ParseDouble(scaling_str, &scaling_factor)) {
76         tfm::format(std::cerr, "Error parsing scaling factor as double: %s\n", scaling_str.c_str());
77         return EXIT_FAILURE;
78     }
79 
80     std::unique_ptr<benchmark::Printer> printer = MakeUnique<benchmark::ConsolePrinter>();
81     std::string printer_arg = gArgs.GetArg("-printer", DEFAULT_BENCH_PRINTER);
82     if ("plot" == printer_arg) {
83         printer.reset(new benchmark::PlotlyPrinter(
84             gArgs.GetArg("-plot-plotlyurl", DEFAULT_PLOT_PLOTLYURL),
85             gArgs.GetArg("-plot-width", DEFAULT_PLOT_WIDTH),
86             gArgs.GetArg("-plot-height", DEFAULT_PLOT_HEIGHT)));
87     }
88 
89     benchmark::BenchRunner::RunAll(*printer, evaluations, scaling_factor, regex_filter, is_list_only);
90 
91     fs::remove_all(bench_datadir);
92 
93     ECC_Stop();
94 
95     return EXIT_SUCCESS;
96 }
97