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 #include <libethcore/SealEngine.h>
10 
11 #include <memory>
12 
13 static const int64_t DEFAULT_BENCH_EVALUATIONS = 5;
14 static const char* DEFAULT_BENCH_FILTER = ".*";
15 static const char* DEFAULT_BENCH_SCALING = "1.0";
16 static const char* DEFAULT_BENCH_PRINTER = "console";
17 static const char* DEFAULT_PLOT_PLOTLYURL = "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 
SetupBenchArgs()21 static void SetupBenchArgs()
22 {
23     SetupHelpOptions(gArgs);
24 
25     gArgs.AddArg("-list", "List benchmarks without executing them. Can be combined with -scaling and -filter", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
26     gArgs.AddArg("-evals=<n>", strprintf("Number of measurement evaluations to perform. (default: %u)", DEFAULT_BENCH_EVALUATIONS), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
27     gArgs.AddArg("-filter=<regex>", strprintf("Regular expression filter to select benchmark by name (default: %s)", DEFAULT_BENCH_FILTER), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
28     gArgs.AddArg("-scaling=<n>", strprintf("Scaling factor for benchmark's runtime (default: %u)", DEFAULT_BENCH_SCALING), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
29     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);
30     gArgs.AddArg("-plot-plotlyurl=<uri>", strprintf("URL to use for plotly.js (default: %s)", DEFAULT_PLOT_PLOTLYURL), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
31     gArgs.AddArg("-plot-width=<x>", strprintf("Plot width in pixel (default: %u)", DEFAULT_PLOT_WIDTH), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
32     gArgs.AddArg("-plot-height=<x>", strprintf("Plot height in pixel (default: %u)", DEFAULT_PLOT_HEIGHT), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
33 }
34 
main(int argc,char ** argv)35 int main(int argc, char** argv)
36 {
37     SetupBenchArgs();
38     std::string error;
39     if (!gArgs.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(gArgs)) {
45         std::cout << gArgs.GetHelpMessage();
46 
47         return EXIT_SUCCESS;
48     }
49 
50     int64_t evaluations = gArgs.GetArg("-evals", DEFAULT_BENCH_EVALUATIONS);
51     std::string regex_filter = gArgs.GetArg("-filter", DEFAULT_BENCH_FILTER);
52     std::string scaling_str = gArgs.GetArg("-scaling", DEFAULT_BENCH_SCALING);
53     bool is_list_only = gArgs.GetBoolArg("-list", false);
54 
55     // Overwrite arguments for bench
56     gArgs.SoftSetBoolArg("-acceptnonstdtxn", true);
57 
58     if (evaluations == 0) {
59         return EXIT_SUCCESS;
60     } else if (evaluations < 0) {
61         tfm::format(std::cerr, "Error parsing evaluations argument: %d\n", evaluations);
62         return EXIT_FAILURE;
63     }
64 
65     double scaling_factor;
66     if (!ParseDouble(scaling_str, &scaling_factor)) {
67         tfm::format(std::cerr, "Error parsing scaling factor as double: %s\n", scaling_str);
68         return EXIT_FAILURE;
69     }
70 
71     std::unique_ptr<benchmark::Printer> printer = MakeUnique<benchmark::ConsolePrinter>();
72     std::string printer_arg = gArgs.GetArg("-printer", DEFAULT_BENCH_PRINTER);
73     if ("plot" == printer_arg) {
74         printer.reset(new benchmark::PlotlyPrinter(
75             gArgs.GetArg("-plot-plotlyurl", DEFAULT_PLOT_PLOTLYURL),
76             gArgs.GetArg("-plot-width", DEFAULT_PLOT_WIDTH),
77             gArgs.GetArg("-plot-height", DEFAULT_PLOT_HEIGHT)));
78     }
79 
80     benchmark::BenchRunner::RunAll(*printer, evaluations, scaling_factor, regex_filter, is_list_only);
81 
82     return EXIT_SUCCESS;
83 }
84