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.
5 #ifndef BITCOIN_BENCH_BENCH_H
6 #define BITCOIN_BENCH_BENCH_H
13 #include <boost/preprocessor/cat.hpp>
14 #include <boost/preprocessor/stringize.hpp>
16 // Simple micro-benchmarking framework; API mostly matches a subset of the Google Benchmark
17 // framework (see https://github.com/google/benchmark)
18 // Why not use the Google Benchmark framework? Because adding Yet Another Dependency
19 // (that uses cmake as its build system and has lots of features we don't need) isn't
25 static void CODE_TO_TIME(benchmark::State& state)
27 ... do any setup needed...
28 while (state.KeepRunning()) {
29 ... do stuff you want to time...
31 ... do any cleanup needed...
34 BENCHMARK(CODE_TO_TIME);
44 double lastTime
, minTime
, maxTime
, countMaskInv
;
52 State(std::string _name
, double _maxElapsed
) : name(_name
), maxElapsed(_maxElapsed
), count(0) {
53 minTime
= std::numeric_limits
<double>::max();
54 maxTime
= std::numeric_limits
<double>::min();
55 minCycles
= std::numeric_limits
<uint64_t>::max();
56 maxCycles
= std::numeric_limits
<uint64_t>::min();
58 countMaskInv
= 1./(countMask
+ 1);
63 typedef std::function
<void(State
&)> BenchFunction
;
67 typedef std::map
<std::string
, BenchFunction
> BenchmarkMap
;
68 static BenchmarkMap
&benchmarks();
71 BenchRunner(std::string name
, BenchFunction func
);
73 static void RunAll(double elapsedTimeForOne
=1.0);
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);
81 #endif // BITCOIN_BENCH_BENCH_H