Bitcoin ABC 0.30.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 <util/fs.h>
8
9#include <test/util/setup_common.h>
10
11#include <chrono>
12#include <fstream>
13#include <iostream>
14#include <map>
15#include <regex>
16#include <string>
17#include <vector>
18
19using namespace std::chrono_literals;
20
21namespace {
22
23void GenerateTemplateResults(
24 const std::vector<ankerl::nanobench::Result> &benchmarkResults,
25 const fs::path &file, const char *tpl) {
26 if (benchmarkResults.empty() || file.empty()) {
27 // nothing to write, bail out
28 return;
29 }
30 std::ofstream fout{file};
31 if (fout.is_open()) {
32 ankerl::nanobench::render(tpl, benchmarkResults, fout);
33 std::cout << "Created " << file << std::endl;
34 } else {
35 std::cout << "Could not write to file " << file << std::endl;
36 }
37}
38
39} // namespace
40
42 static std::map<std::string, BenchFunction> benchmarks_map;
43 return benchmarks_map;
44}
45
48 benchmarks().insert(std::make_pair(name, func));
49}
50
52 std::regex reFilter(args.regex_filter);
53 std::smatch baseMatch;
54
55 std::vector<ankerl::nanobench::Result> benchmarkResults;
56
57 for (const auto &p : benchmarks()) {
58 if (!std::regex_match(p.first, baseMatch, reFilter)) {
59 continue;
60 }
61
62 if (args.is_list_only) {
63 std::cout << p.first << std::endl;
64 continue;
65 }
66
67 Bench bench;
68 bench.name(p.first);
69 if (args.min_time > 0ms) {
70 // convert to nanos before dividing to reduce rounding errors
71 std::chrono::nanoseconds min_time_ns = args.min_time;
72 bench.minEpochTime(min_time_ns / bench.epochs());
73 }
74
75 if (args.asymptote.empty()) {
76 p.second(bench);
77 } else {
78 for (auto n : args.asymptote) {
79 bench.complexityN(n);
80 p.second(bench);
81 }
82 std::cout << bench.complexityBigO() << std::endl;
83 }
84 benchmarkResults.push_back(bench.results().back());
85 }
86
87 GenerateTemplateResults(
88 benchmarkResults, args.output_csv,
89 "# Benchmark, evals, iterations, total, min, max, median\n"
90 "{{#result}}{{name}}, {{epochs}}, {{average(iterations)}}, "
91 "{{sumProduct(iterations, elapsed)}}, {{minimum(elapsed)}}, "
92 "{{maximum(elapsed)}}, {{median(elapsed)}}\n"
93 "{{/result}}");
94 GenerateTemplateResults(benchmarkResults, args.output_json,
96}
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:616
Bench & complexityN(T b) noexcept
Definition: nanobench.h:1214
Bench & epochs(size_t numEpochs) noexcept
Controls number of epochs, the number of measurements to perform.
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
ANKERL_NANOBENCH(NODISCARD) std Bench & minEpochTime(std::chrono::nanoseconds t) noexcept
Minimum time each epoch should take.
static BenchmarkMap & benchmarks()
Definition: bench.cpp:41
static void RunAll(const Args &args)
Definition: bench.cpp:51
std::map< std::string, BenchFunction > BenchmarkMap
Definition: bench.h:54
BenchRunner(std::string name, BenchFunction func)
Definition: bench.cpp:46
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:30
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
const char * name
Definition: rest.cpp:47
std::string regex_filter
Definition: bench.h:50
fs::path output_json
Definition: bench.h:49
fs::path output_csv
Definition: bench.h:48
std::vector< double > asymptote
Definition: bench.h:47
std::chrono::milliseconds min_time
Definition: bench.h:46
bool is_list_only
Definition: bench.h:45