Bitcoin Core 31.99.0
P2P Digital Currency
bench.h
Go to the documentation of this file.
1// Copyright (c) 2015-present 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#include <util/time.h>
12
13#include <functional>
14#include <map>
15#include <string>
16#include <string_view>
17#include <vector>
18
19/*
20 * Usage:
21
22static 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
34BENCHMARK(NameOfYourBenchmarkFunction);
35
36 */
37
38namespace benchmark {
39
41
42using BenchFunction = std::function<void(Bench&)>;
43
44struct Args {
47 std::chrono::milliseconds min_time;
48 std::vector<double> asymptote;
49 fs::path output_csv;
50 fs::path output_json;
51 std::string regex_filter;
52 std::vector<std::string> setup_args;
53};
54
56{
57 // maps from "name" -> function
58 using BenchmarkMap = std::map<std::string, BenchFunction>;
59 static BenchmarkMap& benchmarks();
60
61public:
62 BenchRunner(std::string_view name, BenchFunction func);
63
64 static void RunAll(const Args& args);
65};
66} // namespace benchmark
67
68// BENCHMARK(foo); expands to: benchmark::BenchRunner bench_runner_foo{"foo", foo};
69#define BENCHMARK(n) \
70 benchmark::BenchRunner PASTE2(bench_runner_, n) { STRINGIZE(n), n }
71
72#endif // BITCOIN_BENCH_BENCH_H
ArgsManager & args
Definition: bitcoind.cpp:280
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:649
static void RunAll(const Args &args)
Definition: bench.cpp:75
BenchRunner(std::string_view name, BenchFunction func)
Definition: bench.cpp:70
std::map< std::string, BenchFunction > BenchmarkMap
Definition: bench.h:58
static BenchmarkMap & benchmarks()
Definition: bench.cpp:64
std::function< void(Bench &)> BenchFunction
Definition: bench.h:42
const char * name
Definition: rest.cpp:50
std::vector< std::string > setup_args
Definition: bench.h:52
std::string regex_filter
Definition: bench.h:51
fs::path output_json
Definition: bench.h:50
fs::path output_csv
Definition: bench.h:49
std::vector< double > asymptote
Definition: bench.h:48
bool sanity_check
Definition: bench.h:46
std::chrono::milliseconds min_time
Definition: bench.h:47
bool is_list_only
Definition: bench.h:45