Raven Core  3.0.0
P2P Digital Currency
bench.h
Go to the documentation of this file.
1 // Copyright (c) 2015-2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef RAVEN_BENCH_BENCH_H
7 #define RAVEN_BENCH_BENCH_H
8 
9 #include <functional>
10 #include <limits>
11 #include <map>
12 #include <string>
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 
41  class State {
42  std::string name;
43  double maxElapsed;
44  double beginTime;
46  uint64_t count;
47  uint64_t countMask;
48  uint64_t beginCycles;
49  uint64_t lastCycles;
50  uint64_t minCycles;
51  uint64_t maxCycles;
52  public:
53  State(std::string _name, double _maxElapsed) : name(_name), maxElapsed(_maxElapsed), count(0) {
54  minTime = std::numeric_limits<double>::max();
55  maxTime = std::numeric_limits<double>::min();
56  minCycles = std::numeric_limits<uint64_t>::max();
57  maxCycles = std::numeric_limits<uint64_t>::min();
58  countMask = 1;
59  }
60  bool KeepRunning();
61  };
62 
63  typedef std::function<void(State&)> BenchFunction;
64 
66  {
67  typedef std::map<std::string, BenchFunction> BenchmarkMap;
68  static BenchmarkMap &benchmarks();
69 
70  public:
71  BenchRunner(std::string name, BenchFunction func);
72 
73  static void RunAll(double elapsedTimeForOne=1.0);
74  };
75 }
76 
77 // BENCHMARK(foo) expands to: benchmark::BenchRunner bench_11foo("foo", foo);
78 #define BENCHMARK(n) \
79  benchmark::BenchRunner BOOST_PP_CAT(bench_, BOOST_PP_CAT(__LINE__, n))(BOOST_PP_STRINGIZE(n), n);
80 
81 #endif // RAVEN_BENCH_BENCH_H
uint64_t beginCycles
Definition: bench.h:48
double beginTime
Definition: bench.h:44
uint64_t minCycles
Definition: bench.h:50
uint64_t count
Definition: bench.h:46
uint64_t maxCycles
Definition: bench.h:51
bool KeepRunning()
Definition: bench.cpp:44
double lastTime
Definition: bench.h:45
State(std::string _name, double _maxElapsed)
Definition: bench.h:53
uint64_t lastCycles
Definition: bench.h:49
double minTime
Definition: bench.h:45
std::map< std::string, BenchFunction > BenchmarkMap
Definition: bench.h:67
double maxTime
Definition: bench.h:45
std::function< void(State &)> BenchFunction
Definition: bench.h:63
uint64_t countMask
Definition: bench.h:47
std::string name
Definition: bench.h:42
double maxElapsed
Definition: bench.h:43