CppBenchmark 1.0.5.0
C++ Benchmark Library
Loading...
Searching...
No Matches
Namespaces | Macros
cppbenchmark.h File Reference

CppBenchmark definitions. More...

#include "benchmark/executor.h"
#include "benchmark/launcher_console.h"
#include "benchmark/reporter_console.h"
#include "benchmark/reporter_csv.h"
#include "benchmark/reporter_json.h"

Go to the source code of this file.

Namespaces

namespace  CppBenchmark
 C++ Benchmark project definitions.
 

Macros

#define BENCHMARK_MAIN()
 Benchmark main entry point macro.
 
#define BENCHMARK(...)
 Benchmark register macro.
 
#define BENCHMARK_FIXTURE(fixture, ...)
 Benchmark with fixture register macro.
 
#define BENCHMARK_THREADS(...)
 Benchmark threads register macro.
 
#define BENCHMARK_THREADS_FIXTURE(fixture, ...)
 Benchmark threads with fixture register macro.
 
#define BENCHMARK_CLASS(type, ...)   namespace CppBenchmark { Internals::BenchmarkRegistrator BENCHMARK_INTERNAL_UNIQUE_NAME(benchmark_registrator)([]() { return std::make_shared<type>(__VA_ARGS__); }); }
 Benchmark class register macro.
 
#define BENCHCODE_SCOPE(name)   CppBenchmark::Executor::ScopeBenchmark(name);
 Dynamic benchmark scope register macro.
 
#define BENCHCODE_START(name)   CppBenchmark::Executor::StartBenchmark(name);
 Dynamic benchmark start macro.
 
#define BENCHCODE_STOP(name)   CppBenchmark::Executor::StopBenchmark(name);
 Dynamic benchmark stop macro.
 
#define BENCHCODE_REPORT()
 Dynamic benchmarks report to console macro.
 
#define BENCHCODE_REPORT_STR(value)
 Dynamic benchmarks report as a string macro.
 
#define BENCHCODE_REPORT_CSV(value)
 Dynamic benchmarks report as a CSV format string macro.
 
#define BENCHCODE_REPORT_JSON(value)
 Dynamic benchmarks report as a JSON format string macro.
 

Detailed Description

CppBenchmark definitions.

Author
Ivan Shynkarenka
Date
16.07.2015

Definition in file cppbenchmark.h.

Macro Definition Documentation

◆ BENCHCODE_REPORT

#define BENCHCODE_REPORT ( )
Value:
{\
CppBenchmark::ReporterConsole BENCHMARK_INTERNAL_UNIQUE_NAME(reporter);\
CppBenchmark::Executor::Report(BENCHMARK_INTERNAL_UNIQUE_NAME(reporter));\
}

Dynamic benchmarks report to console macro.

Prepare results of all dynamic benchmarks registered in static Executor class and show them using ReporterConsole.

Example:

int main(int argc, char** argv)
{
// Do some calculations or program run and measure code fragments with dynamic benchmarks...
...
// Report benchmark results
return 0;
}
#define BENCHCODE_REPORT()
Dynamic benchmarks report to console macro.
Examples
executor.cpp.

Definition at line 331 of file cppbenchmark.h.

◆ BENCHCODE_REPORT_CSV

#define BENCHCODE_REPORT_CSV (   value)
Value:
{\
std::ostringstream BENCHMARK_INTERNAL_UNIQUE_NAME(output);\
CppBenchmark::ReporterCSV BENCHMARK_INTERNAL_UNIQUE_NAME(reporter)(BENCHMARK_INTERNAL_UNIQUE_NAME(output));\
CppBenchmark::Executor::Report(BENCHMARK_INTERNAL_UNIQUE_NAME(reporter));\
value = BENCHMARK_INTERNAL_UNIQUE_NAME(output).str();\
}
Comma-separated values (CSV) reporter.

Dynamic benchmarks report as a CSV format string macro.

Prepare results of all dynamic benchmarks registered in static Executor class using ReporterCSV and store them in a string variable value.

Example:

// Report benchmark results in CSV format
std::string report;
#define BENCHCODE_REPORT_CSV(value)
Dynamic benchmarks report as a CSV format string macro.

Definition at line 369 of file cppbenchmark.h.

◆ BENCHCODE_REPORT_JSON

#define BENCHCODE_REPORT_JSON (   value)
Value:
{\
std::ostringstream BENCHMARK_INTERNAL_UNIQUE_NAME(output);\
CppBenchmark::ReporterJSON BENCHMARK_INTERNAL_UNIQUE_NAME(reporter)(BENCHMARK_INTERNAL_UNIQUE_NAME(output));\
CppBenchmark::Executor::Report(BENCHMARK_INTERNAL_UNIQUE_NAME(reporter));\
value = BENCHMARK_INTERNAL_UNIQUE_NAME(output).str();\
}

