Dash Core  0.12.2.1
P2P Digital Currency
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 <map>
9 #include <string>
10 
11 #include <boost/function.hpp>
12 #include <boost/preprocessor/cat.hpp>
13 #include <boost/preprocessor/stringize.hpp>
14 
15 // Simple micro-benchmarking framework; API mostly matches a subset of the Google Benchmark
16 // framework (see https://github.com/google/benchmark)
17 // Wny not use the Google Benchmark framework? Because adding Yet Another Dependency
18 // (that uses cmake as its build system and has lots of features we don't need) isn't
19 // worth it.
20 
21 /*
22  * Usage:
23 
24 static void CODE_TO_TIME(benchmark::State& state)
25 {
26  ... do any setup needed...
27  while (state.KeepRunning()) {
28  ... do stuff you want to time...
29  }
30  ... do any cleanup needed...
31 }
32 
33 BENCHMARK(CODE_TO_TIME);
34 
35  */
36 
37 namespace benchmark {
38 
39  class State {
40  std::string name;
41  double maxElapsed;
42  double beginTime;
44  int64_t count;
45  int64_t timeCheckCount;
46  public:
47  State(std::string _name, double _maxElapsed) : name(_name), maxElapsed(_maxElapsed), count(0) {
48  minTime = std::numeric_limits<double>::max();
49  maxTime = std::numeric_limits<double>::min();
50  timeCheckCount = 1;
51  }
52  bool KeepRunning();
53  };
54 
55  typedef boost::function<void(State&)> BenchFunction;
56 
58  {
59  static std::map<std::string, BenchFunction> benchmarks;
60 
61  public:
62  BenchRunner(std::string name, BenchFunction func);
63 
64  static void RunAll(double elapsedTimeForOne=1.0);
65  };
66 }
67 
68 // BENCHMARK(foo) expands to: benchmark::BenchRunner bench_11foo("foo", foo);
69 #define BENCHMARK(n) \
70  benchmark::BenchRunner BOOST_PP_CAT(bench_, BOOST_PP_CAT(__LINE__, n))(BOOST_PP_STRINGIZE(n), n);
71 
72 #endif // BITCOIN_BENCH_BENCH_H
double beginTime
Definition: bench.h:42
std::string name
Definition: bench.h:40
int64_t timeCheckCount
Definition: bench.h:45
bool KeepRunning()
Definition: bench.cpp:39
boost::function< void(State &)> BenchFunction
Definition: bench.h:55
double maxTime
Definition: bench.h:43
double lastTime
Definition: bench.h:43
double minTime
Definition: bench.h:43
const char * name
Definition: rest.cpp:37
int64_t count
Definition: bench.h:44
State(std::string _name, double _maxElapsed)
Definition: bench.h:47
static std::map< std::string, BenchFunction > benchmarks
Definition: bench.h:59
double maxElapsed
Definition: bench.h:41
static void RunAll(double elapsedTimeForOne=1.0)
Definition: bench.cpp:26
BenchRunner(std::string name, BenchFunction func)
Definition: bench.cpp:20