Bitcoin ABC  0.22.12
P2P Digital Currency
bench.h
Go to the documentation of this file.
1 // Copyright (c) 2015-2016 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 #ifndef BITCOIN_BENCH_BENCH_H
6 #define BITCOIN_BENCH_BENCH_H
7 
8 #include <chrono>
9 #include <functional>
10 #include <map>
11 #include <string>
12 #include <vector>
13 
14 #include <boost/preprocessor/cat.hpp>
15 #include <boost/preprocessor/stringize.hpp>
16 
17 struct RegTestingSetup;
19 extern const RegTestingSetup *g_testing_setup;
20 
21 // Simple micro-benchmarking framework; API mostly matches a subset of the
22 // Google Benchmark framework (see https://github.com/google/benchmark)
23 // Why not use the Google Benchmark framework? Because adding Yet Another
24 // Dependency (that uses cmake as its build system and has lots of features we
25 // don't need) isn't worth it.
26 
27 /*
28  * Usage:
29 
30 static void CODE_TO_TIME(benchmark::State& state)
31 {
32  ... do any setup needed...
33  while (state.KeepRunning()) {
34  ... do stuff you want to time...
35  }
36  ... do any cleanup needed...
37 }
38 
39 // default to running benchmark for 5000 iterations
40 BENCHMARK(CODE_TO_TIME, 5000);
41 
42  */
43 
44 namespace benchmark {
45 // In case high_resolution_clock is steady, prefer that, otherwise use
46 // steady_clock.
47 struct best_clock {
48  using hi_res_clock = std::chrono::high_resolution_clock;
49  using steady_clock = std::chrono::steady_clock;
50  using type = std::conditional<hi_res_clock::is_steady, hi_res_clock,
52 };
56 
57 class Printer;
58 
59 class State {
60 public:
61  std::string m_name;
62  uint64_t m_num_iters_left;
63  const uint64_t m_num_iters;
64  const uint64_t m_num_evals;
65  std::vector<double> m_elapsed_results;
67 
68  bool UpdateTimer(time_point finish_time);
69 
70  State(std::string name, uint64_t num_evals, double num_iters,
71  Printer &printer)
72  : m_name(name), m_num_iters_left(0), m_num_iters(num_iters),
73  m_num_evals(num_evals) {}
74 
75  inline bool KeepRunning() {
76  if (m_num_iters_left--) {
77  return true;
78  }
79 
80  bool result = UpdateTimer(clock::now());
81  // measure again so runtime of UpdateTimer is not included
82  m_start_time = clock::now();
83  return result;
84  }
85 };
86 
87 typedef std::function<void(State &)> BenchFunction;
88 
89 class BenchRunner {
90  struct Bench {
91  BenchFunction func;
93  };
94  typedef std::map<std::string, Bench> BenchmarkMap;
95  static BenchmarkMap &benchmarks();
96 
97 public:
98  BenchRunner(std::string name, BenchFunction func,
99  uint64_t num_iters_for_one_second);
100 
101  static void RunAll(Printer &printer, uint64_t num_evals, double scaling,
102  const std::string &filter, bool is_list_only);
103 };
104 
105 // interface to output benchmark results.
106 class Printer {
107 public:
108  virtual ~Printer() {}
109  virtual void header() = 0;
110  virtual void result(const State &state) = 0;
111  virtual void footer() = 0;
112 };
113 
114 // default printer to console, shows min, max, median.
115 class ConsolePrinter : public Printer {
116 public:
117  void header() override;
118  void result(const State &state) override;
119  void footer() override;
120 };
121 
122 // creates box plot with plotly.js
123 class PlotlyPrinter : public Printer {
124 public:
125  PlotlyPrinter(std::string plotly_url, int64_t width, int64_t height);
126  void header() override;
127  void result(const State &state) override;
128  void footer() override;
129 
130 private:
131  std::string m_plotly_url;
132  int64_t m_width;
133  int64_t m_height;
134 };
135 
136 // Junit compatible printer, allow to log durations on compatible CI
137 class JunitPrinter : public Printer {
138 public:
139  void header() override;
140  void result(const State &state) override;
141  void footer() override;
142 
143 private:
144  std::vector<std::pair<std::string, double>> bench_results;
146 };
147 } // namespace benchmark
148 
149 // BENCHMARK(foo, num_iters_for_one_second) expands to: benchmark::BenchRunner
150 // bench_11foo("foo", num_iterations);
151 // Choose a num_iters_for_one_second that takes roughly 1 second. The goal is
152 // that all benchmarks should take approximately
153 // the same time, and scaling factor can be used that the total time is
154 // appropriate for your system.
155 #define BENCHMARK(n, num_iters_for_one_second) \
156  benchmark::BenchRunner BOOST_PP_CAT(bench_, BOOST_PP_CAT(__LINE__, n))( \
157  BOOST_PP_STRINGIZE(n), n, (num_iters_for_one_second));
158 
159 #endif // BITCOIN_BENCH_BENCH_H
const uint64_t m_num_iters
Definition: bench.h:63
std::string m_plotly_url
Definition: bench.h:131
std::chrono::steady_clock steady_clock
Definition: bench.h:49
std::function< void(State &)> BenchFunction
Definition: bench.h:87
std::vector< double > m_elapsed_results
Definition: bench.h:65
bool KeepRunning()
Definition: bench.h:75
std::vector< std::pair< std::string, double > > bench_results
Definition: bench.h:144
time_point m_start_time
Definition: bench.h:66
uint64_t m_num_iters_left
Definition: bench.h:62
best_clock::type clock
Definition: bench.h:53
const char * name
Definition: rest.cpp:43
clock::duration duration
Definition: bench.h:55
BenchFunction func
Definition: bench.h:91
State(std::string name, uint64_t num_evals, double num_iters, Printer &printer)
Definition: bench.h:70
const uint64_t m_num_evals
Definition: bench.h:64
std::chrono::high_resolution_clock hi_res_clock
Definition: bench.h:48
std::map< std::string, Bench > BenchmarkMap
Definition: bench.h:94
std::conditional< hi_res_clock::is_steady, hi_res_clock, steady_clock >::type type
Definition: bench.h:51
std::string m_name
Definition: bench.h:61
uint64_t num_iters_for_one_second
Definition: bench.h:92
clock::time_point time_point
Definition: bench.h:54
const RegTestingSetup * g_testing_setup
A pointer to the current testing setup.
Definition: bench.cpp:19
virtual ~Printer()
Definition: bench.h:108