Dynamic benchmarks report as a JSON format string macro.

Prepare results of all dynamic benchmarks registered in static Executor class using ReporterJSON and store them in a string variable value.

Example:

// Report benchmark results in JSON format
std::string report;
#define BENCHCODE_REPORT_JSON(value)
Dynamic benchmarks report as a JSON format string macro.

Definition at line 389 of file cppbenchmark.h.

◆ BENCHCODE_REPORT_STR

#define BENCHCODE_REPORT_STR (   value)
Value:
{\
std::ostringstream BENCHMARK_INTERNAL_UNIQUE_NAME(output);\
CppBenchmark::ReporterConsole BENCHMARK_INTERNAL_UNIQUE_NAME(reporter)(BENCHMARK_INTERNAL_UNIQUE_NAME(output));\
CppBenchmark::Executor::Report(BENCHMARK_INTERNAL_UNIQUE_NAME(reporter));\
value = BENCHMARK_INTERNAL_UNIQUE_NAME(output).str();\
}

Dynamic benchmarks report as a string macro.

Prepare results of all dynamic benchmarks registered in static Executor class using ReporterConsole and store them in a string variable value.

Example:

// Report benchmark results
std::string report;
#define BENCHCODE_REPORT_STR(value)
Dynamic benchmarks report as a string macro.

Definition at line 349 of file cppbenchmark.h.

◆ BENCHCODE_SCOPE

#define BENCHCODE_SCOPE (   name)    CppBenchmark::Executor::ScopeBenchmark(name);

Dynamic benchmark scope register macro.

Create a scope guard for dynamic benchmark with the given name. It will be automatically registered in static Executor class.

Example:

// Some scope...
{
auto benchmark = BENCHCODE_SCOPE("My dynamic benchmark");
// Here goes some code fragment you want to benchmark dynamically...
...
}
#define BENCHCODE_SCOPE(name)
Dynamic benchmark scope register macro.
Examples
executor.cpp.

Definition at line 277 of file cppbenchmark.h.

◆ BENCHCODE_START

#define BENCHCODE_START (   name)    CppBenchmark::Executor::StartBenchmark(name);

Dynamic benchmark start macro.

Start dynamic benchmark with the given name. It will be automatically registered in static Executor class. Should be used in pair with BENCHCODE_STOP macro!

Example:

BENCHCODE_START("My dynamic benchmark");
// Here goes some code fragment you want to benchmark dynamically...
...
BENCHCODE_STOP("My dynamic benchmark");
#define BENCHCODE_START(name)
Dynamic benchmark start macro.
Examples
executor.cpp.

Definition at line 294 of file cppbenchmark.h.

◆ BENCHCODE_STOP

#define BENCHCODE_STOP (   name)    CppBenchmark::Executor::StopBenchmark(name);

Dynamic benchmark stop macro.

Stop dynamic benchmark with the given name. It will be automatically registered in static Executor class. Should be used in pair with BENCHCODE_START macro!

Example:

BENCHCODE_START("My dynamic benchmark");
// Here goes some code fragment you want to benchmark dynamically...
...
BENCHCODE_STOP("My dynamic benchmark");
Examples
executor.cpp.

Definition at line 311 of file cppbenchmark.h.

◆ BENCHMARK

#define BENCHMARK (   ...)
Value:
namespace CppBenchmark {\
class BENCHMARK_INTERNAL_UNIQUE_NAME(__benchmark__) : public Benchmark\
{\
public:\
using Benchmark::Benchmark;\
protected:\
void Run(Context& context) override;\
};\
Internals::BenchmarkRegistrator BENCHMARK_INTERNAL_UNIQUE_NAME(benchmark_registrator)([]() { return std::make_shared<BENCHMARK_INTERNAL_UNIQUE_NAME(__benchmark__)>(__VA_ARGS__); });\
}\
void CppBenchmark::BENCHMARK_INTERNAL_UNIQUE_NAME(__benchmark__)::Run(CppBenchmark::Context& context)
C++ Benchmark project definitions.
Definition barrier.h:15

Benchmark register macro.

Register a new benchmark with a given name and settings. Next to the definition you should provide a benchmark code.

Example:

// This benchmark will call MyTest() function in 1000000 operations
BENCHMARK("MyTestBenchmark", 1000000)
{
MyTest();
}
#define BENCHMARK(...)
Benchmark register macro.
Examples
fpu.cpp, and random.cpp.

Definition at line 68 of file cppbenchmark.h.

◆ BENCHMARK_CLASS

#define BENCHMARK_CLASS (   type,
  ... 
)    namespace CppBenchmark { Internals::BenchmarkRegistrator BENCHMARK_INTERNAL_UNIQUE_NAME(benchmark_registrator)([]() { return std::make_shared<type>(__VA_ARGS__); }); }

