diff --git a/examples/google_benchmark/fixture_bench.hpp b/examples/google_benchmark/fixture_bench.hpp new file mode 100644 index 0000000..738ba31 --- /dev/null +++ b/examples/google_benchmark/fixture_bench.hpp @@ -0,0 +1,65 @@ +// See docs: +// https://github.com/google/benchmark/blob/main/docs/user_guide.md#fixtures + +#ifndef FIXTURE_BENCH_HPP +#define FIXTURE_BENCH_HPP + + +#include + +namespace example_namespace { + +class MyFixture : public benchmark::Fixture { +public: + void SetUp(::benchmark::State &state) {} + + void TearDown(::benchmark::State &state) {} +}; +BENCHMARK_F(MyFixture, FooTest)(benchmark::State &st) { + for (auto _ : st) { + } +} +BENCHMARK_DEFINE_F(MyFixture, BarTest)(benchmark::State &st) { + for (auto _ : st) { + } +} +BENCHMARK_REGISTER_F(MyFixture, BarTest); + +// +// + +template class MyTemplatedFixture : public benchmark::Fixture {}; +BENCHMARK_TEMPLATE_F(MyTemplatedFixture, IntTest, int)(benchmark::State &st) { + for (auto _ : st) { + } +} +BENCHMARK_TEMPLATE_DEFINE_F(MyTemplatedFixture, DoubleTest, + double)(benchmark::State &st) { + for (auto _ : st) { + } +} +BENCHMARK_REGISTER_F(MyTemplatedFixture, DoubleTest); + +// +// + +template class MyTemplate1 : public benchmark::Fixture {}; +BENCHMARK_TEMPLATE1_DEFINE_F(MyTemplate1, TestA, int)(benchmark::State &st) { + for (auto _ : st) { + } +} +BENCHMARK_REGISTER_F(MyTemplate1, TestA); + +// +// + +template class MyTemplate2 : public benchmark::Fixture {}; +BENCHMARK_TEMPLATE2_DEFINE_F(MyTemplate2, TestB, int, double)(benchmark::State &st) { + for (auto _ : st) { + } +} +BENCHMARK_REGISTER_F(MyTemplate2, TestB); + +} + +#endif diff --git a/examples/google_benchmark/main.cpp b/examples/google_benchmark/main.cpp index 143259c..496ae3f 100644 --- a/examples/google_benchmark/main.cpp +++ b/examples/google_benchmark/main.cpp @@ -1,3 +1,4 @@ +#include "fixture_bench.hpp" #include "template_bench.hpp" #include #include diff --git a/google_benchmark/include/benchmark/benchmark.h b/google_benchmark/include/benchmark/benchmark.h index e4684b1..48f06e9 100644 --- a/google_benchmark/include/benchmark/benchmark.h +++ b/google_benchmark/include/benchmark/benchmark.h @@ -1447,6 +1447,7 @@ class Fixture : public internal::Benchmark { std::filesystem::relative(__FILE__, CODSPEED_GIT_ROOT_DIR).string() + "::" #define NAMESPACE \ (([]() { return extract_lambda_namespace(__PRETTY_FUNCTION__); })()) +#define STATIC_NAMESPACE_STRING(name) static std::string name = NAMESPACE; #define FILE_AND_NAMESPACE CUR_FILE + NAMESPACE @@ -1581,6 +1582,62 @@ class Fixture : public internal::Benchmark { [](::benchmark::State& st) { func(st, __VA_ARGS__); }))) #endif +#ifdef CODSPEED_ENABLED + +#define BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \ + STATIC_NAMESPACE_STRING(ns_##BaseClass##_##Method); \ + class BaseClass##_##Method##_Benchmark : public BaseClass { \ + public: \ + BaseClass##_##Method##_Benchmark() { \ + this->SetName(CUR_FILE + ns_##BaseClass##_##Method + \ + #Method "[" #BaseClass "]"); \ + } \ + \ + protected: \ + void BenchmarkCase(::benchmark::State&) override; \ + }; + +#define BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(BaseClass, Method, a) \ + STATIC_NAMESPACE_STRING(ns_##BaseClass##_##Method); \ + class BaseClass##_##Method##_Benchmark : public BaseClass { \ + public: \ + BaseClass##_##Method##_Benchmark() { \ + this->SetName(CUR_FILE + ns_##BaseClass##_##Method + \ + #Method "[" #BaseClass ", " #a "]"); \ + } \ + \ + protected: \ + void BenchmarkCase(::benchmark::State&) override; \ + }; + +#define BENCHMARK_TEMPLATE2_PRIVATE_DECLARE_F(BaseClass, Method, a, b) \ + STATIC_NAMESPACE_STRING(ns_##BaseClass##_##Method); \ + class BaseClass##_##Method##_Benchmark : public BaseClass { \ + public: \ + BaseClass##_##Method##_Benchmark() { \ + this->SetName(CUR_FILE + ns_##BaseClass##_##Method + \ + #Method "[" #BaseClass ", " #a ", " #b "]"); \ + } \ + \ + protected: \ + void BenchmarkCase(::benchmark::State&) override; \ + }; + +#define BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(BaseClass, Method, ...) \ + STATIC_NAMESPACE_STRING(ns_##BaseClass##_##Method); \ + class BaseClass##_##Method##_Benchmark : public BaseClass<__VA_ARGS__> { \ + public: \ + BaseClass##_##Method##_Benchmark() { \ + this->SetName(CUR_FILE + ns_##BaseClass##_##Method + \ + #Method "[" #BaseClass ", " #__VA_ARGS__ "]"); \ + } \ + \ + protected: \ + void BenchmarkCase(::benchmark::State&) override; \ + }; + +#else // CODSPEED_ENABLED undefined: + #define BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \ class BaseClass##_##Method##_Benchmark : public BaseClass { \ public: \ @@ -1602,7 +1659,6 @@ class Fixture : public internal::Benchmark { protected: \ void BenchmarkCase(::benchmark::State&) override; \ }; - #define BENCHMARK_TEMPLATE2_PRIVATE_DECLARE_F(BaseClass, Method, a, b) \ class BaseClass##_##Method##_Benchmark : public BaseClass { \ public: \ @@ -1624,6 +1680,7 @@ class Fixture : public internal::Benchmark { protected: \ void BenchmarkCase(::benchmark::State&) override; \ }; +#endif #define BENCHMARK_DEFINE_F(BaseClass, Method) \ BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \