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