1 // Copyright (c) 2015-2020 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 <bench/nanobench.h> 15 #include <boost/preprocessor/cat.hpp> 16 #include <boost/preprocessor/stringize.hpp> 17 18 /* 19 * Usage: 20 21 static void CODE_TO_TIME(benchmark::Bench& bench) 22 { 23 ... do any setup needed... 24 nanobench::Config().run([&] { 25 ... do stuff you want to time... 26 }); 27 ... do any cleanup needed... 28 } 29 30 BENCHMARK(CODE_TO_TIME); 31 32 */ 33 34 namespace benchmark { 35 36 using ankerl::nanobench::Bench; 37 38 typedef std::function<void(Bench&)> BenchFunction; 39 40 struct Args { 41 std::string regex_filter; 42 bool is_list_only; 43 std::vector<double> asymptote; 44 std::string output_csv; 45 std::string output_json; 46 }; 47 48 class BenchRunner 49 { 50 typedef std::map<std::string, BenchFunction> BenchmarkMap; 51 static BenchmarkMap& benchmarks(); 52 53 public: 54 BenchRunner(std::string name, BenchFunction func); 55 56 static void RunAll(const Args& args); 57 }; 58 } 59 // BENCHMARK(foo) expands to: benchmark::BenchRunner bench_11foo("foo"); 60 #define BENCHMARK(n) \ 61 benchmark::BenchRunner BOOST_PP_CAT(bench_, BOOST_PP_CAT(__LINE__, n))(BOOST_PP_STRINGIZE(n), n); 62 63 #endif // BITCOIN_BENCH_BENCH_H 64