Benchmark class register macro.

Register a new benchmark based on a child class of a type with a given name and settings. You should inherit type from Benchmark, BenchmarkThreads or BenchmarkPC and implement all necessary benchmark methods.

Example:

class VectorBenchmark
{
protected:
std::vector<int> container;
void Initialize(CppBenchmark::Context& context) { container.reserve(1000); }
void Run(CppBenchmark::Context& context) override { container.push_back(rand()); }
void Cleanup(CppBenchmark::Context& context) override { container.clear(); }
};
// This benchmark will measure VectorBenchmark with 1000000 operations
BENCHMARK_CLASS(VectorBenchmark, "VectorPushBackBenchmark", 1000000)
Benchmark running context.
Definition context.h:27
#define BENCHMARK_CLASS(type,...)
Benchmark class register macro.
Examples
mpmc.cpp, mpsc.cpp, sort.cpp, and spsc.cpp.

Definition at line 258 of file cppbenchmark.h.

◆ BENCHMARK_FIXTURE

#define BENCHMARK_FIXTURE (   fixture,
  ... 
)
Value:
namespace CppBenchmark {\
class BENCHMARK_INTERNAL_UNIQUE_NAME(__benchmark__) : public Benchmark, public fixture\
{\
public:\
using Benchmark::Benchmark;\
protected:\
void Run(Context& context) override;\
};\
Internals::BenchmarkRegistrator BENCHMARK_INTERNAL_UNIQUE_NAME(benchmark_registrator)([]() { return std::make_shared<BENCHMARK_INTERNAL_UNIQUE_NAME(__benchmark__)>(__VA_ARGS__); });\
}\
void CppBenchmark::BENCHMARK_INTERNAL_UNIQUE_NAME(__benchmark__)::Run(Context& context)

Benchmark with fixture register macro.

Register a new benchmark with a given fixture, name and settings. Next to the definition you should provide a benchmark code. In benchmark code you can access to public and protected fields and methods of the fixture.

Benchmark static fixture is a user class that will be constructed before benchmarking and will be destructed after.

Static fixture example:

// Vector fixture will be created only once and its field will be visible in benchmark body!
class VectorFixture
{
protected:
std::vector<int> container;
};
// This benchmark will insert random value into std::vector<int> 1000000 times
BENCHMARK_FIXTURE(VectorFixture, "VectorPushBackBenchmark", 1000000)
{
container.push_back(rand());
}
#define BENCHMARK_FIXTURE(fixture,...)
Benchmark with fixture register macro.

Benchmark dynamic fixture is a user class inherited from Fixture base class and overrides Initialize()/Cleanup() methods that will be called before/after each benchmark attempt.

Dynamic fixture example:

// Vector fixture will be created only once and its filed will be visible in benchmark body!
class VectorFixture : public virtual CppBenchmark::Fixture
{
protected:
std::vector<int> container;
// Initialize method will called just before each benchmark attempt
void Initialize(CppBenchmark::Context& context) override { container = std::vector<int>(); }
// Cleanup method will called just after each benchmark attempt
void Cleanup(CppBenchmark::Context& context) override { container.clear(); }
};
// This benchmark will insert random value into std::vector<int> 1000000 times
BENCHMARK_FIXTURE(VectorFixture, "VectorPushBackBenchmark", 1000000)
{
container.push_back(rand());
}
Benchmark fixture.
Definition fixture.h:21
Examples
atomic.cpp, containers.cpp, fwrite.cpp, and iterators.cpp.

Definition at line 128 of file cppbenchmark.h.

◆ BENCHMARK_MAIN

#define BENCHMARK_MAIN ( )
Value:
int main(int argc, char** argv)\
{\
CppBenchmark::LauncherConsole::GetInstance().Initialize(argc, argv);\
CppBenchmark::LauncherConsole::GetInstance().Execute();\
CppBenchmark::LauncherConsole::GetInstance().Report();\
return 0;\
}

Benchmark main entry point macro.

Main entry point definition for all benchmarks. Place this macro in some .cpp file to provide a main() function with registered LauncherConsole with argc & argv arguments parsing.

Examples
atomic.cpp, containers.cpp, fpu.cpp, fwrite.cpp, iterators.cpp, mpmc.cpp, mpsc.cpp, random.cpp, sort.cpp, spsc.cpp, and threads.cpp.

Definition at line 46 of file cppbenchmark.h.

◆ BENCHMARK_THREADS

