Bitcoin Core  0.19.99
P2P Digital Currency
bench.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 <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 RegTestingSetup* g_testing_setup = nullptr;
19 const std::function<void(const std::string&)> G_TEST_LOG_FUN{};
20 
22 {
23  std::cout << "# Benchmark, evals, iterations, total, min, max, median" << std::endl;
24 }
25 
27 {
28  auto results = state.m_elapsed_results;
29  std::sort(results.begin(), results.end());
30 
31  double total = state.m_num_iters * std::accumulate(results.begin(), results.end(), 0.0);
32 
33  double front = 0;
34  double back = 0;
35  double median = 0;
36 
37  if (!results.empty()) {
38  front = results.front();
39  back = results.back();
40 
41  size_t mid = results.size() / 2;
42  median = results[mid];
43  if (0 == results.size() % 2) {
44  median = (results[mid] + results[mid + 1]) / 2;
45  }
46  }
47 
48  std::cout << std::setprecision(6);
49  std::cout << state.m_name << ", " << state.m_num_evals << ", " << state.m_num_iters << ", " << total << ", " << front << ", " << back << ", " << median << std::endl;
50 }
51 
53 benchmark::PlotlyPrinter::PlotlyPrinter(std::string plotly_url, int64_t width, int64_t height)
54  : m_plotly_url(plotly_url), m_width(width), m_height(height)
55 {
56 }
57 
59 {
60  std::cout << "<html><head>"
61  << "<script src=\"" << m_plotly_url << "\"></script>"
62  << "</head><body><div id=\"myDiv\" style=\"width:" << m_width << "px; height:" << m_height << "px\"></div>"
63  << "<script> var data = ["
64  << std::endl;
65 }
66 
68 {
69  std::cout << "{ " << std::endl
70  << " name: '" << state.m_name << "', " << std::endl
71  << " y: [";
72 
73  const char* prefix = "";
74  for (const auto& e : state.m_elapsed_results) {
75  std::cout << prefix << std::setprecision(6) << e;
76  prefix = ", ";
77  }
78  std::cout << "]," << std::endl
79  << " boxpoints: 'all', jitter: 0.3, pointpos: 0, type: 'box',"
80  << std::endl
81  << "}," << std::endl;
82 }
83 
85 {
86  std::cout << "]; var layout = { showlegend: false, yaxis: { rangemode: 'tozero', autorange: true } };"
87  << "Plotly.newPlot('myDiv', data, layout);"
88  << "</script></body></html>";
89 }
90 
91 
93 {
94  static std::map<std::string, Bench> benchmarks_map;
95  return benchmarks_map;
96 }
97 
98 benchmark::BenchRunner::BenchRunner(std::string name, benchmark::BenchFunction func, uint64_t num_iters_for_one_second)
99 {
100  benchmarks().insert(std::make_pair(name, Bench{func, num_iters_for_one_second}));
101 }
102 
103 void benchmark::BenchRunner::RunAll(Printer& printer, uint64_t num_evals, double scaling, const std::string& filter, bool is_list_only)
104 {
105  if (!std::ratio_less_equal<benchmark::clock::period, std::micro>::value) {
106  std::cerr << "WARNING: Clock precision is worse than microsecond - benchmarks may be less accurate!\n";
107  }
108 #ifdef DEBUG
109  std::cerr << "WARNING: This is a debug build - may result in slower benchmarks.\n";
110 #endif
111 
112  std::regex reFilter(filter);
113  std::smatch baseMatch;
114 
115  printer.header();
116 
117  for (const auto& p : benchmarks()) {
118  RegTestingSetup test{};
119  assert(g_testing_setup == nullptr);
120  g_testing_setup = &test;
121  {
122  LOCK(cs_main);
123  assert(::ChainActive().Height() == 0);
124  const bool witness_enabled{IsWitnessEnabled(::ChainActive().Tip(), Params().GetConsensus())};
125  assert(witness_enabled);
126  }
127 
128  if (!std::regex_match(p.first, baseMatch, reFilter)) {
129  g_testing_setup = nullptr;
130  continue;
131  }
132 
133  uint64_t num_iters = static_cast<uint64_t>(p.second.num_iters_for_one_second * scaling);
134  if (0 == num_iters) {
135  num_iters = 1;
136  }
137  State state(p.first, num_evals, num_iters, printer);
138  if (!is_list_only) {
139  p.second.func(state);
140  }
141  printer.result(state);
142  g_testing_setup = nullptr;
143  }
144 
145  printer.footer();
146 }
147 
149 {
150  if (m_start_time != time_point()) {
151  std::chrono::duration<double> diff = current_time - m_start_time;
152  m_elapsed_results.push_back(diff.count() / m_num_iters);
153 
154  if (m_elapsed_results.size() == m_num_evals) {
155  return false;
156  }
157  }
158 
159  m_num_iters_left = m_num_iters - 1;
160  return true;
161 }
void result(const State &state) override
Definition: bench.cpp:26
BenchRunner(std::string name, BenchFunction func, uint64_t num_iters_for_one_second)
Definition: bench.cpp:98
const uint64_t m_num_iters
Definition: bench.h:61
CChain & ChainActive()
Definition: validation.cpp:91
void result(const State &state) override
Definition: bench.cpp:67
std::string m_plotly_url
Definition: bench.h:131
const std::function< void(const std::string &)> G_TEST_LOG_FUN
Definition: bench.cpp:19
const char * prefix
Definition: rest.cpp:650
std::vector< double > m_elapsed_results
Definition: bench.h:63
virtual void result(const State &state)=0
bool UpdateTimer(time_point finish_time)
Definition: bench.cpp:148
void header() override
Definition: bench.cpp:58
void footer() override
Definition: bench.cpp:84
void header() override
Definition: bench.cpp:21
const RegTestingSetup * g_testing_setup
A pointer to the current testing setup.
Definition: bench.cpp:18
#define LOCK(cs)
Definition: sync.h:179
const char * name
Definition: rest.cpp:40
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: validation.cpp:106
virtual void header()=0
void footer() override
Definition: bench.cpp:52
const uint64_t m_num_evals
Definition: bench.h:62
std::function< void(State &)> BenchFunction
Definition: bench.h:85
std::map< std::string, Bench > BenchmarkMap
Definition: bench.h:93
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:103
std::string m_name
Definition: bench.h:59
const CChainParams & Params()
Return the currently selected parameters.
clock::time_point time_point
Definition: bench.h:51
static BenchmarkMap & benchmarks()
Definition: bench.cpp:92
bool IsWitnessEnabled(const CBlockIndex *pindexPrev, const Consensus::Params &params)
Check whether witness commitments are required for a block, and whether to enforce NULLDUMMY (BIP 147...
PlotlyPrinter(std::string plotly_url, int64_t width, int64_t height)
Definition: bench.cpp:53