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