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