Bitcoin Core  22.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  std::vector<ankerl::nanobench::Result> benchmarkResults;
61  for (const auto& p : benchmarks()) {
62  if (!std::regex_match(p.first, baseMatch, reFilter)) {
63  continue;
64  }
65 
66  if (args.is_list_only) {
67  std::cout << p.first << std::endl;
68  continue;
69  }
70 
71  Bench bench;
72  bench.name(p.first);
73  if (args.min_time > 0ms) {
74  // convert to nanos before dividing to reduce rounding errors
75  std::chrono::nanoseconds min_time_ns = args.min_time;
76  bench.minEpochTime(min_time_ns / bench.epochs());
77  }
78 
79  if (args.asymptote.empty()) {
80  p.second(bench);
81  } else {
82  for (auto n : args.asymptote) {
83  bench.complexityN(n);
84  p.second(bench);
85  }
86  std::cout << bench.complexityBigO() << std::endl;
87  }
88 
89  if (!bench.results().empty()) {
90  benchmarkResults.push_back(bench.results().back());
91  }
92  }
93 
94  GenerateTemplateResults(benchmarkResults, args.output_csv, "# Benchmark, evals, iterations, total, min, max, median\n"
95  "{{#result}}{{name}}, {{epochs}}, {{average(iterations)}}, {{sumProduct(iterations, elapsed)}}, {{minimum(elapsed)}}, {{maximum(elapsed)}}, {{median(elapsed)}}\n"
96  "{{/result}}");
97  GenerateTemplateResults(benchmarkResults, args.output_json, ankerl::nanobench::templates::json());
98 }
ankerl::nanobench::render
void render(char const *mustacheTemplate, Bench const &bench, std::ostream &out)
Renders output from a mustache-like template and benchmark results.
fs.h
setup_common.h
ankerl::nanobench::Bench::name
ANKERL_NANOBENCH(NODISCARD) std Bench & name(char const *benchmarkName)
Name of the benchmark, will be shown in the table row.
benchmark::BenchRunner::RunAll
static void RunAll(const Args &args)
Definition: bench.cpp:55
ankerl::nanobench::Bench
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:616
benchmark::BenchFunction
std::function< void(Bench &)> BenchFunction
Definition: bench.h:42
ankerl::nanobench::Bench::complexityBigO
std::vector< BigO > complexityBigO() const
fs::path
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:29
benchmark::Args
Definition: bench.h:44
benchmark::BenchRunner::benchmarks
static BenchmarkMap & benchmarks()
Definition: bench.cpp:44
bench.h
name
const char * name
Definition: rest.cpp:52
ankerl::nanobench::Bench::minEpochTime
ANKERL_NANOBENCH(NODISCARD) std Bench & minEpochTime(std::chrono::nanoseconds t) noexcept
Minimum time each epoch should take.
ankerl::nanobench::Bench::complexityN
Bench & complexityN(T b) noexcept
Definition: nanobench.h:1214
benchmark::BenchRunner::BenchRunner
BenchRunner(std::string name, BenchFunction func)
Definition: bench.cpp:50
ankerl::nanobench::templates::json
char const * json() noexcept
Template to generate JSON data.
benchmark::BenchRunner::BenchmarkMap
std::map< std::string, BenchFunction > BenchmarkMap
Definition: bench.h:55
ankerl::nanobench::Bench::epochs
Bench & epochs(size_t numEpochs) noexcept
Controls number of epochs, the number of measurements to perform.
G_TEST_COMMAND_LINE_ARGUMENTS
const std::function< std::vector< const char * >)> G_TEST_COMMAND_LINE_ARGUMENTS
Retrieve the command line arguments.
Definition: bench.cpp:23
G_TEST_LOG_FUN
const std::function< void(const std::string &)> G_TEST_LOG_FUN
This is connected to the logger.
Definition: bench.cpp:21
args
ArgsManager args
Definition: notifications.cpp:36