Bitcoin ABC  0.24.7
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 <validation.h>
9 
10 #include <test/util/setup_common.h>
11 
12 #include <regex>
13 
14 namespace {
15 
16 void GenerateTemplateResults(
17  const std::vector<ankerl::nanobench::Result> &benchmarkResults,
18  const std::string &filename, const char *tpl) {
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  static std::map<std::string, BenchFunction> benchmarks_map;
37  return benchmarks_map;
38 }
39 
42  benchmarks().insert(std::make_pair(name, func));
43 }
44 
46  std::regex reFilter(args.regex_filter);
47  std::smatch baseMatch;
48 
49  std::vector<ankerl::nanobench::Result> benchmarkResults;
50 
51  for (const auto &p : benchmarks()) {
52  if (!std::regex_match(p.first, baseMatch, reFilter)) {
53  continue;
54  }
55 
56  if (args.is_list_only) {
57  std::cout << p.first << std::endl;
58  continue;
59  }
60 
61  Bench bench;
62  bench.name(p.first);
63  if (args.asymptote.empty()) {
64  p.second(bench);
65  } else {
66  for (auto n : args.asymptote) {
67  bench.complexityN(n);
68  p.second(bench);
69  }
70  std::cout << bench.complexityBigO() << std::endl;
71  }
72  benchmarkResults.push_back(bench.results().back());
73  }
74 
75  GenerateTemplateResults(
76  benchmarkResults, args.output_csv,
77  "# Benchmark, evals, iterations, total, min, max, median\n"
78  "{{#result}}{{name}}, {{epochs}}, {{average(iterations)}}, "
79  "{{sumProduct(iterations, elapsed)}}, {{minimum(elapsed)}}, "
80  "{{maximum(elapsed)}}, {{median(elapsed)}}\n"
81  "{{/result}}");
82  GenerateTemplateResults(benchmarkResults, args.output_json,
84 }
ankerl::nanobench::render
void render(char const *mustacheTemplate, Bench const &bench, std::ostream &out)
Renders output from a mustache-like template and benchmark results.
benchmark::Args::output_json
std::string output_json
Definition: bench.h:46
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:45
ankerl::nanobench::Bench
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:583
benchmark::Args::regex_filter
std::string regex_filter
Definition: bench.h:42
chainparams.h
fsbridge::ofstream
fs::ofstream ofstream
Definition: fs.h:99
ankerl::nanobench::Bench::complexityBigO
std::vector< BigO > complexityBigO() const
benchmark::Args
Definition: bench.h:41
benchmark::BenchRunner::benchmarks
static BenchmarkMap & benchmarks()
Definition: bench.cpp:35
bench.h
name
const char * name
Definition: rest.cpp:43
ankerl::nanobench::Bench::complexityN
Bench & complexityN(T b) noexcept
Definition: nanobench.h:1165
benchmark::BenchRunner::BenchRunner
BenchRunner(std::string name, BenchFunction func)
Definition: bench.cpp:40
ankerl::nanobench::templates::json
char const * json() noexcept
Template to generate JSON data.
benchmark::Args::asymptote
std::vector< double > asymptote
Definition: bench.h:44
benchmark::BenchFunction
std::function< void(Bench &)> BenchFunction
Definition: bench.h:39
benchmark::BenchRunner::BenchmarkMap
std::map< std::string, BenchFunction > BenchmarkMap
Definition: bench.h:50
benchmark::Args::output_csv
std::string output_csv
Definition: bench.h:45
benchmark::Args::is_list_only
bool is_list_only
Definition: bench.h:43