Skip to content

Commit 10fe19b

Browse files
committed
fix(google_benchmark): generate proper uri
1 parent c41090c commit 10fe19b

File tree

2 files changed

+79
-16
lines changed

2 files changed

+79
-16
lines changed

examples/google_benchmark/main.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,60 @@
11
#include <benchmark/benchmark.h>
22
#include <cstring>
33

4+
// Passing Arbitrary Arguments to a Benchmark
5+
// See:
6+
// https://github.com/google/benchmark/blob/main/docs/user_guide.md#passing-arbitrary-arguments-to-a-benchmark
7+
template <class... Args>
8+
void BM_takes_args(benchmark::State &state, Args &&...args) {
9+
auto args_tuple = std::make_tuple(std::move(args)...);
10+
for (auto _ : state) {
11+
}
12+
}
13+
BENCHMARK_CAPTURE(BM_takes_args, int_string_test, 42, std::string("abc"));
14+
BENCHMARK_CAPTURE(BM_takes_args, int_test, 42, 43);
15+
16+
// Benchmark template
17+
//
18+
template <class T> void BM_VectorPushBack(benchmark::State &state) {
19+
std::vector<T> v;
20+
for (auto _ : state) {
21+
v.push_back(T());
22+
}
23+
}
24+
BENCHMARK_TEMPLATE(BM_VectorPushBack, int);
25+
BENCHMARK_TEMPLATE(BM_VectorPushBack, std::string);
26+
27+
template <typename T> void BM_Template1(benchmark::State &state) {
28+
T val = T();
29+
for (auto _ : state) {
30+
benchmark::DoNotOptimize(val++);
31+
}
32+
}
33+
BENCHMARK_TEMPLATE1(BM_Template1, int);
34+
35+
template <typename T, typename U> void BM_Template2(benchmark::State &state) {
36+
T t = T();
37+
U u = U();
38+
for (auto _ : state) {
39+
benchmark::DoNotOptimize(t + u);
40+
}
41+
}
42+
BENCHMARK_TEMPLATE2(BM_Template2, int, double);
43+
44+
// Benchmark template capture
45+
//
46+
template <typename T, class... ExtraArgs>
47+
void BM_takes_args(benchmark::State &state, ExtraArgs &&...extra_args) {
48+
auto args_tuple = std::make_tuple(std::move(extra_args)...);
49+
for (auto _ : state) {
50+
}
51+
}
52+
BENCHMARK_TEMPLATE1_CAPTURE(BM_takes_args, void, int_string_test, 42,
53+
std::string("abc"));
54+
55+
BENCHMARK_TEMPLATE2_CAPTURE(BM_takes_args, int, double, two_type_test, 42,
56+
std::string("abc"));
57+
458
// Function to benchmark
559
static void BM_rand_vector(benchmark::State &state) {
660
std::vector<int> v;

google_benchmark/include/benchmark/benchmark.h

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,21 +1440,23 @@ class Fixture : public internal::Benchmark {
14401440

14411441
#ifdef CODSPEED_ENABLED
14421442
#include <filesystem>
1443-
#define BENCHMARK(...) \
1444-
BENCHMARK_PRIVATE_DECLARE(_benchmark_) = \
1445-
(::benchmark::internal::RegisterBenchmarkInternal( \
1446-
std::make_unique<::benchmark::internal::FunctionBenchmark>( \
1447-
std::filesystem::relative(__FILE__, CODSPEED_GIT_ROOT_DIR) \
1448-
.string() + \
1449-
"::" + #__VA_ARGS__, \
1450-
__VA_ARGS__)))
1443+
#define CUR_FILE \
1444+
std::filesystem::relative(__FILE__, CODSPEED_GIT_ROOT_DIR).string() + "::"
1445+
#define TYPE_START "["
1446+
#define TYPE_END "]"
1447+
#define FUNC_TEST_SEP "::"
14511448
#else
1449+
#define CUR_FILE std::string()
1450+
#define TYPE_START "<"
1451+
#define TYPE_END ">"
1452+
#define FUNC_TEST_SEP "/"
1453+
#endif
1454+
14521455
#define BENCHMARK(...) \
14531456
BENCHMARK_PRIVATE_DECLARE(_benchmark_) = \
14541457
(::benchmark::internal::RegisterBenchmarkInternal( \
14551458
std::make_unique<::benchmark::internal::FunctionBenchmark>( \
1456-
#__VA_ARGS__, __VA_ARGS__)))
1457-
#endif
1459+
CUR_FILE + #__VA_ARGS__, __VA_ARGS__)))
14581460

14591461
// Old-style macros
14601462
#define BENCHMARK_WITH_ARG(n, a) BENCHMARK(n)->Arg((a))
@@ -1479,7 +1481,7 @@ class Fixture : public internal::Benchmark {
14791481
BENCHMARK_PRIVATE_DECLARE(_benchmark_) = \
14801482
(::benchmark::internal::RegisterBenchmarkInternal( \
14811483
std::make_unique<::benchmark::internal::FunctionBenchmark>( \
1482-
#func "/" #test_case_name, \
1484+
CUR_FILE + #func FUNC_TEST_SEP #test_case_name, \
14831485
[](::benchmark::State& st) { func(st, __VA_ARGS__); })))
14841486

14851487
// This will register a benchmark for a templatized function. For example:
@@ -1494,19 +1496,20 @@ class Fixture : public internal::Benchmark {
14941496
BENCHMARK_PRIVATE_DECLARE(n) = \
14951497
(::benchmark::internal::RegisterBenchmarkInternal( \
14961498
std::make_unique<::benchmark::internal::FunctionBenchmark>( \
1497-
#n "<" #a ">", n<a>)))
1499+
CUR_FILE + #n TYPE_START #a TYPE_END, n<a>)))
14981500

14991501
#define BENCHMARK_TEMPLATE2(n, a, b) \
15001502
BENCHMARK_PRIVATE_DECLARE(n) = \
15011503
(::benchmark::internal::RegisterBenchmarkInternal( \
15021504
std::make_unique<::benchmark::internal::FunctionBenchmark>( \
1503-
#n "<" #a "," #b ">", n<a, b>)))
1505+
CUR_FILE + #n TYPE_START #a "," #b TYPE_END, n<a, b>)))
15041506

15051507
#define BENCHMARK_TEMPLATE(n, ...) \
15061508
BENCHMARK_PRIVATE_DECLARE(n) = \
15071509
(::benchmark::internal::RegisterBenchmarkInternal( \
15081510
std::make_unique<::benchmark::internal::FunctionBenchmark>( \
1509-
#n "<" #__VA_ARGS__ ">", n<__VA_ARGS__>)))
1511+
CUR_FILE + #n TYPE_START #__VA_ARGS__ TYPE_END, \
1512+
n<__VA_ARGS__>)))
15101513

15111514
// This will register a benchmark for a templatized function,
15121515
// with the additional arguments specified by `...`.
@@ -1520,15 +1523,20 @@ class Fixture : public internal::Benchmark {
15201523
// /* Registers a benchmark named "BM_takes_args<void>/int_string_test` */
15211524
// BENCHMARK_TEMPLATE1_CAPTURE(BM_takes_args, void, int_string_test, 42,
15221525
// std::string("abc"));
1526+
#ifdef CODSPEED_ENABLED
1527+
#define BENCHMARK_TEMPLATE1_CAPTURE(func, a, test_case_name, ...) \
1528+
BENCHMARK_CAPTURE(func, test_case_name, __VA_ARGS__)
1529+
#else
15231530
#define BENCHMARK_TEMPLATE1_CAPTURE(func, a, test_case_name, ...) \
15241531
BENCHMARK_CAPTURE(func<a>, test_case_name, __VA_ARGS__)
1532+
#endif
15251533

15261534
#define BENCHMARK_TEMPLATE2_CAPTURE(func, a, b, test_case_name, ...) \
15271535
BENCHMARK_PRIVATE_DECLARE(func) = \
15281536
(::benchmark::internal::RegisterBenchmarkInternal( \
15291537
std::make_unique<::benchmark::internal::FunctionBenchmark>( \
1530-
#func "<" #a "," #b ">" \
1531-
"/" #test_case_name, \
1538+
CUR_FILE + #func TYPE_START #a \
1539+
"," #b TYPE_END FUNC_TEST_SEP #test_case_name, \
15321540
[](::benchmark::State& st) { func<a, b>(st, __VA_ARGS__); })))
15331541

15341542
#define BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \
@@ -1680,6 +1688,7 @@ struct BENCHMARK_EXPORT SystemInfo {
16801688
// which allows individual fields to be modified or cleared before
16811689
// building the final name using 'str()'.
16821690
struct BENCHMARK_EXPORT BenchmarkName {
1691+
std::string path;
16831692
std::string function_name;
16841693
std::string args;
16851694
std::string min_time;

0 commit comments

Comments
 (0)