Bitcoin ABC  0.26.2
P2P Digital Currency
bench.cpp
Go to the documentation of this file.
1 // Copyright (c) 2015-2019 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 <fs.h>
9 #include <validation.h>
10 
11 #include <test/util/setup_common.h>
12 
13 #include <regex>
14 
15 namespace {
16 
17 void GenerateTemplateResults(
18  const std::vector<ankerl::nanobench::Result> &benchmarkResults,
19  const std::string &filename, const char *tpl) {
20  if (benchmarkResults.empty() || filename.empty()) {
21  // nothing to write, bail out
22  return;
23  }
24  fsbridge::ofstream fout{fs::PathFromString(filename)};
25  if (fout.is_open()) {
26  ankerl::nanobench::render(tpl, benchmarkResults, fout);
27  } else {
28  std::cout << "Could write to file '" << filename << "'" << std::endl;
29  }
30 
31  std::cout << "Created '" << filename << "'" << std::endl;
32 }
33 
34 } // namespace
35 
37  static std::map<std::string, BenchFunction> benchmarks_map;
38  return benchmarks_map;
39 }
40 
43  benchmarks().insert(std::make_pair(name, func));
44 }
45 
47  std::regex reFilter(args.regex_filter);
48  std::smatch baseMatch;
49 
50  std::vector<ankerl::nanobench::Result> benchmarkResults;
51 
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  benchmarkResults.push_back(bench.results().back());
74  }
75 
76  GenerateTemplateResults(
77  benchmarkResults, args.output_csv,
78  "# Benchmark, evals, iterations, total, min, max, median\n"
79  "{{#result}}{{name}}, {{epochs}}, {{average(iterations)}}, "
80  "{{sumProduct(iterations, elapsed)}}, {{minimum(elapsed)}}, "
81  "{{maximum(elapsed)}}, {{median(elapsed)}}\n"
82  "{{/result}}");
83  GenerateTemplateResults(benchmarkResults, args.output_json,
85 }
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:583
Bench & complexityN(T b) noexcept
Definition: nanobench.h:1165
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
static BenchmarkMap & benchmarks()
Definition: bench.cpp:36
static void RunAll(const Args &args)
Definition: bench.cpp:46
std::map< std::string, BenchFunction > BenchmarkMap
Definition: bench.h:50
BenchRunner(std::string name, BenchFunction func)
Definition: bench.cpp:41
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:39
static path PathFromString(const std::string &string)
Convert byte string to path object.
Definition: fs.h:147
fs::ofstream ofstream
Definition: fs.h:247
const char * name
Definition: rest.cpp:44
std::string output_csv
Definition: bench.h:45
std::string regex_filter
Definition: bench.h:42
std::vector< double > asymptote
Definition: bench.h:44
std::string output_json
Definition: bench.h:46
bool is_list_only
Definition: bench.h:43