15#ifndef BENCHMARK_BENCHMARK_API_H_
16#define BENCHMARK_BENCHMARK_API_H_
20#pragma warning(disable : 4251)
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"
37const char kDefaultMinTimeStr[] =
"0.5s";
39BENCHMARK_EXPORT
void MaybeReenterWithoutASLR(
int,
char**);
41BENCHMARK_EXPORT std::string GetBenchmarkVersion();
43BENCHMARK_EXPORT
void PrintDefaultHelp();
45BENCHMARK_EXPORT
void Initialize(
int* argc,
char** argv,
46 void (*HelperPrintf)() = PrintDefaultHelp);
47BENCHMARK_EXPORT
void Shutdown();
49BENCHMARK_EXPORT
bool ReportUnrecognizedArguments(
int argc,
char** argv);
51BENCHMARK_EXPORT std::string GetBenchmarkFilter();
53BENCHMARK_EXPORT
void SetBenchmarkFilter(std::string value);
55BENCHMARK_EXPORT int32_t GetBenchmarkVerbosity();
57BENCHMARK_EXPORT std::unique_ptr<BenchmarkReporter>
58CreateDefaultDisplayReporter();
60BENCHMARK_EXPORT
size_t RunSpecifiedBenchmarks();
61BENCHMARK_EXPORT
size_t RunSpecifiedBenchmarks(std::string spec);
63BENCHMARK_EXPORT
size_t
65BENCHMARK_EXPORT
size_t
68BENCHMARK_EXPORT
size_t RunSpecifiedBenchmarks(
70BENCHMARK_EXPORT
size_t
74BENCHMARK_EXPORT TimeUnit GetDefaultTimeUnit();
76BENCHMARK_EXPORT
void SetDefaultTimeUnit(TimeUnit unit);
79void AddCustomContext(std::string key, std::string value);
83 virtual void RunThreads(
const std::function<
void(
int)>& fn) = 0;
86using threadrunner_factory =
87 std::function<std::unique_ptr<ThreadRunnerBase>(
int)>;
90class BenchmarkFamilies;
91class BenchmarkInstance;
94class BENCHMARK_EXPORT Benchmark {
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;
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);
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);
146 virtual void Run(
State& state) = 0;
148 TimeUnit GetTimeUnit()
const;
151 explicit Benchmark(
const std::string& name);
152 void SetName(
const std::string& name);
155 const char* GetName()
const;
157 const char* GetArgName(
int arg)
const;
164 internal::AggregationReportMode aggregation_report_mode_;
165 std::vector<std::string> arg_names_;
166 std::vector<std::vector<int64_t>> args_;
169 bool use_default_time_unit_;
171 int range_multiplier_;
173 double min_warmup_time_;
174 IterationCount iterations_;
176 bool measure_process_cpu_time_;
178 bool use_manual_time_;
180 BigOFunc* complexity_lambda_;
181 std::vector<internal::Statistics> statistics_;
182 std::vector<int> thread_counts_;
184 callback_function setup_;
185 callback_function teardown_;
187 threadrunner_factory threadrunner_;
189 BENCHMARK_DISALLOW_COPY_AND_ASSIGN(Benchmark);
193typedef BENCHMARK_DEPRECATED_MSG(
195typedef BENCHMARK_DEPRECATED_MSG(
196 "Use ::benchmark::threadrunner_factory instead")::benchmark::
197 threadrunner_factory threadrunner_factory;
199typedef void(Function)(
State&);
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();
209Benchmark* RegisterBenchmark(
const std::string& name, internal::Function* fn);
211template <
class Lambda>
212Benchmark* RegisterBenchmark(
const std::string& name, Lambda&& fn);
214BENCHMARK_EXPORT
void ClearRegisteredBenchmarks();
219 FunctionBenchmark(
const std::string& name, Function* func)
220 : Benchmark(name), func_(func) {}
221 void Run(
State& st)
override;
227template <
class Lambda>
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)) {}
236 LambdaBenchmark(LambdaBenchmark
const&) =
delete;
241inline Benchmark* RegisterBenchmark(
const std::string& name,
242 internal::Function* fn) {
243 return internal::RegisterBenchmarkInternal(
244 ::benchmark::internal::make_unique<internal::FunctionBenchmark>(name,
248template <
class Lambda>
249Benchmark* RegisterBenchmark(
const std::string& name, Lambda&& fn) {
251 internal::LambdaBenchmark<typename std::decay<Lambda>::type>;
252 return internal::RegisterBenchmarkInternal(
253 ::benchmark::internal::make_unique<BenchType>(name,
254 std::forward<Lambda>(fn)));
257template <
class Lambda,
class... Args>
258Benchmark* RegisterBenchmark(
const std::string& name, Lambda&& fn,
260 return benchmark::RegisterBenchmark(
261 name, [=](benchmark::State& st) { fn(st, args...); });
264class Fixture :
public Benchmark {
266 Fixture() : Benchmark(
"") {}
267 void Run(
State& st)
override {
269 this->BenchmarkCase(st);
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)); }
278 virtual void BenchmarkCase(
State&) = 0;
282std::vector<int64_t> CreateRange(int64_t lo, int64_t hi,
int multi);
285std::vector<int64_t> CreateDenseRange(int64_t start, int64_t limit,
int step);
Definition benchmark_api.h:94
Definition benchmark_register.cc:78
Definition benchmark_api_internal.h:20
Definition benchmark_api.h:81