cpp-stat-bench 0.24.0
Benchmark library with statistics for C++.
Loading...
Searching...
No Matches
invocation_context.h
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 */
20#pragma once
21
22#include <cstddef>
23#include <memory>
24#include <string>
25#include <utility>
26#include <vector>
27
34
35namespace stat_bench {
36
41public:
51 std::size_t samples, std::size_t warming_up_samples)
52 : cond_(std::move(cond)),
53 iterations_(iterations),
54 samples_(samples),
55 warming_up_samples_(warming_up_samples) {}
56
57 InvocationContext(const InvocationContext&) = delete;
59 auto operator=(const InvocationContext&) = delete;
60 auto operator=(InvocationContext&&) = delete;
61
65 ~InvocationContext() = default;
66
72 [[nodiscard]] auto threads() const noexcept -> std::size_t {
73 return cond_.threads();
74 }
75
81 [[nodiscard]] auto iterations() const noexcept -> std::size_t {
82 return iterations_;
83 }
84
90 [[nodiscard]] auto samples() const noexcept -> std::size_t {
91 return samples_;
92 }
93
101 template <typename T>
102 [[nodiscard]] auto get_param(const param::ParameterName& param_name) const
103 -> const T& {
104 return cond_.params().get<T>(param_name);
105 }
106
114 template <typename T>
115 [[nodiscard]] auto get_param(std::string param_name) const -> const T& {
116 return get_param<T>(param::ParameterName(std::move(param_name)));
117 }
118
126 [[nodiscard]] auto add_custom_stat(const CustomOutputName& name,
127 stat::CustomOutputAnalysisType analysis_type =
129 -> std::shared_ptr<stat::CustomStatOutput> {
130 auto out =
131 std::make_shared<stat::CustomStatOutput>(name, cond_.threads(),
132 samples_, warming_up_samples_, iterations_, analysis_type);
133 custom_stat_outputs_.push_back(out);
134 return out;
135 }
136
144 [[nodiscard]] auto add_custom_stat(std::string name,
145 stat::CustomOutputAnalysisType analysis_type =
147 -> std::shared_ptr<stat::CustomStatOutput> {
148 return add_custom_stat(
149 CustomOutputName(std::move(name)), analysis_type);
150 }
151
158 void add_custom_output(const CustomOutputName& name, double value) {
159 custom_outputs_.emplace_back(name, value);
160 }
161
168 void add_custom_output(std::string name, double value) {
169 add_custom_output(CustomOutputName(std::move(name)), value);
170 }
171
178 template <typename Func>
179 void measure(const Func& func) {
181 cond_.threads(), iterations_, samples_, warming_up_samples_)
182 .measure(func);
183 }
184
192 [[nodiscard]] auto durations() const noexcept
193 -> const std::vector<std::vector<clock::Duration>>& {
194 return durations_;
195 }
196
202 [[nodiscard]] auto custom_stat_outputs() const noexcept
203 -> const std::vector<std::shared_ptr<stat::CustomStatOutput>>& {
204 return custom_stat_outputs_;
205 }
206
212 [[nodiscard]] auto custom_outputs() const noexcept
213 -> const std::vector<std::pair<CustomOutputName, double>>& {
214 return custom_outputs_;
215 }
216
217private:
219 BenchmarkCondition cond_;
220
222 std::size_t iterations_;
223
225 std::size_t samples_;
226
228 std::size_t warming_up_samples_;
229
231 std::vector<std::vector<clock::Duration>> durations_{};
232
234 std::vector<std::shared_ptr<stat::CustomStatOutput>> custom_stat_outputs_{};
235
237 std::vector<std::pair<CustomOutputName, double>> custom_outputs_{};
238};
239
240} // namespace stat_bench
Definition of BenchmarkCondition class.
Class of conditions of benchmarks.
Class of names of custom outputs.
Class of context of invocations of benchmarks.
auto add_custom_stat(std::string name, stat::CustomOutputAnalysisType analysis_type=stat::CustomOutputAnalysisType::mean) -> std::shared_ptr< stat::CustomStatOutput >
Add a custom output with statistics.
auto custom_outputs() const noexcept -> const std::vector< std::pair< CustomOutputName, double > > &
Get the custom outputs without statistics.
auto durations() const noexcept -> const std::vector< std::vector< clock::Duration > > &
Get the measured durations.
auto samples() const noexcept -> std::size_t
Get the number of samples.
void measure(const Func &func)
Measure time.
auto get_param(std::string param_name) const -> const T &
Get a parameter.
auto add_custom_stat(const CustomOutputName &name, stat::CustomOutputAnalysisType analysis_type=stat::CustomOutputAnalysisType::mean) -> std::shared_ptr< stat::CustomStatOutput >
Add a custom output with statistics.
auto custom_stat_outputs() const noexcept -> const std::vector< std::shared_ptr< stat::CustomStatOutput > > &
Get the custom outputs with statistics.
void add_custom_output(std::string name, double value)
Add a custom output without statistics.
InvocationContext(BenchmarkCondition cond, std::size_t iterations, std::size_t samples, std::size_t warming_up_samples)
Constructor.
auto threads() const noexcept -> std::size_t
Get the number of threads.
~InvocationContext()=default
Destructor.
void add_custom_output(const CustomOutputName &name, double value)
Add a custom output without statistics.
auto iterations() const noexcept -> std::size_t
Get the number of iterations.
auto get_param(const param::ParameterName &param_name) const -> const T &
Get a parameter.
Class to invoke functions measuring durations in threads if needed.
auto measure(const Func &func) const -> std::vector< std::vector< clock::Duration > >
Measure time.
Class of names of parameters.
Definition of CustomOutputName class.
Definition of CustomStatOutput class.
Definition of Duration class.
Namespace of clocks for benchmarks.
Definition duration.h:23
Namespace of statistics.
Definition calc_stat.h:29
CustomOutputAnalysisType
Enumeration of types of analysis applied to custom outputs.
Namespace of stat_bench source codes.
STL namespace.
Definition of ParameterName class.
Definition of ThreadableInvoker class.