benchmark 1.10.0
Loading...
Searching...
No Matches
benchmark_api.h
1// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef BENCHMARK_BENCHMARK_API_H_
16#define BENCHMARK_BENCHMARK_API_H_
17
18#if defined(_MSC_VER)
19#pragma warning(push)
20#pragma warning(disable : 4251)
21#endif
22
23#include <functional>
24#include <memory>
25#include <string>
26#include <utility>
27#include <vector>
28
29#include "benchmark/counter.h"
30#include "benchmark/macros.h"
31#include "benchmark/state.h"
32#include "benchmark/statistics.h"
33#include "benchmark/types.h"
34
35namespace benchmark {
36
37const char kDefaultMinTimeStr[] = "0.5s";
38
39BENCHMARK_EXPORT void MaybeReenterWithoutASLR(int, char**);
40
41BENCHMARK_EXPORT std::string GetBenchmarkVersion();
42
43BENCHMARK_EXPORT void PrintDefaultHelp();
44
45BENCHMARK_EXPORT void Initialize(int* argc, char** argv,
46 void (*HelperPrintf)() = PrintDefaultHelp);
47BENCHMARK_EXPORT void Shutdown();
48
49BENCHMARK_EXPORT bool ReportUnrecognizedArguments(int argc, char** argv);
50
51BENCHMARK_EXPORT std::string GetBenchmarkFilter();
52
53BENCHMARK_EXPORT void SetBenchmarkFilter(std::string value);
54
55BENCHMARK_EXPORT int32_t GetBenchmarkVerbosity();
56
57BENCHMARK_EXPORT std::unique_ptr<BenchmarkReporter>
58CreateDefaultDisplayReporter();
59
60BENCHMARK_EXPORT size_t RunSpecifiedBenchmarks();
61BENCHMARK_EXPORT size_t RunSpecifiedBenchmarks(std::string spec);
62
63BENCHMARK_EXPORT size_t
64RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter);
65BENCHMARK_EXPORT size_t
66RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter, std::string spec);
67
68BENCHMARK_EXPORT size_t RunSpecifiedBenchmarks(
69 BenchmarkReporter* display_reporter, BenchmarkReporter* file_reporter);
70BENCHMARK_EXPORT size_t
71RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter,
72 BenchmarkReporter* file_reporter, std::string spec);
73
74BENCHMARK_EXPORT TimeUnit GetDefaultTimeUnit();
75
76BENCHMARK_EXPORT void SetDefaultTimeUnit(TimeUnit unit);
77
78BENCHMARK_EXPORT
79void AddCustomContext(std::string key, std::string value);
80
82 virtual ~ThreadRunnerBase() {}
83 virtual void RunThreads(const std::function<void(int)>& fn) = 0;
84};
85
86using threadrunner_factory =
87 std::function<std::unique_ptr<ThreadRunnerBase>(int)>;
88
89namespace internal {
90class BenchmarkFamilies;
91class BenchmarkInstance;
92} // namespace internal
93
94class BENCHMARK_EXPORT Benchmark {
95 public:
96 virtual ~Benchmark();
97
98 Benchmark* Name(const std::string& name);
99 Benchmark* Arg(int64_t x);
100 Benchmark* Unit(TimeUnit unit);
101 Benchmark* Range(int64_t start, int64_t limit);
102 Benchmark* DenseRange(int64_t start, int64_t limit, int step = 1);
103 Benchmark* Args(const std::vector<int64_t>& args);
104 Benchmark* ArgPair(int64_t x, int64_t y) {
105 std::vector<int64_t> args;
106 args.push_back(x);
107 args.push_back(y);
108 return Args(args);
109 }
110 Benchmark* Ranges(const std::vector<std::pair<int64_t, int64_t>>& ranges);
111 Benchmark* ArgsProduct(const std::vector<std::vector<int64_t>>& arglists);
112 Benchmark* ArgName(const std::string& name);
113 Benchmark* ArgNames(const std::vector<std::string>& names);
114 Benchmark* RangePair(int64_t lo1, int64_t hi1, int64_t lo2, int64_t hi2) {
115 std::vector<std::pair<int64_t, int64_t>> ranges;
116 ranges.push_back(std::make_pair(lo1, hi1));
117 ranges.push_back(std::make_pair(lo2, hi2));
118 return Ranges(ranges);
119 }
120 Benchmark* Setup(callback_function&&);
121 Benchmark* Setup(const callback_function&);
122 Benchmark* Teardown(callback_function&&);
123 Benchmark* Teardown(const callback_function&);
124 Benchmark* Apply(const std::function<void(Benchmark* benchmark)>&);
125 Benchmark* RangeMultiplier(int multiplier);
126 Benchmark* MinTime(double t);
127 Benchmark* MinWarmUpTime(double t);
128 Benchmark* Iterations(IterationCount n);
129 Benchmark* Repetitions(int n);
130 Benchmark* ReportAggregatesOnly(bool value = true);
131 Benchmark* DisplayAggregatesOnly(bool value = true);
132 Benchmark* MeasureProcessCPUTime();
133 Benchmark* UseRealTime();
134 Benchmark* UseManualTime();
135 Benchmark* Complexity(BigO complexity = benchmark::oAuto);
136 Benchmark* Complexity(BigOFunc* complexity);
137 Benchmark* ComputeStatistics(const std::string& name,
138 StatisticsFunc* statistics,
139 StatisticUnit unit = kTime);
140 Benchmark* Threads(int t);
141 Benchmark* ThreadRange(int min_threads, int max_threads, int mult = 2);
142 Benchmark* DenseThreadRange(int min_threads, int max_threads, int stride = 1);
143 Benchmark* ThreadPerCpu();
144 Benchmark* ThreadRunner(threadrunner_factory&& factory);
145
146 virtual void Run(State& state) = 0;
147
148 TimeUnit GetTimeUnit() const;
149
150 protected:
151 explicit Benchmark(const std::string& name);
152 void SetName(const std::string& name);
153
154 public:
155 const char* GetName() const;
156 int ArgsCnt() const;
157 const char* GetArgName(int arg) const;
158
159 private:
160 friend class internal::BenchmarkFamilies;
161 friend class internal::BenchmarkInstance;
162
163 std::string name_;
164 internal::AggregationReportMode aggregation_report_mode_;
165 std::vector<std::string> arg_names_;
166 std::vector<std::vector<int64_t>> args_;
167
168 TimeUnit time_unit_;
169 bool use_default_time_unit_;
170
171 int range_multiplier_;
172 double min_time_;
173 double min_warmup_time_;
174 IterationCount iterations_;
175 int repetitions_;
176 bool measure_process_cpu_time_;
177 bool use_real_time_;
178 bool use_manual_time_;
179 BigO complexity_;
180 BigOFunc* complexity_lambda_;
181 std::vector<internal::Statistics> statistics_;
182 std::vector<int> thread_counts_;
183
184 callback_function setup_;
185 callback_function teardown_;
186
187 threadrunner_factory threadrunner_;
188
189 BENCHMARK_DISALLOW_COPY_AND_ASSIGN(Benchmark);
190};
191
192namespace internal {
193typedef BENCHMARK_DEPRECATED_MSG(
194 "Use ::benchmark::Benchmark instead")::benchmark::Benchmark Benchmark;
195typedef BENCHMARK_DEPRECATED_MSG(
196 "Use ::benchmark::threadrunner_factory instead")::benchmark::
197 threadrunner_factory threadrunner_factory;
198
199typedef void(Function)(State&);
200
201BENCHMARK_EXPORT ::benchmark::Benchmark* RegisterBenchmarkInternal(
202 std::unique_ptr<::benchmark::Benchmark>);
203BENCHMARK_EXPORT std::map<std::string, std::string>*& GetGlobalContext();
204BENCHMARK_EXPORT void UseCharPointer(char const volatile*);
205BENCHMARK_EXPORT int InitializeStreams();
206BENCHMARK_UNUSED static int stream_init_anchor = InitializeStreams();
207} // namespace internal
208
209Benchmark* RegisterBenchmark(const std::string& name, internal::Function* fn);
210
211template <class Lambda>
212Benchmark* RegisterBenchmark(const std::string& name, Lambda&& fn);
213
214BENCHMARK_EXPORT void ClearRegisteredBenchmarks();
215
216namespace internal {
217class BENCHMARK_EXPORT FunctionBenchmark : public benchmark::Benchmark {
218 public:
219 FunctionBenchmark(const std::string& name, Function* func)
220 : Benchmark(name), func_(func) {}
221 void Run(State& st) override;
222
223 private:
224 Function* func_;
225};
226
227template <class Lambda>
228class LambdaBenchmark : public benchmark::Benchmark {
229 public:
230 void Run(State& st) override { lambda_(st); }
231 template <class OLambda>
232 LambdaBenchmark(const std::string& name, OLambda&& lam)
233 : Benchmark(name), lambda_(std::forward<OLambda>(lam)) {}
234
235 private:
236 LambdaBenchmark(LambdaBenchmark const&) = delete;
237 Lambda lambda_;
238};
239} // namespace internal
240
241inline Benchmark* RegisterBenchmark(const std::string& name,
242 internal::Function* fn) {
243 return internal::RegisterBenchmarkInternal(
244 ::benchmark::internal::make_unique<internal::FunctionBenchmark>(name,
245 fn));
246}
247
248template <class Lambda>
249Benchmark* RegisterBenchmark(const std::string& name, Lambda&& fn) {
250 using BenchType =
251 internal::LambdaBenchmark<typename std::decay<Lambda>::type>;
252 return internal::RegisterBenchmarkInternal(
253 ::benchmark::internal::make_unique<BenchType>(name,
254 std::forward<Lambda>(fn)));
255}
256
257template <class Lambda, class... Args>
258Benchmark* RegisterBenchmark(const std::string& name, Lambda&& fn,
259 Args&&... args) {
260 return benchmark::RegisterBenchmark(
261 name, [=](benchmark::State& st) { fn(st, args...); });
262}
263
264class Fixture : public Benchmark {
265 public:
266 Fixture() : Benchmark("") {}
267 void Run(State& st) override {
268 this->SetUp(st);
269 this->BenchmarkCase(st);
270 this->TearDown(st);
271 }
272 virtual void SetUp(const State&) {}
273 virtual void TearDown(const State&) {}
274 virtual void SetUp(State& st) { SetUp(const_cast<const State&>(st)); }
275 virtual void TearDown(State& st) { TearDown(const_cast<const State&>(st)); }
276
277 protected:
278 virtual void BenchmarkCase(State&) = 0;
279};
280
281BENCHMARK_EXPORT
282std::vector<int64_t> CreateRange(int64_t lo, int64_t hi, int multi);
283
284BENCHMARK_EXPORT
285std::vector<int64_t> CreateDenseRange(int64_t start, int64_t limit, int step);
286
287} // namespace benchmark
288
289#if defined(_MSC_VER)
290#pragma warning(pop)
291#endif
292
293#endif // BENCHMARK_BENCHMARK_API_H_
Definition reporter.h:56
Definition benchmark_api.h:94
Definition state.h:43
Definition benchmark_register.cc:78
Definition benchmark_api_internal.h:20
Definition benchmark_api.h:81