Skip to content

Commit 6d7ad01

Browse files
not-matthiasart049
authored andcommitted
feat(google_benchmark): add namespace to URI
1 parent 2d2b0c3 commit 6d7ad01

File tree

5 files changed

+75
-27
lines changed

5 files changed

+75
-27
lines changed

core/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED True)
1212
include_directories(include)
1313

1414
# Add the library
15-
add_library(codspeed src/codspeed.cpp src/walltime.cpp)
15+
add_library(codspeed src/codspeed.cpp src/walltime.cpp src/uri.cpp)
1616

1717
# Version
1818
add_compile_definitions(CODSPEED_VERSION="${CODSPEED_VERSION}")

core/include/codspeed.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,6 @@ struct RawWalltimeBenchmark {
4141
void generate_codspeed_walltime_report(
4242
const std::vector<RawWalltimeBenchmark> &walltime_data_list);
4343

44+
std::string extract_lambda_namespace(const std::string &pretty_func);
45+
4446
#endif // CODSPEED_H

core/src/uri.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include "codspeed.h"
2+
#include <string>
3+
#include <iostream>
4+
5+
std::string extract_lambda_namespace(const std::string& pretty_func) {
6+
if (pretty_func.find("(anonymous namespace)") != std::string::npos) {
7+
std::cerr << "[ERROR] Anonymous namespace not supported in " << pretty_func << std::endl;
8+
return {};
9+
}
10+
11+
#ifdef __clang__
12+
// Example: auto outer::test12::(anonymous class)::operator()() const
13+
14+
std::size_t anon_class_pos = pretty_func.find("(anonymous class)");
15+
std::size_t space_pos = pretty_func.find(' ');
16+
17+
if (space_pos == std::string::npos || anon_class_pos == std::string::npos) {
18+
return {};
19+
}
20+
21+
return pretty_func.substr(space_pos, anon_class_pos - space_pos) + "::";
22+
#elif __GNUC__
23+
// Example: outer::test12::<lambda()>
24+
25+
auto lambda_pos = pretty_func.find("::<lambda()>");
26+
if (lambda_pos == std::string::npos) {
27+
return {};
28+
}
29+
30+
return pretty_func.substr(0, lambda_pos) + "::";
31+
#else
32+
#error "Unsupported compiler"
33+
#endif
34+
}

examples/google_benchmark/template_bench.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <cstring>
55
#include <string>
66

7+
namespace test {
78
template <class T> void BM_Template(benchmark::State &state) {
89
std::vector<T> v;
910
for (auto _ : state) {
@@ -12,6 +13,7 @@ template <class T> void BM_Template(benchmark::State &state) {
1213
}
1314
BENCHMARK_TEMPLATE(BM_Template, int);
1415
BENCHMARK_TEMPLATE(BM_Template, std::string);
16+
}
1517

1618
//
1719
//

google_benchmark/include/benchmark/benchmark.h

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,9 +1439,16 @@ class Fixture : public internal::Benchmark {
14391439
n) [[maybe_unused]]
14401440

14411441
#ifdef CODSPEED_ENABLED
1442+
#include <codspeed.h>
1443+
14421444
#include <filesystem>
1445+
14431446
#define CUR_FILE \
14441447
std::filesystem::relative(__FILE__, CODSPEED_GIT_ROOT_DIR).string() + "::"
1448+
#define NAMESPACE \
1449+
(([]() { return extract_lambda_namespace(__PRETTY_FUNCTION__); })())
1450+
1451+
#define FILE_AND_NAMESPACE CUR_FILE + NAMESPACE
14451452

14461453
// Transforms `BM_Foo<int, double>` into `BM_Foo[int, double]`.
14471454
#define TYPE_START "["
@@ -1454,7 +1461,7 @@ class Fixture : public internal::Benchmark {
14541461
// Extra space after the comma for readability
14551462
#define COMMA ", "
14561463
#else
1457-
#define CUR_FILE std::string()
1464+
#define FILE_AND_NAMESPACE std::string()
14581465

14591466
#define TYPE_START "<"
14601467
#define TYPE_END ">"
@@ -1469,7 +1476,7 @@ class Fixture : public internal::Benchmark {
14691476
BENCHMARK_PRIVATE_DECLARE(_benchmark_) = \
14701477
(::benchmark::internal::RegisterBenchmarkInternal( \
14711478
std::make_unique<::benchmark::internal::FunctionBenchmark>( \
1472-
CUR_FILE + #__VA_ARGS__, __VA_ARGS__)))
1479+
FILE_AND_NAMESPACE + #__VA_ARGS__, __VA_ARGS__)))
14731480

14741481
// Old-style macros
14751482
#define BENCHMARK_WITH_ARG(n, a) BENCHMARK(n)->Arg((a))
@@ -1490,11 +1497,11 @@ class Fixture : public internal::Benchmark {
14901497
//}
14911498
// /* Registers a benchmark named "BM_takes_args/int_string_test` */
14921499
// BENCHMARK_CAPTURE(BM_takes_args, int_string_test, 42, std::string("abc"));
1493-
#define BENCHMARK_CAPTURE(func, test_case_name, ...) \
1494-
BENCHMARK_PRIVATE_DECLARE(_benchmark_) = \
1495-
(::benchmark::internal::RegisterBenchmarkInternal( \
1496-
std::make_unique<::benchmark::internal::FunctionBenchmark>( \
1497-
CUR_FILE + #func NAME_START #test_case_name NAME_END, \
1500+
#define BENCHMARK_CAPTURE(func, test_case_name, ...) \
1501+
BENCHMARK_PRIVATE_DECLARE(_benchmark_) = \
1502+
(::benchmark::internal::RegisterBenchmarkInternal( \
1503+
std::make_unique<::benchmark::internal::FunctionBenchmark>( \
1504+
FILE_AND_NAMESPACE + #func NAME_START #test_case_name NAME_END, \
14981505
[](::benchmark::State& st) { func(st, __VA_ARGS__); })))
14991506

15001507
// This will register a benchmark for a templatized function. For example:
@@ -1509,19 +1516,20 @@ class Fixture : public internal::Benchmark {
15091516
BENCHMARK_PRIVATE_DECLARE(n) = \
15101517
(::benchmark::internal::RegisterBenchmarkInternal( \
15111518
std::make_unique<::benchmark::internal::FunctionBenchmark>( \
1512-
CUR_FILE + #n TYPE_START #a TYPE_END, n<a>)))
1513-
1514-
#define BENCHMARK_TEMPLATE2(n, a, b) \
1515-
BENCHMARK_PRIVATE_DECLARE(n) = \
1516-
(::benchmark::internal::RegisterBenchmarkInternal( \
1517-
std::make_unique<::benchmark::internal::FunctionBenchmark>( \
1518-
CUR_FILE + #n TYPE_START #a COMMA #b TYPE_END, n<a, b>)))
1519-
1520-
#define BENCHMARK_TEMPLATE(n, ...) \
1521-
BENCHMARK_PRIVATE_DECLARE(n) = \
1522-
(::benchmark::internal::RegisterBenchmarkInternal( \
1523-
std::make_unique<::benchmark::internal::FunctionBenchmark>( \
1524-
CUR_FILE + #n TYPE_START #__VA_ARGS__ TYPE_END, \
1519+
FILE_AND_NAMESPACE + #n TYPE_START #a TYPE_END, n<a>)))
1520+
1521+
#define BENCHMARK_TEMPLATE2(n, a, b) \
1522+
BENCHMARK_PRIVATE_DECLARE(n) = \
1523+
(::benchmark::internal::RegisterBenchmarkInternal( \
1524+
std::make_unique<::benchmark::internal::FunctionBenchmark>( \
1525+
FILE_AND_NAMESPACE + #n TYPE_START #a COMMA #b TYPE_END, \
1526+
n<a, b>)))
1527+
1528+
#define BENCHMARK_TEMPLATE(n, ...) \
1529+
BENCHMARK_PRIVATE_DECLARE(n) = \
1530+
(::benchmark::internal::RegisterBenchmarkInternal( \
1531+
std::make_unique<::benchmark::internal::FunctionBenchmark>( \
1532+
FILE_AND_NAMESPACE + #n TYPE_START #__VA_ARGS__ TYPE_END, \
15251533
n<__VA_ARGS__>)))
15261534

15271535
// This will register a benchmark for a templatized function,
@@ -1544,7 +1552,8 @@ class Fixture : public internal::Benchmark {
15441552
BENCHMARK_PRIVATE_DECLARE(_benchmark_) = \
15451553
(::benchmark::internal::RegisterBenchmarkInternal( \
15461554
std::make_unique<::benchmark::internal::FunctionBenchmark>( \
1547-
CUR_FILE + #func_name NAME_START #test_case_name NAME_END, \
1555+
FILE_AND_NAMESPACE + \
1556+
#func_name NAME_START #test_case_name NAME_END, \
15481557
[](::benchmark::State& st) { func(st, __VA_ARGS__); })))
15491558

15501559
#define BENCHMARK_TEMPLATE1_CAPTURE(func, a, test_case_name, ...) \
@@ -1555,11 +1564,12 @@ class Fixture : public internal::Benchmark {
15551564
#endif
15561565

15571566
#ifdef CODSPEED_ENABLED
1558-
#define BENCHMARK_TEMPLATE2_CAPTURE(func, a, b, test_case_name, ...) \
1559-
BENCHMARK_PRIVATE_DECLARE(func) = \
1560-
(::benchmark::internal::RegisterBenchmarkInternal( \
1561-
std::make_unique<::benchmark::internal::FunctionBenchmark>( \
1562-
CUR_FILE + #func "[" #test_case_name COMMA #a COMMA #b "]", \
1567+
#define BENCHMARK_TEMPLATE2_CAPTURE(func, a, b, test_case_name, ...) \
1568+
BENCHMARK_PRIVATE_DECLARE(func) = \
1569+
(::benchmark::internal::RegisterBenchmarkInternal( \
1570+
std::make_unique<::benchmark::internal::FunctionBenchmark>( \
1571+
FILE_AND_NAMESPACE + #func "[" #test_case_name COMMA #a COMMA #b \
1572+
"]", \
15631573
[](::benchmark::State& st) { func<a, b>(st, __VA_ARGS__); })))
15641574
#else
15651575
#define BENCHMARK_TEMPLATE2_CAPTURE(func, a, b, test_case_name, ...) \

0 commit comments

Comments
 (0)