cpp-stat-bench 0.24.0
Benchmark library with statistics for C++.
Loading...
Searching...
No Matches
fixture_base.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
34
35namespace stat_bench {
36
40class FixtureBase : public bench_impl::IBenchmarkCase {
41public:
46
55 virtual void setup(InvocationContext& context) {
56 // no operation in default implementation
57 (void)context;
58 }
59
69 virtual void tear_down(InvocationContext& context) {
70 // no operation in default implementation
71 (void)context;
72 }
73
75
80
88 template <typename T>
89 [[nodiscard]] auto add_param(const param::ParameterName& param_name)
90 -> std::shared_ptr<param::ParameterValueVector<T>> {
91 return params_.add<T>(param_name);
92 }
93
101 template <typename T>
102 [[nodiscard]] auto add_param(std::string param_name)
103 -> std::shared_ptr<param::ParameterValueVector<T>> {
104 return add_param<T>(param::ParameterName(std::move(param_name)));
105 }
106
113 -> std::shared_ptr<param::ParameterValueVector<std::size_t>> {
115 }
116
118
125 virtual void run() = 0;
126
127 FixtureBase(const FixtureBase&) = delete;
128 FixtureBase(FixtureBase&&) = delete;
129 auto operator=(const FixtureBase&) -> FixtureBase& = delete;
130 auto operator=(FixtureBase&&) -> FixtureBase& = delete;
131
135 ~FixtureBase() override = default;
136
137protected:
141 FixtureBase() = default;
142
143private:
145 void execute() final {
146 auto& context = current_invocation_context();
147 setup(context);
148 try {
149 run();
150 tear_down(context);
151 } catch (...) {
152 tear_down(context);
153 throw;
154 }
155 }
156
158 [[nodiscard]] auto params() const noexcept
159 -> const param::ParameterConfig& final {
160 return params_;
161 }
162
164 param::ParameterConfig params_{};
165};
166
167} // namespace stat_bench
virtual void run()=0
Run actual process in each case.
virtual void tear_down(InvocationContext &context)
Tear down after running actual process.
FixtureBase()=default
Constructor.
~FixtureBase() override=default
Destructor.
auto add_param(const param::ParameterName &param_name) -> std::shared_ptr< param::ParameterValueVector< T > >
Add a parameter.
virtual void setup(InvocationContext &context)
Setup before running actual process.
auto add_param(std::string param_name) -> std::shared_ptr< param::ParameterValueVector< T > >
Add a parameter.
auto add_threads_param() -> std::shared_ptr< param::ParameterValueVector< std::size_t > >
Add a parameter of the number of threads.
Class of context of invocations of benchmarks.
Interface of cases in benchmarks.
Class of names of parameters.
Definition of current_invocation_context function.
Definition of IBenchmarkCase class.
Definition of InvocationContext class.
Namespace of parameters of benchmarks.
auto num_threads_parameter_name() -> const ParameterName &
Get the parameter name for the number of threads.
Namespace of stat_bench source codes.
auto current_invocation_context() -> InvocationContext &
Get the current invocation context.
Definition of num_threads_parameter_name function.
Definition of ParameterConfig class.
Definition of ParameterName class.
Definition of ParameterValueVector class.