Skip to content

Commit bd015a4

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

File tree

9 files changed

+41
-24
lines changed

9 files changed

+41
-24
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

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;

google_benchmark/src/benchmark_runner.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,8 @@ void BenchmarkRunner::DoOneRepetition() {
466466
manager.reset(new internal::ThreadManager(b.threads()));
467467
internal::ThreadTimer timer = internal::ThreadTimer::Create();
468468
b.Setup();
469-
State st = b.RunInstrumented(CodSpeed::getInstance(), &timer, manager.get(),
470-
nullptr, nullptr);
469+
State st = b.RunInstrumented(codspeed::CodSpeed::getInstance(), &timer,
470+
manager.get(), nullptr, nullptr);
471471
b.Teardown();
472472

473473
return;

0 commit comments

Comments
 (0)