Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions examples/google_benchmark/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
#include "template_bench.hpp"
#include <benchmark/benchmark.h>
#include <cstring>

template <class... Args>
void BM_Capture(benchmark::State &state, Args &&...args) {
auto args_tuple = std::make_tuple(std::move(args)...);
for (auto _ : state) {
}
}
BENCHMARK_CAPTURE(BM_Capture, int_string_test, 42, std::string("abc"));
BENCHMARK_CAPTURE(BM_Capture, int_test, 42, 43);

// Function to benchmark
static void BM_rand_vector(benchmark::State &state) {
std::vector<int> v;
Expand Down
51 changes: 51 additions & 0 deletions examples/google_benchmark/template_bench.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#pragma once

#include <benchmark/benchmark.h>
#include <cstring>
#include <string>

template <class T> void BM_Template(benchmark::State &state) {
std::vector<T> v;
for (auto _ : state) {
v.push_back(T());
}
}
BENCHMARK_TEMPLATE(BM_Template, int);
BENCHMARK_TEMPLATE(BM_Template, std::string);

//
//

template <typename T> void BM_Template1(benchmark::State &state) {
T val = T();
for (auto _ : state) {
benchmark::DoNotOptimize(val++);
}
}
BENCHMARK_TEMPLATE1(BM_Template1, int);

//
//

template <typename T, typename U> void BM_Template2(benchmark::State &state) {
T t = T();
U u = U();
for (auto _ : state) {
benchmark::DoNotOptimize(t + u);
}
}
BENCHMARK_TEMPLATE2(BM_Template2, int, double);

//
//

template <typename T, class... ExtraArgs>
void BM_Template1_Capture(benchmark::State &state, ExtraArgs &&...extra_args) {
auto args_tuple = std::make_tuple(std::move(extra_args)...);
for (auto _ : state) {
}
}
BENCHMARK_TEMPLATE1_CAPTURE(BM_Template1_Capture, void, int_string_test, 42,
std::string("abc"));
BENCHMARK_TEMPLATE2_CAPTURE(BM_Template1_Capture, int, double, two_type_test,
42, std::string("abc"));
68 changes: 54 additions & 14 deletions google_benchmark/include/benchmark/benchmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -1440,21 +1440,36 @@ class Fixture : public internal::Benchmark {

#ifdef CODSPEED_ENABLED
#include <filesystem>
#define BENCHMARK(...) \
BENCHMARK_PRIVATE_DECLARE(_benchmark_) = \
(::benchmark::internal::RegisterBenchmarkInternal( \
std::make_unique<::benchmark::internal::FunctionBenchmark>( \
std::filesystem::relative(__FILE__, CODSPEED_GIT_ROOT_DIR) \
.string() + \
"::" + #__VA_ARGS__, \
__VA_ARGS__)))
#define CUR_FILE \
std::filesystem::relative(__FILE__, CODSPEED_GIT_ROOT_DIR).string() + "::"

// Transforms `BM_Foo<int, double>` into `BM_Foo[int, double]`.
#define TYPE_START "["
#define TYPE_END "]"

// Transforms `BM_Foo/int_arg` into `BM_Foo[int_arg]`.
#define NAME_START "["
#define NAME_END "]"

// Extra space after the comma for readability
#define COMMA ", "
#else
#define CUR_FILE std::string()

#define TYPE_START "<"
#define TYPE_END ">"

#define NAME_START "/"
#define NAME_END ""

#define COMMA ","
#endif

#define BENCHMARK(...) \
BENCHMARK_PRIVATE_DECLARE(_benchmark_) = \
(::benchmark::internal::RegisterBenchmarkInternal( \
std::make_unique<::benchmark::internal::FunctionBenchmark>( \
#__VA_ARGS__, __VA_ARGS__)))
#endif
CUR_FILE + #__VA_ARGS__, __VA_ARGS__)))

// Old-style macros
#define BENCHMARK_WITH_ARG(n, a) BENCHMARK(n)->Arg((a))
Expand All @@ -1479,7 +1494,7 @@ class Fixture : public internal::Benchmark {
BENCHMARK_PRIVATE_DECLARE(_benchmark_) = \
(::benchmark::internal::RegisterBenchmarkInternal( \
std::make_unique<::benchmark::internal::FunctionBenchmark>( \
#func "/" #test_case_name, \
CUR_FILE + #func NAME_START #test_case_name NAME_END, \
[](::benchmark::State& st) { func(st, __VA_ARGS__); })))

// This will register a benchmark for a templatized function. For example:
Expand All @@ -1494,19 +1509,20 @@ class Fixture : public internal::Benchmark {
BENCHMARK_PRIVATE_DECLARE(n) = \
(::benchmark::internal::RegisterBenchmarkInternal( \
std::make_unique<::benchmark::internal::FunctionBenchmark>( \
#n "<" #a ">", n<a>)))
CUR_FILE + #n TYPE_START #a TYPE_END, n<a>)))

#define BENCHMARK_TEMPLATE2(n, a, b) \
BENCHMARK_PRIVATE_DECLARE(n) = \
(::benchmark::internal::RegisterBenchmarkInternal( \
std::make_unique<::benchmark::internal::FunctionBenchmark>( \
#n "<" #a "," #b ">", n<a, b>)))
CUR_FILE + #n TYPE_START #a COMMA #b TYPE_END, n<a, b>)))

#define BENCHMARK_TEMPLATE(n, ...) \
BENCHMARK_PRIVATE_DECLARE(n) = \
(::benchmark::internal::RegisterBenchmarkInternal( \
std::make_unique<::benchmark::internal::FunctionBenchmark>( \
#n "<" #__VA_ARGS__ ">", n<__VA_ARGS__>)))
CUR_FILE + #n TYPE_START #__VA_ARGS__ TYPE_END, \
n<__VA_ARGS__>)))

// This will register a benchmark for a templatized function,
// with the additional arguments specified by `...`.
Expand All @@ -1520,16 +1536,40 @@ class Fixture : public internal::Benchmark {
// /* Registers a benchmark named "BM_takes_args<void>/int_string_test` */
// BENCHMARK_TEMPLATE1_CAPTURE(BM_takes_args, void, int_string_test, 42,
// std::string("abc"));
#ifdef CODSPEED_ENABLED

// BM_Template1_Capture<void>[int_string_test] will be turned into
// BM_Template1_Capture[int_string_test]
#define BENCHMARK_CAPTURE_WITH_NAME(func, func_name, test_case_name, ...) \
BENCHMARK_PRIVATE_DECLARE(_benchmark_) = \
(::benchmark::internal::RegisterBenchmarkInternal( \
std::make_unique<::benchmark::internal::FunctionBenchmark>( \
CUR_FILE + #func_name NAME_START #test_case_name NAME_END, \
[](::benchmark::State& st) { func(st, __VA_ARGS__); })))

#define BENCHMARK_TEMPLATE1_CAPTURE(func, a, test_case_name, ...) \
BENCHMARK_CAPTURE_WITH_NAME(func<a>, func, test_case_name, __VA_ARGS__)
#else
#define BENCHMARK_TEMPLATE1_CAPTURE(func, a, test_case_name, ...) \
BENCHMARK_CAPTURE(func<a>, test_case_name, __VA_ARGS__)
#endif

#ifdef CODSPEED_ENABLED
#define BENCHMARK_TEMPLATE2_CAPTURE(func, a, b, test_case_name, ...) \
BENCHMARK_PRIVATE_DECLARE(func) = \
(::benchmark::internal::RegisterBenchmarkInternal( \
std::make_unique<::benchmark::internal::FunctionBenchmark>( \
CUR_FILE + #func "[" #test_case_name COMMA #a COMMA #b "]", \
[](::benchmark::State& st) { func<a, b>(st, __VA_ARGS__); })))
#else
#define BENCHMARK_TEMPLATE2_CAPTURE(func, a, b, test_case_name, ...) \
BENCHMARK_PRIVATE_DECLARE(func) = \
(::benchmark::internal::RegisterBenchmarkInternal( \
std::make_unique<::benchmark::internal::FunctionBenchmark>( \
#func "<" #a "," #b ">" \
"/" #test_case_name, \
[](::benchmark::State& st) { func<a, b>(st, __VA_ARGS__); })))
#endif

#define BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \
class BaseClass##_##Method##_Benchmark : public BaseClass { \
Expand Down