Bitcoin Core  0.20.99
P2P Digital Currency
bench.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 <chainparams.h>
8 #include <test/util/setup_common.h>
9 #include <validation.h>
10 
11 #include <algorithm>
12 #include <assert.h>
13 #include <iomanip>
14 #include <iostream>
15 #include <numeric>
16 #include <regex>
17 
18 const std::function<void(const std::string&)> G_TEST_LOG_FUN{};
19 
21 {
22  std::cout << "# Benchmark, evals, iterations, total, min, max, median" << std::endl;
23 }
24 
26 {
27  auto results = state.m_elapsed_results;
28  std::sort(results.begin(), results.end());
29 
30  double total = state.m_num_iters * std::accumulate(results.begin(), results.end(), 0.0);
31 
32  double front = 0;
33  double back = 0;
34  double median = 0;
35 
36  if (!results.empty()) {
37  front = results.front();
38  back = results.back();
39 
40  size_t mid = results.size() / 2;
41  median = results[mid];
42  if (0 == results.size() % 2) {
43  median = (results[mid] + results[mid + 1]) / 2;
44  }
45  }
46 
47  std::cout << std::setprecision(6);
48  std::cout << state.m_name << ", " << state.m_num_evals << ", " << state.m_num_iters << ", " << total << ", " << front << ", " << back << ", " << median << std::endl;
49 }
50 
52 benchmark::PlotlyPrinter::PlotlyPrinter(std::string plotly_url, int64_t width, int64_t height)
53  : m_plotly_url(plotly_url), m_width(width), m_height(height)
54 {
55 }
56 
58 {
59  std::cout << "<html><head>"
60  << "<script src=\"" << m_plotly_url << "\"></script>"
61  << "</head><body><div id=\"myDiv\" style=\"width:" << m_width << "px; height:" << m_height << "px\"></div>"
62  << "<script> var data = ["
63  << std::endl;
64 }
65 
67 {
68  std::cout << "{ " << std::endl
69  << " name: '" << state.m_name << "', " << std::endl
70  << " y: [";
71 
72  const char* prefix = "";
73  for (const auto& e : state.m_elapsed_results) {
74  std::cout << prefix << std::setprecision(6) << e;
75  prefix = ", ";
76  }
77  std::cout << "]," << std::endl
78  << " boxpoints: 'all', jitter: 0.3, pointpos: 0, type: 'box',"
79  << std::endl
80  << "}," << std::endl;
81 }
82 
84 {
85  std::cout << "]; var layout = { showlegend: false, yaxis: { rangemode: 'tozero', autorange: true } };"
86  << "Plotly.newPlot('myDiv', data, layout);"
87  << "</script></body></html>";
88 }
89 
90 
92 {
93  static std::map<std::string, Bench> benchmarks_map;
94  return benchmarks_map;
95 }
96 
97 benchmark::BenchRunner::BenchRunner(std::string name, benchmark::BenchFunction func, uint64_t num_iters_for_one_second)
98 {
99  benchmarks().insert(std::make_pair(name, Bench{func, num_iters_for_one_second}));
100 }
101 
102 void benchmark::BenchRunner::RunAll(Printer& printer, uint64_t num_evals, double scaling, const std::string& filter, bool is_list_only)
103 {
104  if (!std::ratio_less_equal<benchmark::clock::period, std::micro>::value) {
105  std::cerr << "WARNING: Clock precision is worse than microsecond - benchmarks may be less accurate!\n";
106  }
107 #ifdef DEBUG
108  std::cerr << "WARNING: This is a debug build - may result in slower benchmarks.\n";
109 #endif
110 
111  std::regex reFilter(filter);
112  std::smatch baseMatch;
113 
114  printer.header();
115 
116  for (const auto& p : benchmarks()) {
117  if (!std::regex_match(p.first, baseMatch, reFilter)) {
118  continue;
119  }
120 
121  uint64_t num_iters = static_cast<uint64_t>(p.second.num_iters_for_one_second * scaling);
122  if (0 == num_iters) {
123  num_iters = 1;
124  }
125  State state(p.first, num_evals, num_iters, printer);
126  if (!is_list_only) {
127  p.second.func(state);
128  }
129  printer.result(state);
130  }
131 
132  printer.footer();
133 }
134 
136 {
137  if (m_start_time != time_point()) {
138  std::chrono::duration<double> diff = current_time - m_start_time;
139  m_elapsed_results.push_back(diff.count() / m_num_iters);
140 
141  if (m_elapsed_results.size() == m_num_evals) {
142  return false;
143  }
144  }
145 
146  m_num_iters_left = m_num_iters - 1;
147  return true;
148 }
void result(const State &state) override
Definition: bench.cpp:25
BenchRunner(std::string name, BenchFunction func, uint64_t num_iters_for_one_second)
Definition: bench.cpp:97
const uint64_t m_num_iters
Definition: bench.h:58
void result(const State &state) override
Definition: bench.cpp:66
std::string m_plotly_url
Definition: bench.h:128
const std::function< void(const std::string &)> G_TEST_LOG_FUN
Definition: bench.cpp:18
const char * prefix
Definition: rest.cpp:648
std::vector< double > m_elapsed_results
Definition: bench.h:60
virtual void result(const State &state)=0
bool UpdateTimer(time_point finish_time)
Definition: bench.cpp:135
void header() override
Definition: bench.cpp:57
void footer() override
Definition: bench.cpp:83
void header() override
Definition: bench.cpp:20
const char * name
Definition: rest.cpp:41
virtual void header()=0
void footer() override
Definition: bench.cpp:51
const uint64_t m_num_evals
Definition: bench.h:59
std::function< void(State &)> BenchFunction
Definition: bench.h:82
std::map< std::string, Bench > BenchmarkMap
Definition: bench.h:90
virtual void footer()=0
static void RunAll(Printer &printer, uint64_t num_evals, double scaling, const std::string &filter, bool is_list_only)
Definition: bench.cpp:102
std::string m_name
Definition: bench.h:56
clock::time_point time_point
Definition: bench.h:48
static BenchmarkMap & benchmarks()
Definition: bench.cpp:91
PlotlyPrinter(std::string plotly_url, int64_t width, int64_t height)
Definition: bench.cpp:52