cpp-stat-bench 0.24.0
Benchmark library with statistics for C++.
Loading...
Searching...
No Matches
console_reporter.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2021 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
21
22#include <cstdint>
23#include <cstdio>
24#include <exception>
25#include <memory>
26#include <string>
27#include <utility>
28#include <vector>
29
30#include <fmt/base.h>
31#include <fmt/color.h>
32#include <fmt/format.h>
33
45#include "stat_bench/version.h"
46
47namespace stat_bench {
48namespace reporter {
49
50namespace {
51
52void print_line(std::FILE* file, char c) {
53 constexpr std::size_t line_length = 120;
54 fmt::print(file, "{}\n", std::string(line_length, c));
55}
56
57} // namespace
58
59ConsoleReporter::ConsoleReporter(std::FILE* file) : file_(file) {}
60
62 const clock::SystemTimePoint& time_stamp) {
63 fmt::print(file_, FMT_STRING("cpp-stat-bench {}.{}.{}\n\n"),
66
67 fmt::print(file_, FMT_STRING("Benchmark start at {}\n\n"), time_stamp);
68
69 fmt::print(file_, FMT_STRING("Time resolution: {:.3e} sec.\n\n"),
70 clock::MonotoneTimePoint::resolution().seconds());
71
72 (void)std::fflush(file_);
73}
74
76 const clock::SystemTimePoint& time_stamp) {
77 fmt::print(file_, FMT_STRING("Benchmark finished at {}\n"), time_stamp);
78 (void)std::fflush(file_);
79}
80
82 print_line(file_, '=');
83 fmt::print(file_, FMT_STRING("{}\n"), name);
84 print_line(file_, '=');
85 fmt::print(file_, "\n");
86 (void)std::fflush(file_);
87}
88
90 // no operation
91 (void)name;
92}
93
94namespace {
95
96auto format_duration(double val) -> std::string {
97 constexpr double sec_to_us = 1e+6;
98 constexpr double tol = 1e-4;
99 if (val < tol) {
100 return fmt::format(FMT_STRING("{:.4f}"), val * sec_to_us);
101 }
102 return fmt::format(FMT_STRING("{}"),
103 fmt::group_digits(static_cast<std::uint64_t>(val * sec_to_us)));
104}
105
106} // namespace
107
109#define CONSOLE_TABLE_FORMAT "{:<58} {:>10} {:>7} {:>9} {:>9} {:>9} "
110
112#define CONSOLE_TABLE_FORMAT_ERROR "{:<58} {}"
113
115 const bench_impl::BenchmarkGroupConfig& /*config*/) {
116 fmt::print(file_, FMT_STRING(">> {}\n"), name);
117 fmt::print(file_, FMT_STRING(CONSOLE_TABLE_FORMAT "{}\n"), "", "", "",
118 "Time [us]", "", "", "");
119 fmt::print(file_, FMT_STRING(CONSOLE_TABLE_FORMAT "{}\n"), "", "Iterations",
120 "Samples", "Mean", "Std. Err.", "Max", "Custom Outputs (mean)");
121 print_line(file_, '-');
122 (void)std::fflush(file_);
123}
124
126 fmt::print(file_, "\n");
127 (void)std::fflush(file_);
128}
129
131 // no operation
132}
133
135 // no operation
136}
137
138namespace {
139
140auto format_case_name_with_params(const BenchmarkFullName& case_info,
141 const BenchmarkCondition& cond) -> std::string {
142 if (cond.params().empty()) {
143 return case_info.case_name().str().str();
144 }
145 return fmt::format(
146 FMT_STRING("{} ({}) "), case_info.case_name(), cond.params());
147}
148
149} // namespace
150
152 const measurer::Measurement& measurement) {
153 fmt::print(file_, FMT_STRING(CONSOLE_TABLE_FORMAT),
154 format_case_name_with_params(
155 measurement.case_info(), measurement.cond()),
156 measurement.iterations(), measurement.samples(),
157 format_duration(measurement.durations_stat().mean()),
158 format_duration(measurement.durations_stat().standard_error()),
159 format_duration(measurement.durations_stat().max()));
160 for (std::size_t i = 0; i < measurement.custom_stat_outputs().size(); ++i) {
161 fmt::print(file_, FMT_STRING("{}={:.3e}, "),
162 measurement.custom_stat_outputs().at(i)->name(),
163 measurement.custom_stat().at(i).mean());
164 }
165 for (const auto& out : measurement.custom_outputs()) {
166 fmt::print(file_, FMT_STRING("{}={:.3e}, "), out.first, out.second);
167 }
168 fmt::print(file_, "\n");
169 (void)std::fflush(file_);
170}
171
173 const BenchmarkCondition& cond, const std::exception_ptr& error) {
174 try {
175 std::rethrow_exception(error);
176 } catch (const std::exception& e) {
177 fmt::print(file_, fmt::fg(fmt::color::red),
178 FMT_STRING(CONSOLE_TABLE_FORMAT_ERROR),
179 format_case_name_with_params(case_info, cond), e.what());
180 }
181 fmt::print(file_, "\n");
182 (void)std::fflush(file_);
183}
184
185} // namespace reporter
186} // namespace stat_bench
Definition of BenchmarkCaseName class.
Definition of BenchmarkCondition class.
Definition of BenchmarkFullName class.
Class of conditions of benchmarks.
Class of information of cases in benchmarks.
Class of names of groups of benchmarks.
Class of configuration of a group of cases in benchmarks.
Class of time points of system clocks.
Class of data of measurements.
Definition measurement.h:42
auto custom_stat() const noexcept -> const std::vector< stat::Statistics > &
Get the statistics of custom outputs.
auto iterations() const noexcept -> const std::size_t &
Get the number of iterations.
auto samples() const noexcept -> const std::size_t &
Get the number of samples.
auto cond() const noexcept -> const BenchmarkCondition &
Get the condition.
Definition measurement.h:92
auto custom_stat_outputs() const noexcept -> const std::vector< std::shared_ptr< stat::CustomStatOutput > > &
Get the custom outputs with statistics.
auto case_info() const noexcept -> const BenchmarkFullName &
Get the information of the case.
Definition measurement.h:83
auto durations_stat() const noexcept -> const stat::Statistics &
Get the statistics of durations.
auto custom_outputs() const noexcept -> const std::vector< std::pair< CustomOutputName, double > > &
Get the custom outputs without statistics.
Class of names of measurers.
void group_starts(const BenchmarkGroupName &name, const bench_impl::BenchmarkGroupConfig &config) override
Start a group of benchmarks.
void measurement_succeeded(const measurer::Measurement &measurement) override
Successfully finished a measurement.
void experiment_starts(const clock::SystemTimePoint &time_stamp) override
Start an experiment.
void experiment_finished(const clock::SystemTimePoint &time_stamp) override
Finished an experiment.
void case_starts(const BenchmarkFullName &case_info) override
Start a case of a benchmark.
void measurer_starts(const measurer::MeasurerName &name) override
Start benchmarks using a measurer.
void measurement_failed(const BenchmarkFullName &case_info, const BenchmarkCondition &cond, const std::exception_ptr &error) override
Failed a measurement.
ConsoleReporter(std::FILE *file=stdout)
Constructor.
void measurer_finished(const measurer::MeasurerName &name) override
Finished benchmarks using a measurer.
void group_finished(const BenchmarkGroupName &name) override
Finished a group of benchmarks.
void case_finished(const BenchmarkFullName &case_info) override
Finished a case of a benchmark.
#define CONSOLE_TABLE_FORMAT_ERROR
Format of the table for console output when an error occurs.
#define CONSOLE_TABLE_FORMAT
Format of the table for console output.
Definition of ConsoleReporter class.
Definition of CustomOutputName class.
Definition of CustomStatOutput class.
Definition of Duration class.
Definition of Measurement class.
Definition of classes of monotone clocks.
Namespace of reporters of results of benchmarks.
Namespace of stat_bench source codes.
Definition of ParameterDict class.
Definition of Statistics class.
Definition of Utf8String class.
Definition of macros of versions.
#define STAT_BENCH_VERSION_PATCH
Patch version of cpp-stat-bench project.
Definition version.h:29
#define STAT_BENCH_VERSION_MINOR
Minor version of cpp-stat-bench project.
Definition version.h:26
#define STAT_BENCH_VERSION_MAJOR
Major version of cpp-stat-bench project.
Definition version.h:23