Skip to content

feat: add support for fixtures #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions examples/google_benchmark/fixture_bench.hpp
Original file line number Diff line number Diff line change
@@ -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 <benchmark/benchmark.h>

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 <typename T> 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 <typename T> class MyTemplate1 : public benchmark::Fixture {};
BENCHMARK_TEMPLATE1_DEFINE_F(MyTemplate1, TestA, int)(benchmark::State &st) {
for (auto _ : st) {
}
}
BENCHMARK_REGISTER_F(MyTemplate1, TestA);

//
//

template <typename T, typename U> 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
1 change: 1 addition & 0 deletions examples/google_benchmark/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "fixture_bench.hpp"
#include "template_bench.hpp"
#include <benchmark/benchmark.h>
#include <cstring>
Expand Down
59 changes: 58 additions & 1 deletion google_benchmark/include/benchmark/benchmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -1581,6 +1582,62 @@ class Fixture : public internal::Benchmark {
[](::benchmark::State& st) { func<a, b>(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<a> { \
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<a, b> { \
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: \
Expand All @@ -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<a, b> { \
public: \
Expand All @@ -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) \
Expand Down