CppBenchmark  1.0.4.0
C++ Benchmark Library
cppbenchmark.h
Go to the documentation of this file.
1 
9 #ifndef CPPBENCHMARK_H
10 #define CPPBENCHMARK_H
11 
12 #include "benchmark/executor.h"
15 #include "benchmark/reporter_csv.h"
17 
18 namespace CppBenchmark {
19 
21 namespace Internals {
22 
23 class BenchmarkRegistrator
24 {
25 public:
26  explicit BenchmarkRegistrator(const std::function<std::shared_ptr<BenchmarkBase>()>& builder)
28 };
29 
30 } // namespace Internals
32 
33 } // namespace CppBenchmark
34 
36 #define BENCHMARK_INTERNAL_UNIQUE_NAME_LINE2(name, line) name##line
37 #define BENCHMARK_INTERNAL_UNIQUE_NAME_LINE(name, line) BENCHMARK_INTERNAL_UNIQUE_NAME_LINE2(name, line)
38 #define BENCHMARK_INTERNAL_UNIQUE_NAME(name) BENCHMARK_INTERNAL_UNIQUE_NAME_LINE(name, __LINE__)
40 
42 
46 #define BENCHMARK_MAIN()\
47 int main(int argc, char** argv)\
48 {\
49  CppBenchmark::LauncherConsole::GetInstance().Initialize(argc, argv);\
50  CppBenchmark::LauncherConsole::GetInstance().Execute();\
51  CppBenchmark::LauncherConsole::GetInstance().Report();\
52  return 0;\
53 }
54 
56 
68 #define BENCHMARK(...)\
69 namespace CppBenchmark {\
70  class BENCHMARK_INTERNAL_UNIQUE_NAME(__benchmark__) : public Benchmark\
71  {\
72  public:\
73  using Benchmark::Benchmark;\
74  protected:\
75  void Run(Context& context) override;\
76  };\
77  Internals::BenchmarkRegistrator BENCHMARK_INTERNAL_UNIQUE_NAME(benchmark_registrator)([]() { return std::make_shared<BENCHMARK_INTERNAL_UNIQUE_NAME(__benchmark__)>(__VA_ARGS__); });\
78 }\
79 void CppBenchmark::BENCHMARK_INTERNAL_UNIQUE_NAME(__benchmark__)::Run(CppBenchmark::Context& context)
80 
82 
128 #define BENCHMARK_FIXTURE(fixture, ...)\
129 namespace CppBenchmark {\
130  class BENCHMARK_INTERNAL_UNIQUE_NAME(__benchmark__) : public Benchmark, public fixture\
131  {\
132  public:\
133  using Benchmark::Benchmark;\
134  protected:\
135  void Run(Context& context) override;\
136  };\
137  Internals::BenchmarkRegistrator BENCHMARK_INTERNAL_UNIQUE_NAME(benchmark_registrator)([]() { return std::make_shared<BENCHMARK_INTERNAL_UNIQUE_NAME(__benchmark__)>(__VA_ARGS__); });\
138 }\
139 void CppBenchmark::BENCHMARK_INTERNAL_UNIQUE_NAME(__benchmark__)::Run(Context& context)
140 
142 
156 #define BENCHMARK_THREADS(...)\
157 namespace CppBenchmark {\
158  class BENCHMARK_INTERNAL_UNIQUE_NAME(__benchmark__) : public BenchmarkThreads\
159  {\
160  public:\
161  using BenchmarkThreads::BenchmarkThreads;\
162  protected:\
163  void RunThread(ContextThreads& context) override;\
164  };\
165  Internals::BenchmarkRegistrator BENCHMARK_INTERNAL_UNIQUE_NAME(benchmark_registrator)([]() { return std::make_shared<BENCHMARK_INTERNAL_UNIQUE_NAME(__benchmark__)>(__VA_ARGS__); });\
166 }\
167 void CppBenchmark::BENCHMARK_INTERNAL_UNIQUE_NAME(__benchmark__)::RunThread(CppBenchmark::ContextThreads& context)
168 
170 
224 #define BENCHMARK_THREADS_FIXTURE(fixture, ...)\
225 namespace CppBenchmark {\
226  class BENCHMARK_INTERNAL_UNIQUE_NAME(__benchmark__) : public BenchmarkThreads, public fixture\
227  {\
228  public:\
229  using BenchmarkThreads::BenchmarkThreads;\
230  protected:\
231  void RunThread(ContextThreads& context) override;\
232  };\
233  Internals::BenchmarkRegistrator BENCHMARK_INTERNAL_UNIQUE_NAME(benchmark_registrator)([]() { return std::make_shared<BENCHMARK_INTERNAL_UNIQUE_NAME(__benchmark__)>(__VA_ARGS__); });\
234 }\
235 void CppBenchmark::BENCHMARK_INTERNAL_UNIQUE_NAME(__benchmark__)::RunThread(ContextThreads& context)
236 
238 
258 #define BENCHMARK_CLASS(type, ...)\
259 namespace CppBenchmark { Internals::BenchmarkRegistrator BENCHMARK_INTERNAL_UNIQUE_NAME(benchmark_registrator)([]() { return std::make_shared<type>(__VA_ARGS__); }); }
260 
262 
277 #define BENCHCODE_SCOPE(name) CppBenchmark::Executor::ScopeBenchmark(name);
278 
280 
294 #define BENCHCODE_START(name) CppBenchmark::Executor::StartBenchmark(name);
295 
297 
311 #define BENCHCODE_STOP(name) CppBenchmark::Executor::StopBenchmark(name);
312 
314 
331 #define BENCHCODE_REPORT()\
332 {\
333  CppBenchmark::ReporterConsole BENCHMARK_INTERNAL_UNIQUE_NAME(reporter);\
334  CppBenchmark::Executor::Report(BENCHMARK_INTERNAL_UNIQUE_NAME(reporter));\
335 }
336 
338 
349 #define BENCHCODE_REPORT_STR(value)\
350 {\
351  std::ostringstream BENCHMARK_INTERNAL_UNIQUE_NAME(output);\
352  CppBenchmark::ReporterConsole BENCHMARK_INTERNAL_UNIQUE_NAME(reporter)(BENCHMARK_INTERNAL_UNIQUE_NAME(output));\
353  CppBenchmark::Executor::Report(BENCHMARK_INTERNAL_UNIQUE_NAME(reporter));\
354  value = BENCHMARK_INTERNAL_UNIQUE_NAME(output).str();\
355 }
356 
358 
369 #define BENCHCODE_REPORT_CSV(value)\
370 {\
371  std::ostringstream BENCHMARK_INTERNAL_UNIQUE_NAME(output);\
372  CppBenchmark::ReporterCSV BENCHMARK_INTERNAL_UNIQUE_NAME(reporter)(BENCHMARK_INTERNAL_UNIQUE_NAME(output));\
373  CppBenchmark::Executor::Report(BENCHMARK_INTERNAL_UNIQUE_NAME(reporter));\
374  value = BENCHMARK_INTERNAL_UNIQUE_NAME(output).str();\
375 }
376 
378 
389 #define BENCHCODE_REPORT_JSON(value)\
390 {\
391  std::ostringstream BENCHMARK_INTERNAL_UNIQUE_NAME(output);\
392  CppBenchmark::ReporterJSON BENCHMARK_INTERNAL_UNIQUE_NAME(reporter)(BENCHMARK_INTERNAL_UNIQUE_NAME(output));\
393  CppBenchmark::Executor::Report(BENCHMARK_INTERNAL_UNIQUE_NAME(reporter));\
394  value = BENCHMARK_INTERNAL_UNIQUE_NAME(output).str();\
395 }
396 
397 #endif // CPPBENCHMARK_H
static LauncherConsole & GetInstance()
Get singleton instance.
void AddBenchmarkBuilder(const std::function< std::shared_ptr< BenchmarkBase >()> &builder)
Add the given benchmark builder function to the benchmark builders collection.
Definition: launcher.h:47
Dynamic benchmarks executor definition.
Console launcher definition.
C++ Benchmark project definitions.
Definition: barrier.h:15
Console reporter definition.
Comma-separated values (CSV) reporter definition.
JSON reporter definition.