Bitcoin Core  29.1.0
P2P Digital Currency
bench.h
Go to the documentation of this file.
1 // Copyright (c) 2015-2022 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 <bench/nanobench.h> // IWYU pragma: export
9 #include <util/fs.h>
10 #include <util/macros.h>
11 
12 #include <chrono>
13 #include <cstdint>
14 #include <functional>
15 #include <map>
16 #include <string>
17 #include <utility>
18 #include <vector>
19 
20 /*
21  * Usage:
22 
23 static void NameOfYourBenchmarkFunction(benchmark::Bench& bench)
24 {
25  ...do any setup needed...
26 
27  bench.run([&] {
28  ...do stuff you want to time; refer to src/bench/nanobench.h
29  for more information and the options that can be passed here...
30  });
31 
32  ...do any cleanup needed...
33 }
34 
35 BENCHMARK(NameOfYourBenchmarkFunction);
36 
37  */
38 
39 namespace benchmark {
40 
42 
43 typedef std::function<void(Bench&)> BenchFunction;
44 
45 enum PriorityLevel : uint8_t
46 {
47  LOW = 1 << 0,
48  HIGH = 1 << 2,
49 };
50 
51 // List priority labels, comma-separated and sorted by increasing priority
52 std::string ListPriorities();
53 uint8_t StringToPriority(const std::string& str);
54 
55 struct Args {
58  std::chrono::milliseconds min_time;
59  std::vector<double> asymptote;
62  std::string regex_filter;
63  uint8_t priority;
64  std::vector<std::string> setup_args;
65 };
66 
68 {
69  // maps from "name" -> (function, priority_level)
70  typedef std::map<std::string, std::pair<BenchFunction, PriorityLevel>> BenchmarkMap;
71  static BenchmarkMap& benchmarks();
72 
73 public:
74  BenchRunner(std::string name, BenchFunction func, PriorityLevel level);
75 
76  static void RunAll(const Args& args);
77 };
78 } // namespace benchmark
79 
80 // BENCHMARK(foo) expands to: benchmark::BenchRunner bench_11foo("foo", foo, priority_level);
81 #define BENCHMARK(n, priority_level) \
82  benchmark::BenchRunner PASTE2(bench_, PASTE2(__LINE__, n))(STRINGIZE(n), n, priority_level);
83 
84 #endif // BITCOIN_BENCH_BENCH_H
std::chrono::milliseconds min_time
Definition: bench.h:58
std::vector< double > asymptote
Definition: bench.h:59
PriorityLevel
Definition: bench.h:45
uint8_t StringToPriority(const std::string &str)
Definition: bench.cpp:87
static BenchmarkMap & benchmarks()
Definition: bench.cpp:94
ArgsManager & args
Definition: bitcoind.cpp:277
const char * name
Definition: rest.cpp:49
fs::path output_json
Definition: bench.h:61
std::string ListPriorities()
Definition: bench.cpp:79
std::function< void(Bench &)> BenchFunction
Definition: bench.h:43
uint8_t priority
Definition: bench.h:63
static void RunAll(const Args &args)
Definition: bench.cpp:105
std::map< std::string, std::pair< BenchFunction, PriorityLevel > > BenchmarkMap
Definition: bench.h:70
bool sanity_check
Definition: bench.h:57
bool is_list_only
Definition: bench.h:56
fs::path output_csv
Definition: bench.h:60
Main entry point to nanobench&#39;s benchmarking facility.
Definition: nanobench.h:627
std::string regex_filter
Definition: bench.h:62
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:32
std::vector< std::string > setup_args
Definition: bench.h:64
BenchRunner(std::string name, BenchFunction func, PriorityLevel level)
Definition: bench.cpp:100