Skip to content

Commit 680721a

Browse files
refactor(core): add a codspeed namespace for codspeed core functions
1 parent b6cbff9 commit 680721a

File tree

11 files changed

+68
-41
lines changed

11 files changed

+68
-41
lines changed

core/include/codspeed.h

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

7+
namespace codspeed {
8+
79
class CodSpeed {
810
public:
911
// Public static method to access the single instance
@@ -44,4 +46,6 @@ void generate_codspeed_walltime_report(
4446
std::string extract_lambda_namespace(const std::string &pretty_func);
4547
std::string sanitize_bench_args(std::string &text);
4648

49+
} // namespace codspeed
50+
4751
#endif // CODSPEED_H

core/src/codspeed.cpp

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

7+
namespace codspeed {
78
// Remove any `::` between brackets at the end to not mess with the URI
89
// parsing
910
// FIXME: Remove this bandaid when we migrate to structured benchmark metadata
@@ -91,3 +92,5 @@ void CodSpeed::end_benchmark() {
9192
std::cerr << action_str << ": " << current_benchmark << group_str
9293
<< std::endl;
9394
}
95+
96+
} // namespace codspeed

core/src/uri.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#include "codspeed.h"
2-
#include <string>
32
#include <iostream>
3+
#include <string>
4+
5+
namespace codspeed {
46

57
// Example: auto outer::test12::(anonymous class)::operator()() const
68
// Returns: outer::test12::
7-
std::string extract_namespace_clang(const std::string& pretty_func) {
9+
std::string extract_namespace_clang(const std::string &pretty_func) {
810
std::size_t anon_class_pos = pretty_func.find("::(anonymous class)");
911
std::size_t space_pos = pretty_func.find(' ');
1012

@@ -18,31 +20,34 @@ std::string extract_namespace_clang(const std::string& pretty_func) {
1820

1921
// Example: outer::test12::<lambda()>
2022
// Returns: outer::test12::
21-
std::string extract_namespace_gcc(const std::string& pretty_func) {
22-
auto lambda_pos = pretty_func.find("::<lambda()>");
23-
if (lambda_pos == std::string::npos) {
24-
return {};
25-
}
23+
std::string extract_namespace_gcc(const std::string &pretty_func) {
24+
auto lambda_pos = pretty_func.find("::<lambda()>");
25+
if (lambda_pos == std::string::npos) {
26+
return {};
27+
}
2628

27-
return pretty_func.substr(0, lambda_pos) + "::";
29+
return pretty_func.substr(0, lambda_pos) + "::";
2830
}
2931

30-
// Has to pass the pretty function from a lambda:
32+
// Has to pass the pretty function from a lambda:
3133
// (([]() { return __PRETTY_FUNCTION__; })())
3234
//
33-
// Returns: An empty string if the namespace could not be extracted,
35+
// Returns: An empty string if the namespace could not be extracted,
3436
// otherwise the namespace with a trailing "::"
35-
std::string extract_lambda_namespace(const std::string& pretty_func) {
37+
std::string extract_lambda_namespace(const std::string &pretty_func) {
3638
if (pretty_func.find("(anonymous namespace)") != std::string::npos) {
37-
std::cerr << "[ERROR] Anonymous namespace not supported in " << pretty_func << std::endl;
39+
std::cerr << "[ERROR] Anonymous namespace not supported in " << pretty_func
40+
<< std::endl;
3841
return {};
3942
}
4043

4144
#ifdef __clang__
42-
return extract_namespace_clang(pretty_func);
45+
return extract_namespace_clang(pretty_func);
4346
#elif __GNUC__
44-
return extract_namespace_gcc(pretty_func);
47+
return extract_namespace_gcc(pretty_func);
4548
#else
4649
#error "Unsupported compiler"
4750
#endif
4851
}
52+
53+
} // namespace codspeed

core/src/walltime.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#endif
1616
#include <vector>
1717

18+
namespace codspeed {
19+
1820
const double IQR_OUTLIER_FACTOR = 1.5;
1921
const double STDEV_OUTLIER_FACTOR = 3.0;
2022

@@ -243,3 +245,5 @@ void generate_codspeed_walltime_report(
243245

244246
write_codspeed_benchmarks_to_json(codspeed_walltime_benchmarks);
245247
}
248+
249+
} // namespace codspeed

core/test/codspeed.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ TEST(CodSpeedTest, TestSearchAndReplaceBetweenBracketsNamespace) {
88
"examples/google_benchmark/main.cpp::BM_rand_vector";
99
std::string no_brackets_output =
1010
"examples/google_benchmark/main.cpp::BM_rand_vector";
11-
EXPECT_EQ(sanitize_bench_args(no_brackets_input), no_brackets_output);
11+
EXPECT_EQ(codspeed::sanitize_bench_args(no_brackets_input),
12+
no_brackets_output);
1213

1314
std::string brackets_and_no_escaped_type_input =
1415
"examples/google_benchmark/"
1516
"template_bench.hpp::BM_Template1_Capture[two_type_test, int, double]";
1617
std::string brackets_and_no_escaped_type_output =
1718
"examples/google_benchmark/"
1819
"template_bench.hpp::BM_Template1_Capture[two_type_test, int, double]";
19-
EXPECT_EQ(sanitize_bench_args(brackets_and_no_escaped_type_input),
20+
EXPECT_EQ(codspeed::sanitize_bench_args(brackets_and_no_escaped_type_input),
2021
brackets_and_no_escaped_type_output);
2122

2223
std::string brackets_and_escaped_type_input =
@@ -26,7 +27,7 @@ TEST(CodSpeedTest, TestSearchAndReplaceBetweenBracketsNamespace) {
2627
"examples/google_benchmark/"
2728
"template_bench.hpp::test::BM_Template[std\\:\\:string]";
2829

29-
EXPECT_EQ(sanitize_bench_args(brackets_and_escaped_type_input),
30+
EXPECT_EQ(codspeed::sanitize_bench_args(brackets_and_escaped_type_input),
3031
brackets_and_escaped_type_output);
3132

3233
std::string brackets_and_escaped_types_input =
@@ -36,7 +37,7 @@ TEST(CodSpeedTest, TestSearchAndReplaceBetweenBracketsNamespace) {
3637
"examples/google_benchmark/"
3738
"template_bench.hpp::test::BM_Template[std\\:\\:string, std\\:\\:string]";
3839

39-
EXPECT_EQ(sanitize_bench_args(brackets_and_escaped_types_input),
40+
EXPECT_EQ(codspeed::sanitize_bench_args(brackets_and_escaped_types_input),
4041
brackets_and_escaped_types_output);
4142

4243
std::string brackets_and_multiple_types_input =
@@ -46,6 +47,6 @@ TEST(CodSpeedTest, TestSearchAndReplaceBetweenBracketsNamespace) {
4647
"examples/google_benchmark/"
4748
"template_bench.hpp::test::BM_Template[std\\:\\:string, int, double]";
4849

49-
EXPECT_EQ(sanitize_bench_args(brackets_and_multiple_types_input),
50+
EXPECT_EQ(codspeed::sanitize_bench_args(brackets_and_multiple_types_input),
5051
brackets_and_multiple_types_output);
5152
}

core/test/uri.cpp

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,38 @@
1-
#include <gtest/gtest.h>
21
#include "codspeed.h"
2+
#include <gtest/gtest.h>
33

44
// Manual definition (to avoid including it in the public header):
5-
std::string extract_namespace_clang(const std::string& func_str);
6-
std::string extract_namespace_gcc(const std::string& func_str);
5+
namespace codspeed {
6+
std::string extract_namespace_clang(const std::string &func_str);
7+
std::string extract_namespace_gcc(const std::string &func_str);
8+
} // namespace codspeed
79

810
TEST(UriTest, TestExtractNamespaceClang) {
9-
EXPECT_EQ(extract_namespace_clang("auto outer::test12::(anonymous class)::operator()() const"), "outer::test12::");
10-
EXPECT_EQ(extract_namespace_clang("auto outer::(anonymous namespace)::test12::(anonymous class)::operator()() const"), "outer::(anonymous namespace)::test12::");
11+
EXPECT_EQ(codspeed::extract_namespace_clang(
12+
"auto outer::test12::(anonymous class)::operator()() const"),
13+
"outer::test12::");
14+
EXPECT_EQ(codspeed::extract_namespace_clang(
15+
"auto outer::(anonymous namespace)::test12::(anonymous "
16+
"class)::operator()() const"),
17+
"outer::(anonymous namespace)::test12::");
1118
}
1219

1320
TEST(UriTest, TestExtractNamespaceGcc) {
14-
EXPECT_EQ(extract_namespace_gcc("outer::test12::<lambda()>"), "outer::test12::");
15-
EXPECT_EQ(extract_namespace_gcc("outer::(anonymous namespace)::test12::<lambda()>"), "outer::(anonymous namespace)::test12::");
21+
EXPECT_EQ(codspeed::extract_namespace_gcc("outer::test12::<lambda()>"),
22+
"outer::test12::");
23+
EXPECT_EQ(codspeed::extract_namespace_gcc(
24+
"outer::(anonymous namespace)::test12::<lambda()>"),
25+
"outer::(anonymous namespace)::test12::");
1626
}
1727

18-
1928
namespace a {
2029
namespace b {
2130
namespace c {
2231
static std::string pretty_func = ([]() { return __PRETTY_FUNCTION__; })();
2332

2433
TEST(UriTest, TestExtractNamespace) {
25-
EXPECT_EQ(extract_lambda_namespace(pretty_func), "a::b::c::");
26-
}
27-
}
28-
}
34+
EXPECT_EQ(codspeed::extract_lambda_namespace(pretty_func), "a::b::c::");
2935
}
36+
} // namespace c
37+
} // namespace b
38+
} // namespace a

google_benchmark/include/benchmark/benchmark.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,7 @@ class Fixture : public internal::Benchmark {
14461446
#define CUR_FILE \
14471447
std::filesystem::relative(__FILE__, CODSPEED_GIT_ROOT_DIR).string() + "::"
14481448
#define NAMESPACE \
1449-
(([]() { return extract_lambda_namespace(__PRETTY_FUNCTION__); })())
1449+
(([]() { return codspeed::extract_lambda_namespace(__PRETTY_FUNCTION__); })())
14501450
#define STATIC_NAMESPACE_STRING(name) static std::string name = NAMESPACE;
14511451

14521452
#define FILE_AND_NAMESPACE CUR_FILE + NAMESPACE

google_benchmark/src/benchmark.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -352,12 +352,13 @@ void FlushStreams(BenchmarkReporter* reporter) {
352352
#ifdef CODSPEED_WALLTIME
353353
// We use real time by default, but we could offer CPU time usage as a build
354354
// option, open an issue if you need it.
355-
RawWalltimeBenchmark generate_raw_walltime_data(const RunResults& run_results) {
356-
RawWalltimeBenchmark walltime_data;
355+
codspeed::RawWalltimeBenchmark generate_raw_walltime_data(
356+
const RunResults& run_results) {
357+
codspeed::RawWalltimeBenchmark walltime_data;
357358

358359
for (const auto& run : run_results.non_aggregates) {
359360
walltime_data.uri = run.benchmark_name();
360-
walltime_data.uri = sanitize_bench_args(walltime_data.uri);
361+
walltime_data.uri = codspeed::sanitize_bench_args(walltime_data.uri);
361362

362363
size_t pos = walltime_data.uri.rfind("::");
363364

@@ -529,7 +530,7 @@ void RunBenchmarks(const std::vector<BenchmarkInstance>& benchmarks,
529530
}
530531

531532
#ifdef CODSPEED_WALLTIME
532-
std::vector<RawWalltimeBenchmark> codspeed_walltime_data;
533+
std::vector<codspeed::RawWalltimeBenchmark> codspeed_walltime_data;
533534
#endif
534535
for (size_t repetition_index : repetition_indices) {
535536
internal::BenchmarkRunner& runner = runners[repetition_index];
@@ -568,7 +569,7 @@ void RunBenchmarks(const std::vector<BenchmarkInstance>& benchmarks,
568569
Report(display_reporter, file_reporter, run_results);
569570
}
570571
#ifdef CODSPEED_WALLTIME
571-
generate_codspeed_walltime_report(codspeed_walltime_data);
572+
codspeed::generate_codspeed_walltime_report(codspeed_walltime_data);
572573
#endif
573574
}
574575
display_reporter->Finalize();

google_benchmark/src/benchmark_api_internal.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ BenchmarkInstance::BenchmarkInstance(Benchmark* benchmark, int family_idx,
9393

9494
#ifdef CODSPEED_INSTRUMENTATION
9595
State BenchmarkInstance::RunInstrumented(
96-
CodSpeed* codspeed, internal::ThreadTimer* timer,
96+
codspeed::CodSpeed* codspeed, internal::ThreadTimer* timer,
9797
internal::ThreadManager* manager,
9898
internal::PerfCountersMeasurement* perf_counters_measurement,
9999
ProfilerManager* profiler_manager) const {

google_benchmark/src/benchmark_api_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class BenchmarkInstance {
5959

6060
#ifdef CODSPEED_INSTRUMENTATION
6161
State RunInstrumented(
62-
CodSpeed* codspeed, internal::ThreadTimer* timer,
62+
codspeed::CodSpeed* codspeed, internal::ThreadTimer* timer,
6363
internal::ThreadManager* manager,
6464
internal::PerfCountersMeasurement* perf_counters_measurement,
6565
ProfilerManager* profiler_manager) const;

0 commit comments

Comments
 (0)