Skip to content

Commit 772a2db

Browse files
committed
feat: add support for fixtures
1 parent 1dda619 commit 772a2db

File tree

3 files changed

+124
-1
lines changed

3 files changed

+124
-1
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// See docs:
2+
// https://github.com/google/benchmark/blob/main/docs/user_guide.md#fixtures
3+
4+
#ifndef FIXTURE_BENCH_HPP
5+
#define FIXTURE_BENCH_HPP
6+
7+
8+
#include <benchmark/benchmark.h>
9+
10+
namespace example_namespace {
11+
12+
class MyFixture : public benchmark::Fixture {
13+
public:
14+
void SetUp(::benchmark::State &state) {}
15+
16+
void TearDown(::benchmark::State &state) {}
17+
};
18+
BENCHMARK_F(MyFixture, FooTest)(benchmark::State &st) {
19+
for (auto _ : st) {
20+
}
21+
}
22+
BENCHMARK_DEFINE_F(MyFixture, BarTest)(benchmark::State &st) {
23+
for (auto _ : st) {
24+
}
25+
}
26+
BENCHMARK_REGISTER_F(MyFixture, BarTest);
27+
28+
//
29+
//
30+
31+
template <typename T> class MyTemplatedFixture : public benchmark::Fixture {};
32+
BENCHMARK_TEMPLATE_F(MyTemplatedFixture, IntTest, int)(benchmark::State &st) {
33+
for (auto _ : st) {
34+
}
35+
}
36+
BENCHMARK_TEMPLATE_DEFINE_F(MyTemplatedFixture, DoubleTest,
37+
double)(benchmark::State &st) {
38+
for (auto _ : st) {
39+
}
40+
}
41+
BENCHMARK_REGISTER_F(MyTemplatedFixture, DoubleTest);
42+
43+
//
44+
//
45+
46+
template <typename T> class MyTemplate1 : public benchmark::Fixture {};
47+
BENCHMARK_TEMPLATE1_DEFINE_F(MyTemplate1, TestA, int)(benchmark::State &st) {
48+
for (auto _ : st) {
49+
}
50+
}
51+
BENCHMARK_REGISTER_F(MyTemplate1, TestA);
52+
53+
//
54+
//
55+
56+
template <typename T, typename U> class MyTemplate2 : public benchmark::Fixture {};
57+
BENCHMARK_TEMPLATE2_DEFINE_F(MyTemplate2, TestB, int, double)(benchmark::State &st) {
58+
for (auto _ : st) {
59+
}
60+
}
61+
BENCHMARK_REGISTER_F(MyTemplate2, TestB);
62+
63+
}
64+
65+
#endif

examples/google_benchmark/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "fixture_bench.hpp"
12
#include "template_bench.hpp"
23
#include <benchmark/benchmark.h>
34
#include <cstring>

google_benchmark/include/benchmark/benchmark.h

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1447,6 +1447,7 @@ class Fixture : public internal::Benchmark {
14471447
std::filesystem::relative(__FILE__, CODSPEED_GIT_ROOT_DIR).string() + "::"
14481448
#define NAMESPACE \
14491449
(([]() { return extract_lambda_namespace(__PRETTY_FUNCTION__); })())
1450+
#define STATIC_NAMESPACE_STRING(name) static std::string name = NAMESPACE;
14501451

14511452
#define FILE_AND_NAMESPACE CUR_FILE + NAMESPACE
14521453

@@ -1581,6 +1582,62 @@ class Fixture : public internal::Benchmark {
15811582
[](::benchmark::State& st) { func<a, b>(st, __VA_ARGS__); })))
15821583
#endif
15831584

1585+
#ifdef CODSPEED_ENABLED
1586+
1587+
#define BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \
1588+
STATIC_NAMESPACE_STRING(ns_##BaseClass##_##Method); \
1589+
class BaseClass##_##Method##_Benchmark : public BaseClass { \
1590+
public: \
1591+
BaseClass##_##Method##_Benchmark() { \
1592+
this->SetName(CUR_FILE + ns_##BaseClass##_##Method + \
1593+
#Method "[" #BaseClass "]"); \
1594+
} \
1595+
\
1596+
protected: \
1597+
void BenchmarkCase(::benchmark::State&) override; \
1598+
};
1599+
1600+
#define BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(BaseClass, Method, a) \
1601+
STATIC_NAMESPACE_STRING(ns_##BaseClass##_##Method); \
1602+
class BaseClass##_##Method##_Benchmark : public BaseClass<a> { \
1603+
public: \
1604+
BaseClass##_##Method##_Benchmark() { \
1605+
this->SetName(CUR_FILE + ns_##BaseClass##_##Method + \
1606+
#Method "[" #BaseClass ", " #a "]"); \
1607+
} \
1608+
\
1609+
protected: \
1610+
void BenchmarkCase(::benchmark::State&) override; \
1611+
};
1612+
1613+
#define BENCHMARK_TEMPLATE2_PRIVATE_DECLARE_F(BaseClass, Method, a, b) \
1614+
STATIC_NAMESPACE_STRING(ns_##BaseClass##_##Method); \
1615+
class BaseClass##_##Method##_Benchmark : public BaseClass<a, b> { \
1616+
public: \
1617+
BaseClass##_##Method##_Benchmark() { \
1618+
this->SetName(CUR_FILE + ns_##BaseClass##_##Method + \
1619+
#Method "[" #BaseClass ", " #a ", " #b "]"); \
1620+
} \
1621+
\
1622+
protected: \
1623+
void BenchmarkCase(::benchmark::State&) override; \
1624+
};
1625+
1626+
#define BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(BaseClass, Method, ...) \
1627+
STATIC_NAMESPACE_STRING(ns_##BaseClass##_##Method); \
1628+
class BaseClass##_##Method##_Benchmark : public BaseClass<__VA_ARGS__> { \
1629+
public: \
1630+
BaseClass##_##Method##_Benchmark() { \
1631+
this->SetName(CUR_FILE + ns_##BaseClass##_##Method + \
1632+
#Method "[" #BaseClass ", " #__VA_ARGS__ "]"); \
1633+
} \
1634+
\
1635+
protected: \
1636+
void BenchmarkCase(::benchmark::State&) override; \
1637+
};
1638+
1639+
#else // CODSPEED_ENABLED undefined:
1640+
15841641
#define BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \
15851642
class BaseClass##_##Method##_Benchmark : public BaseClass { \
15861643
public: \
@@ -1602,7 +1659,6 @@ class Fixture : public internal::Benchmark {
16021659
protected: \
16031660
void BenchmarkCase(::benchmark::State&) override; \
16041661
};
1605-
16061662
#define BENCHMARK_TEMPLATE2_PRIVATE_DECLARE_F(BaseClass, Method, a, b) \
16071663
class BaseClass##_##Method##_Benchmark : public BaseClass<a, b> { \
16081664
public: \
@@ -1624,6 +1680,7 @@ class Fixture : public internal::Benchmark {
16241680
protected: \
16251681
void BenchmarkCase(::benchmark::State&) override; \
16261682
};
1683+
#endif
16271684

16281685
#define BENCHMARK_DEFINE_F(BaseClass, Method) \
16291686
BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \

0 commit comments

Comments
 (0)