Bitcoin Core  24.99.0
P2P Digital Currency
bench.cpp
Go to the documentation of this file.
1 // Copyright (c) 2015-2021 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 #include <bench/bench.h>
6 
7 #include <fs.h>
9 
10 #include <chrono>
11 #include <fstream>
12 #include <functional>
13 #include <iostream>
14 #include <map>
15 #include <regex>
16 #include <string>
17 #include <vector>
18 
19 using namespace std::chrono_literals;
20 
21 const std::function<void(const std::string&)> G_TEST_LOG_FUN{};
22 
23 const std::function<std::vector<const char*>()> G_TEST_COMMAND_LINE_ARGUMENTS{};
24 
25 namespace {
26 
27 void GenerateTemplateResults(const std::vector<ankerl::nanobench::Result>& benchmarkResults, const fs::path& file, const char* tpl)
28 {
29  if (benchmarkResults.empty() || file.empty()) {
30  // nothing to write, bail out
31  return;
32  }
33  std::ofstream fout{file};
34  if (fout.is_open()) {
35  ankerl::nanobench::render(tpl, benchmarkResults, fout);
36  std::cout << "Created " << file << std::endl;
37  } else {
38  std::cout << "Could not write to file " << file << std::endl;
39  }
40 }
41 
42 } // namespace
43 
45 {
46  static std::map<std::string, BenchFunction> benchmarks_map;
47  return benchmarks_map;
48 }
49 
51 {
52  benchmarks().insert(std::make_pair(name, func));
53 }
54 
56 {
57  std::regex reFilter(args.regex_filter);
58  std::smatch baseMatch;
59 
60  if (args.sanity_check) {
61  std::cout << "Running with --sanity-check option, benchmark results will be useless." << std::endl;
62  }
63 
64  std::vector<ankerl::nanobench::Result> benchmarkResults;
65  for (const auto& p : benchmarks()) {
66  if (!std::regex_match(p.first, baseMatch, reFilter)) {
67  continue;
68  }
69 
70  if (args.is_list_only) {
71  std::cout << p.first << std::endl;
72  continue;
73  }
74 
75  Bench bench;
76  if (args.sanity_check) {
77  bench.epochs(1).epochIterations(1);
78  }
79  bench.name(p.first);
80  if (args.min_time > 0ms) {
81  // convert to nanos before dividing to reduce rounding errors
82  std::chrono::nanoseconds min_time_ns = args.min_time;
83  bench.minEpochTime(min_time_ns / bench.epochs());
84  }
85 
86  if (args.asymptote.empty()) {
87  p.second(bench);
88  } else {
89  for (auto n : args.asymptote) {
90  bench.complexityN(n);
91  p.second(bench);
92  }
93  std::cout << bench.complexityBigO() << std::endl;
94  }
95 
96  if (!bench.results().empty()) {
97  benchmarkResults.push_back(bench.results().back());
98  }
99  }
100 
101  GenerateTemplateResults(benchmarkResults, args.output_csv, "# Benchmark, evals, iterations, total, min, max, median\n"
102  "{{#result}}{{name}}, {{epochs}}, {{average(iterations)}}, {{sumProduct(iterations, elapsed)}}, {{minimum(elapsed)}}, {{maximum(elapsed)}}, {{median(elapsed)}}\n"
103  "{{/result}}");
104  GenerateTemplateResults(benchmarkResults, args.output_json, ankerl::nanobench::templates::json());
105 }
const std::function< void(const std::string &)> G_TEST_LOG_FUN
This is connected to the logger.
Definition: bench.cpp:21
const std::function< std::vector< const char * >)> G_TEST_COMMAND_LINE_ARGUMENTS
Retrieve the command line arguments.
Definition: bench.cpp:23
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:616
Bench & complexityN(T b) noexcept
Definition: nanobench.h:1214
ANKERL_NANOBENCH(NODISCARD) std Bench & minEpochTime(std::chrono::nanoseconds t) noexcept
Minimum time each epoch should take.
Bench & epochIterations(uint64_t numIters) noexcept
Sets exactly the number of iterations for each epoch.
ANKERL_NANOBENCH(NODISCARD) std Bench & name(char const *benchmarkName)
Name of the benchmark, will be shown in the table row.
std::vector< BigO > complexityBigO() const
Bench & epochs(size_t numEpochs) noexcept
Controls number of epochs, the number of measurements to perform.
static BenchmarkMap & benchmarks()
Definition: bench.cpp:44
static void RunAll(const Args &args)
Definition: bench.cpp:55
std::map< std::string, BenchFunction > BenchmarkMap
Definition: bench.h:56
BenchRunner(std::string name, BenchFunction func)
Definition: bench.cpp:50
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:31
char const * json() noexcept
Template to generate JSON data.
void render(char const *mustacheTemplate, Bench const &bench, std::ostream &out)
Renders output from a mustache-like template and benchmark results.
std::function< void(Bench &)> BenchFunction
Definition: bench.h:42
ArgsManager args
const char * name
Definition: rest.cpp:46