Bitcoin Core 28.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 <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
23static 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
35BENCHMARK(NameOfYourBenchmarkFunction);
36
37 */
38
39namespace benchmark {
40
42
43typedef std::function<void(Bench&)> BenchFunction;
44
45enum 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
52std::string ListPriorities();
53uint8_t StringToPriority(const std::string& str);
54
55struct 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
73public:
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
ArgsManager & args
Definition: bitcoind.cpp:277
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:627
static void RunAll(const Args &args)
Definition: bench.cpp:105
BenchRunner(std::string name, BenchFunction func, PriorityLevel level)
Definition: bench.cpp:100
std::map< std::string, std::pair< BenchFunction, PriorityLevel > > BenchmarkMap
Definition: bench.h:70
static BenchmarkMap & benchmarks()
Definition: bench.cpp:94
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:33
std::string ListPriorities()
Definition: bench.cpp:79
std::function< void(Bench &)> BenchFunction
Definition: bench.h:43
PriorityLevel
Definition: bench.h:46
@ HIGH
Definition: bench.h:48
@ LOW
Definition: bench.h:47
uint8_t StringToPriority(const std::string &str)
Definition: bench.cpp:87
const char * name
Definition: rest.cpp:49
std::vector< std::string > setup_args
Definition: bench.h:64
std::string regex_filter
Definition: bench.h:62
fs::path output_json
Definition: bench.h:61
uint8_t priority
Definition: bench.h:63
fs::path output_csv
Definition: bench.h:60
std::vector< double > asymptote
Definition: bench.h:59
bool sanity_check
Definition: bench.h:57
std::chrono::milliseconds min_time
Definition: bench.h:58
bool is_list_only
Definition: bench.h:56