#define BENCHMARK_THREADS (   ...)
Value:
namespace CppBenchmark {\
class BENCHMARK_INTERNAL_UNIQUE_NAME(__benchmark__) : public BenchmarkThreads\
{\
public:\
using BenchmarkThreads::BenchmarkThreads;\
protected:\
void RunThread(ContextThreads& context) override;\
};\
Internals::BenchmarkRegistrator BENCHMARK_INTERNAL_UNIQUE_NAME(benchmark_registrator)([]() { return std::make_shared<BENCHMARK_INTERNAL_UNIQUE_NAME(__benchmark__)>(__VA_ARGS__); });\
}\
void CppBenchmark::BENCHMARK_INTERNAL_UNIQUE_NAME(__benchmark__)::RunThread(CppBenchmark::ContextThreads& context)

Benchmark threads register macro.

Register a new threads benchmark with a given name and settings. Next to the definition you should provide a benchmark code that will be executed in multi-thread environment. You can use settings parameter to give threads count you want to measure with.

Example:

// This benchmark will output random value in std::cout 1000000 times in 4 concurrent threads environment
BENCHMARK_THREADS("ThreadsConsoleBenchmark", 4, 1000000)
{
std::count << rand();
}
#define BENCHMARK_THREADS(...)
Benchmark threads register macro.

Definition at line 156 of file cppbenchmark.h.

◆ BENCHMARK_THREADS_FIXTURE

#define BENCHMARK_THREADS_FIXTURE (   fixture,
  ... 
)
Value:
namespace CppBenchmark {\
class BENCHMARK_INTERNAL_UNIQUE_NAME(__benchmark__) : public BenchmarkThreads, public fixture\
{\
public:\
using BenchmarkThreads::BenchmarkThreads;\
protected:\
void RunThread(ContextThreads& context) override;\
};\
Internals::BenchmarkRegistrator BENCHMARK_INTERNAL_UNIQUE_NAME(benchmark_registrator)([]() { return std::make_shared<BENCHMARK_INTERNAL_UNIQUE_NAME(__benchmark__)>(__VA_ARGS__); });\
}\
void CppBenchmark::BENCHMARK_INTERNAL_UNIQUE_NAME(__benchmark__)::RunThread(ContextThreads& context)

Benchmark threads with fixture register macro.

Register a new threads benchmark with a given fixture, name and settings. Next to the definition you should provide a benchmark code that will be executed in multi-thread environment. In benchmark code you can access to public and protected fields & methods of the fixture. You can use settings parameter to give threads count you want to measure with.

Benchmark static threads fixture is a user class that will be constructed before benchmarking and destructed after.

Static threads fixture example:

// Atomic threads fixture will be created only once and its field will be visible in benchmark body!
class AtomicPreset
{
protected:
std::atomic<int> counter;
};
// This benchmark will increment atomic counter 1000000 times in 4 concurrent threads environment
BENCHMARK_THREADS_PRESET(AtomicPreset, "ThreadsAtomicIncrementBenchmark", 4, 1000000)
{
counter++;
}

Benchmark dynamic threads fixture is a user class inherited from Fixture or FixtureThreads base class and overrides Initialize()/Cleanup()/InitializeThread()/CleanupThread() methods that will be called before/after each benchmark attempt and before/after each thread is started/finished.

Dynamic threads fixture example:

// Atomic threads fixture will be created only once and its field will be visible in benchmark body!
class AtomicFixture : public virtual CppBenchmark::FixtureThreads
{
protected:
std::atomic<int> counter;
// Initialize method will called just before each benchmark attempt
void Initialize(CppBenchmark::ContextThreads& context) override { counter = 123; }
// Cleanup method will called just after each benchmark attempt
void Cleanup(CppBenchmark::ContextThreads& context) override { counter = 987; }
// Initialize thread method will called just before each benchmark thread is started
void InitializeThread(CppBenchmark::ContextThreads& context) override { counter = context.threads(); }
// Cleanup thread method will called just after each benchmark thread is finished
void CleanupThread(CppBenchmark::ContextThreads& context) override { counter = 0; }
};
// This benchmark will increment atomic counter 1000000 times in 4 concurrent threads environment
BENCHMARK_THREADS_FIXTURE(AtomicFixture, "ThreadsAtomicIncrementBenchmark", 4, 1000000)
{
counter++;
}
Benchmark thread running context.
int threads() const noexcept
Benchmark threads count.
Threads benchmark fixture.
virtual void Cleanup(ContextThreads &context)
Cleanup benchmark.
virtual void Initialize(ContextThreads &context)
Initialize benchmark.
virtual void InitializeThread(ContextThreads &context)
Initialize thread.
virtual void CleanupThread(ContextThreads &context)
Cleanup thread.
#define BENCHMARK_THREADS_FIXTURE(fixture,...)
Benchmark threads with fixture register macro.
Examples
threads.cpp.

Definition at line 224 of file cppbenchmark.h.