Skip to content

Commit 0e0dd66

Browse files
committed
feat: add support for fixtures
1 parent 1dda619 commit 0e0dd66

File tree

3 files changed

+108
-2
lines changed

3 files changed

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

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: 46 additions & 2 deletions
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

@@ -1461,7 +1462,9 @@ class Fixture : public internal::Benchmark {
14611462
// Extra space after the comma for readability
14621463
#define COMMA ", "
14631464
#else
1465+
#define CUR_FILE std::string()
14641466
#define FILE_AND_NAMESPACE std::string()
1467+
#define STATIC_NAMESPACE_STRING(name) static std::string name = "";
14651468

14661469
#define TYPE_START "<"
14671470
#define TYPE_END ">"
@@ -1582,27 +1585,67 @@ class Fixture : public internal::Benchmark {
15821585
#endif
15831586

15841587
#define BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \
1588+
STATIC_NAMESPACE_STRING(ns_##BaseClass##_##Method); \
15851589
class BaseClass##_##Method##_Benchmark : public BaseClass { \
15861590
public: \
15871591
BaseClass##_##Method##_Benchmark() { \
1588-
this->SetName(#BaseClass "/" #Method); \
1592+
this->SetName(CUR_FILE + ns_##BaseClass##_##Method + \
1593+
#BaseClass NAME_START #Method NAME_END); \
15891594
} \
15901595
\
15911596
protected: \
15921597
void BenchmarkCase(::benchmark::State&) override; \
15931598
};
15941599

1600+
#ifdef CODSPEED_ENABLED
15951601
#define BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(BaseClass, Method, a) \
1602+
STATIC_NAMESPACE_STRING(ns_##BaseClass##_##Method); \
15961603
class BaseClass##_##Method##_Benchmark : public BaseClass<a> { \
15971604
public: \
15981605
BaseClass##_##Method##_Benchmark() { \
1599-
this->SetName(#BaseClass "<" #a ">/" #Method); \
1606+
this->SetName(CUR_FILE + ns_##BaseClass##_##Method + \
1607+
#BaseClass "[" #Method ", " #a "]"); \
16001608
} \
16011609
\
16021610
protected: \
16031611
void BenchmarkCase(::benchmark::State&) override; \
16041612
};
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+
#BaseClass "[" #Method ", " #a ", " #b "]"); \
1620+
} \
1621+
\
1622+
protected: \
1623+
void BenchmarkCase(::benchmark::State&) override; \
1624+
};
16051625

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+
#BaseClass "[" #Method ", " #__VA_ARGS__ "]"); \
1633+
} \
1634+
\
1635+
protected: \
1636+
void BenchmarkCase(::benchmark::State&) override; \
1637+
};
1638+
#else
1639+
#define BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(BaseClass, Method, a) \
1640+
class BaseClass##_##Method##_Benchmark : public BaseClass<a> { \
1641+
public: \
1642+
BaseClass##_##Method##_Benchmark() { \
1643+
this->SetName(#BaseClass "<" #a ">/" #Method); \
1644+
} \
1645+
\
1646+
protected: \
1647+
void BenchmarkCase(::benchmark::State&) override; \
1648+
};
16061649
#define BENCHMARK_TEMPLATE2_PRIVATE_DECLARE_F(BaseClass, Method, a, b) \
16071650
class BaseClass##_##Method##_Benchmark : public BaseClass<a, b> { \
16081651
public: \
@@ -1624,6 +1667,7 @@ class Fixture : public internal::Benchmark {
16241667
protected: \
16251668
void BenchmarkCase(::benchmark::State&) override; \
16261669
};
1670+
#endif
16271671

16281672
#define BENCHMARK_DEFINE_F(BaseClass, Method) \
16291673
BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \

0 commit comments

Comments
 (0)