Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

bench.h
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 #ifndef BITCOIN_BENCH_BENCH_H
6 #define BITCOIN_BENCH_BENCH_H
7 
8 #include <functional>
9 #include <limits>
10 #include <map>
11 #include <string>
12 #include <chrono>
13 
14 #include <boost/preprocessor/cat.hpp>
15 #include <boost/preprocessor/stringize.hpp>
16 
17 // Simple micro-benchmarking framework; API mostly matches a subset of the Google Benchmark
18 // framework (see https://github.com/google/benchmark)
19 // Why not use the Google Benchmark framework? Because adding Yet Another Dependency
20 // (that uses cmake as its build system and has lots of features we don't need) isn't
21 // worth it.
22 
23 /*
24  * Usage:
25 
26 static void CODE_TO_TIME(benchmark::State& state)
27 {
28  ... do any setup needed...
29  while (state.KeepRunning()) {
30  ... do stuff you want to time...
31  }
32  ... do any cleanup needed...
33 }
34 
35 BENCHMARK(CODE_TO_TIME);
36 
37  */
38 
39 namespace benchmark {
40  // In case high_resolution_clock is steady, prefer that, otherwise use steady_clock.
41  struct best_clock {
42  using hi_res_clock = std::chrono::high_resolution_clock;
43  using steady_clock = std::chrono::steady_clock;
44  using type = std::conditional<hi_res_clock::is_steady, hi_res_clock, steady_clock>::type;
45  };
49 
50  class State {
51  std::string name;
55  uint64_t count;
56  uint64_t countMask;
57  uint64_t beginCycles;
58  uint64_t lastCycles;
59  uint64_t minCycles;
60  uint64_t maxCycles;
61  public:
62  State(std::string _name, duration _maxElapsed) :
63  name(_name),
64  maxElapsed(_maxElapsed),
65  minTime(duration::max()),
66  maxTime(duration::zero()),
67  count(0),
68  countMask(1),
69  beginCycles(0),
70  lastCycles(0),
71  minCycles(std::numeric_limits<uint64_t>::max()),
72  maxCycles(std::numeric_limits<uint64_t>::min()) {
73  }
74  bool KeepRunning();
75  };
76 
77  typedef std::function<void(State&)> BenchFunction;
78 
80  {
81  typedef std::map<std::string, BenchFunction> BenchmarkMap;
82  static BenchmarkMap &benchmarks();
83 
84  public:
85  BenchRunner(std::string name, BenchFunction func);
86 
87  static void RunAll(duration elapsedTimeForOne = std::chrono::seconds(1));
88  };
89 }
90 
91 // BENCHMARK(foo) expands to: benchmark::BenchRunner bench_11foo("foo", foo);
92 #define BENCHMARK(n) \
93  benchmark::BenchRunner BOOST_PP_CAT(bench_, BOOST_PP_CAT(__LINE__, n))(BOOST_PP_STRINGIZE(n), n);
94 
95 #endif // BITCOIN_BENCH_BENCH_H
uint64_t beginCycles
Definition: bench.h:57
BenchRunner(std::string name, BenchFunction func)
Definition: bench.cpp:17
uint64_t minCycles
Definition: bench.h:59
time_point beginTime
Definition: bench.h:53
std::chrono::steady_clock steady_clock
Definition: bench.h:43
uint64_t count
Definition: bench.h:55
uint64_t maxCycles
Definition: bench.h:60
bool KeepRunning()
Definition: bench.cpp:39
Definition: box.hpp:161
uint64_t lastCycles
Definition: bench.h:58
best_clock::type clock
Definition: bench.h:46
time_point lastTime
Definition: bench.h:53
duration maxTime
Definition: bench.h:54
const char * name
Definition: rest.cpp:36
clock::duration duration
Definition: bench.h:48
std::map< std::string, BenchFunction > BenchmarkMap
Definition: bench.h:81
duration minTime
Definition: bench.h:54
std::function< void(State &)> BenchFunction
Definition: bench.h:77
std::chrono::high_resolution_clock hi_res_clock
Definition: bench.h:42
std::conditional< hi_res_clock::is_steady, hi_res_clock, steady_clock >::type type
Definition: bench.h:44
uint64_t countMask
Definition: bench.h:56
State(std::string _name, duration _maxElapsed)
Definition: bench.h:62
duration maxElapsed
Definition: bench.h:52
clock::time_point time_point
Definition: bench.h:47
static BenchmarkMap & benchmarks()
Definition: bench.cpp:12
std::string name
Definition: bench.h:51
static void RunAll(duration elapsedTimeForOne=std::chrono::seconds(1))
Definition: bench.cpp:23
Released under the MIT license