forked from libnonius/nonius
/
reporter.h++
178 lines (148 loc) · 5.73 KB
/
reporter.h++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Nonius - C++ benchmarking tool
//
// Written in 2014- by the nonius contributors <nonius@rmf.io>
//
// To the extent possible under law, the author(s) have dedicated all copyright and related
// and neighboring rights to this software to the public domain worldwide. This software is
// distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>
// Reporter interface
#ifndef NONIUS_REPORTER_HPP
#define NONIUS_REPORTER_HPP
#include <nonius/clock.h++>
#include <nonius/outlier_classification.h++>
#include <nonius/environment.h++>
#include <nonius/execution_plan.h++>
#include <nonius/sample_analysis.h++>
#include <nonius/detail/noexcept.h++>
#include <nonius/detail/unique_name.h++>
#include <vector>
#include <string>
#include <ios>
#include <ostream>
#include <fstream>
#include <iostream>
#include <memory>
#include <unordered_map>
#include <exception>
namespace nonius {
struct bad_stream : virtual std::exception {
char const* what() const NONIUS_NOEXCEPT override {
return "failed to open file";
}
};
struct reporter {
public:
virtual ~reporter() = default;
void configure(configuration& cfg) {
if(cfg.output_file.empty()) {
os = [&]() -> std::ostream& { return std::cout; };
} else {
auto ofs = std::make_shared<std::ofstream>(cfg.output_file);
os = [ofs]() -> std::ostream& { return *ofs; };
}
report_stream().exceptions(std::ios::failbit);
if(!report_stream()) throw bad_stream();
do_configure(cfg);
}
void warmup_start() {
do_warmup_start();
}
void warmup_end(int iterations) {
do_warmup_end(iterations);
}
void estimate_clock_resolution_start() {
do_estimate_clock_resolution_start();
}
void estimate_clock_resolution_complete(environment_estimate<fp_seconds> estimate) {
do_estimate_clock_resolution_complete(estimate);
}
void estimate_clock_cost_start() {
do_estimate_clock_cost_start();
}
void estimate_clock_cost_complete(environment_estimate<fp_seconds> estimate) {
do_estimate_clock_cost_complete(estimate);
}
void suite_start() {
do_suite_start();
}
void params_start(parameters const& params) {
do_params_start(params);
}
void benchmark_start(std::string const& name) {
do_benchmark_start(name);
}
void measurement_start(execution_plan<fp_seconds> plan) {
do_measurement_start(plan);
}
void measurement_complete(std::vector<fp_seconds> const& samples) {
do_measurement_complete(samples);
}
void analysis_start() {
do_analysis_start();
}
void analysis_complete(sample_analysis<fp_seconds> const& analysis) {
do_analysis_complete(analysis);
}
void benchmark_failure(std::exception_ptr error) {
do_benchmark_failure(error);
}
void benchmark_complete() {
do_benchmark_complete();
}
void params_complete() {
do_params_complete();
}
void suite_complete() {
do_suite_complete();
}
virtual std::string description() = 0;
private:
virtual void do_configure(configuration& /*cfg*/) {}
virtual void do_warmup_start() {}
virtual void do_warmup_end(int /*iterations*/) {}
virtual void do_estimate_clock_resolution_start() {}
virtual void do_estimate_clock_resolution_complete(environment_estimate<fp_seconds> /*estimate*/) {}
virtual void do_estimate_clock_cost_start() {}
virtual void do_estimate_clock_cost_complete(environment_estimate<fp_seconds> /*estimate*/) {}
virtual void do_suite_start() {}
virtual void do_params_start(parameters const& /*params*/) {}
virtual void do_benchmark_start(std::string const& /*name*/) {}
virtual void do_measurement_start(execution_plan<fp_seconds> /*plan*/) {}
virtual void do_measurement_complete(std::vector<fp_seconds> const& /*samples*/) {}
virtual void do_analysis_start() {} // TODO make generic?
virtual void do_analysis_complete(sample_analysis<fp_seconds> const& /*analysis*/) {}
virtual void do_benchmark_failure(std::exception_ptr /*error*/) {}
virtual void do_benchmark_complete() {}
virtual void do_params_complete() {}
virtual void do_suite_complete() {}
protected:
std::ostream& progress_stream() {
return std::cout;
}
std::ostream& error_stream() {
return std::cerr;
}
std::ostream& report_stream() {
return os();
}
private:
std::function<std::ostream&()> os;
};
using reporter_registry = std::unordered_map<std::string, std::unique_ptr<reporter>>;
inline reporter_registry& global_reporter_registry() {
static reporter_registry registry;
return registry;
}
struct reporter_registrar {
reporter_registrar(reporter_registry& registry, std::string name, reporter* registrant) {
registry.emplace(std::move(name), std::unique_ptr<reporter>(registrant));
}
};
} // namespace nonius
#define NONIUS_REPORTER(name, ...) \
namespace { static ::nonius::reporter_registrar NONIUS_DETAIL_UNIQUE_NAME(reporter_registrar) (::nonius::global_reporter_registry(), name, new __VA_ARGS__()); } \
static_assert(true, "")
#endif // NONIUS_REPORTER_HPP