Bitcoin Core  0.20.99
P2P Digital Currency
bench.cpp
Go to the documentation of this file.
1 // Copyright (c) 2015-2020 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 <chainparams.h>
8 #include <test/util/setup_common.h>
9 #include <validation.h>
10 
11 #include <regex>
12 
13 const std::function<void(const std::string&)> G_TEST_LOG_FUN{};
14 
15 namespace {
16 
17 void GenerateTemplateResults(const std::vector<ankerl::nanobench::Result>& benchmarkResults, const std::string& filename, const char* tpl)
18 {
19  if (benchmarkResults.empty() || filename.empty()) {
20  // nothing to write, bail out
21  return;
22  }
23  std::ofstream fout(filename);
24  if (fout.is_open()) {
25  ankerl::nanobench::render(tpl, benchmarkResults, fout);
26  } else {
27  std::cout << "Could write to file '" << filename << "'" << std::endl;
28  }
29 
30  std::cout << "Created '" << filename << "'" << std::endl;
31 }
32 
33 } // namespace
34 
36 {
37  static std::map<std::string, BenchFunction> benchmarks_map;
38  return benchmarks_map;
39 }
40 
42 {
43  benchmarks().insert(std::make_pair(name, func));
44 }
45 
47 {
48  std::regex reFilter(args.regex_filter);
49  std::smatch baseMatch;
50 
51  std::vector<ankerl::nanobench::Result> benchmarkResults;
52  for (const auto& p : benchmarks()) {
53  if (!std::regex_match(p.first, baseMatch, reFilter)) {
54  continue;
55  }
56 
57  if (args.is_list_only) {
58  std::cout << p.first << std::endl;
59  continue;
60  }
61 
62  Bench bench;
63  bench.name(p.first);
64  if (args.asymptote.empty()) {
65  p.second(bench);
66  } else {
67  for (auto n : args.asymptote) {
68  bench.complexityN(n);
69  p.second(bench);
70  }
71  std::cout << bench.complexityBigO() << std::endl;
72  }
73 
74  if (!bench.results().empty()) {
75  benchmarkResults.push_back(bench.results().back());
76  }
77  }
78 
79  GenerateTemplateResults(benchmarkResults, args.output_csv, "# Benchmark, evals, iterations, total, min, max, median\n"
80  "{{#result}}{{name}}, {{epochs}}, {{average(iterations)}}, {{sumProduct(iterations, elapsed)}}, {{minimum(elapsed)}}, {{maximum(elapsed)}}, {{median(elapsed)}}\n"
81  "{{/result}}");
82  GenerateTemplateResults(benchmarkResults, args.output_json, ankerl::nanobench::templates::json());
83 }
char const * json() noexcept
Template to generate JSON data.
std::string output_json
Definition: bench.h:45
BenchRunner(std::string name, BenchFunction func)
Definition: bench.cpp:41
std::vector< double > asymptote
Definition: bench.h:43
const std::function< void(const std::string &)> G_TEST_LOG_FUN
Definition: bench.cpp:13
void render(char const *mustacheTemplate, Bench const &bench, std::ostream &out)
Renders output from a mustache-like template and benchmark results.
std::string output_csv
Definition: bench.h:44
fs::ofstream ofstream
Definition: fs.h:91
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
const char * name
Definition: rest.cpp:41
Bench & complexityN(T b) noexcept
Definition: nanobench.h:1165
std::function< void(Bench &)> BenchFunction
Definition: bench.h:38
std::map< std::string, BenchFunction > BenchmarkMap
Definition: bench.h:50
static void RunAll(const Args &args)
Definition: bench.cpp:46
bool is_list_only
Definition: bench.h:42
static BenchmarkMap & benchmarks()
Definition: bench.cpp:35
Main entry point to nanobench&#39;s benchmarking facility.
Definition: nanobench.h:583
std::string regex_filter
Definition: bench.h:41