benchmark/test/register_benchmark_test.cc
Go to the documentation of this file.
1 
2 #undef NDEBUG
3 #include <cassert>
4 #include <vector>
5 
6 #include "../src/check.h" // NOTE: check.h is for internal use only!
7 #include "benchmark/benchmark.h"
8 
9 namespace {
10 
11 class TestReporter : public benchmark::ConsoleReporter {
12  public:
13  virtual void ReportRuns(const std::vector<Run>& report) BENCHMARK_OVERRIDE {
14  all_runs_.insert(all_runs_.end(), begin(report), end(report));
16  }
17 
18  std::vector<Run> all_runs_;
19 };
20 
21 struct TestCase {
23  const char* label;
24  // Note: not explicit as we rely on it being converted through ADD_CASES.
25  TestCase(const char* xname) : TestCase(xname, nullptr) {}
26  TestCase(const char* xname, const char* xlabel)
27  : name(xname), label(xlabel) {}
28 
30 
31  void CheckRun(Run const& run) const {
32  // clang-format off
33  BM_CHECK(name == run.benchmark_name()) << "expected " << name << " got "
34  << run.benchmark_name();
35  if (label) {
36  BM_CHECK(run.report_label == label) << "expected " << label << " got "
37  << run.report_label;
38  } else {
39  BM_CHECK(run.report_label == "");
40  }
41  // clang-format on
42  }
43 };
44 
45 std::vector<TestCase> ExpectedResults;
46 
47 int AddCases(std::initializer_list<TestCase> const& v) {
48  for (auto N : v) {
49  ExpectedResults.push_back(N);
50  }
51  return 0;
52 }
53 
54 #define CONCAT(x, y) CONCAT2(x, y)
55 #define CONCAT2(x, y) x##y
56 #define ADD_CASES(...) int CONCAT(dummy, __LINE__) = AddCases({__VA_ARGS__})
57 
58 } // end namespace
59 
61 
62 //----------------------------------------------------------------------------//
63 // Test RegisterBenchmark with no additional arguments
64 //----------------------------------------------------------------------------//
66  for (auto _ : state) {
67  }
68 }
71  "BM_function_manual_registration", BM_function);
72 ADD_CASES({"BM_function"}, {"BM_function_manual_registration"});
73 
74 //----------------------------------------------------------------------------//
75 // Test RegisterBenchmark with additional arguments
76 // Note: GCC <= 4.8 do not support this form of RegisterBenchmark because they
77 // reject the variadic pack expansion of lambda captures.
78 //----------------------------------------------------------------------------//
79 #ifndef BENCHMARK_HAS_NO_VARIADIC_REGISTER_BENCHMARK
80 
81 void BM_extra_args(benchmark::State& st, const char* label) {
82  for (auto _ : st) {
83  }
84  st.SetLabel(label);
85 }
87  std::pair<const char*, const char*> cases[] = {
88  {"test1", "One"}, {"test2", "Two"}, {"test3", "Three"}};
89  for (auto const& c : cases)
91  return 0;
92 }
94 ADD_CASES({"test1", "One"}, {"test2", "Two"}, {"test3", "Three"});
95 
96 #endif // BENCHMARK_HAS_NO_VARIADIC_REGISTER_BENCHMARK
97 
98 //----------------------------------------------------------------------------//
99 // Test RegisterBenchmark with different callable types
100 //----------------------------------------------------------------------------//
101 
104  for (auto _ : st) {
105  }
106  }
107 };
108 
110 #ifdef BENCHMARK_HAS_CXX11
111  {
112  CustomFixture fx;
113  benchmark::RegisterBenchmark("custom_fixture", fx);
114  AddCases({"custom_fixture"});
115  }
116 #endif
117 #ifndef BENCHMARK_HAS_NO_VARIADIC_REGISTER_BENCHMARK
118  {
119  const char* x = "42";
120  auto capturing_lam = [=](benchmark::State& st) {
121  for (auto _ : st) {
122  }
123  st.SetLabel(x);
124  };
125  benchmark::RegisterBenchmark("lambda_benchmark", capturing_lam);
126  AddCases({{"lambda_benchmark", x}});
127  }
128 #endif
129 }
130 
131 // Test that all benchmarks, registered at either during static init or runtime,
132 // are run and the results are passed to the reported.
133 void RunTestOne() {
135 
136  TestReporter test_reporter;
137  benchmark::RunSpecifiedBenchmarks(&test_reporter);
138 
140  auto EB = ExpectedResults.begin();
141 
142  for (Run const& run : test_reporter.all_runs_) {
143  assert(EB != ExpectedResults.end());
144  EB->CheckRun(run);
145  ++EB;
146  }
147  assert(EB == ExpectedResults.end());
148 }
149 
150 // Test that ClearRegisteredBenchmarks() clears all previously registered
151 // benchmarks.
152 // Also test that new benchmarks can be registered and ran afterwards.
153 void RunTestTwo() {
154  assert(ExpectedResults.size() != 0 &&
155  "must have at least one registered benchmark");
156  ExpectedResults.clear();
158 
159  TestReporter test_reporter;
160  size_t num_ran = benchmark::RunSpecifiedBenchmarks(&test_reporter);
161  assert(num_ran == 0);
162  assert(test_reporter.all_runs_.begin() == test_reporter.all_runs_.end());
163 
165  num_ran = benchmark::RunSpecifiedBenchmarks(&test_reporter);
166  assert(num_ran == ExpectedResults.size());
167 
169  auto EB = ExpectedResults.begin();
170 
171  for (Run const& run : test_reporter.all_runs_) {
172  assert(EB != ExpectedResults.end());
173  EB->CheckRun(run);
174  ++EB;
175  }
176  assert(EB == ExpectedResults.end());
177 }
178 
179 int main(int argc, char* argv[]) {
180  benchmark::Initialize(&argc, argv);
181 
182  RunTestOne();
183  RunTestTwo();
184 }
begin
char * begin
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1007
TestCase::run
void run()
Definition: pgv_test.cc:32
BM_CHECK
#define BM_CHECK(b)
Definition: benchmark/src/check.h:58
RunTestOne
void RunTestOne()
Definition: benchmark/test/register_benchmark_test.cc:133
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
setup.name
name
Definition: setup.py:542
TestCase::TestCase
TestCase(std::string re, int rule=MR_Default)
Definition: benchmark/test/output_test_helper.cc:348
benchmark::ConsoleReporter::ReportRuns
virtual void ReportRuns(const std::vector< Run > &reports) BENCHMARK_OVERRIDE
Definition: benchmark/src/console_reporter.cc:71
benchmark::RegisterBenchmark
internal::Benchmark * RegisterBenchmark(const char *name, internal::Function *fn)
Definition: benchmark/include/benchmark/benchmark.h:1093
benchmark::RunSpecifiedBenchmarks
size_t RunSpecifiedBenchmarks()
Definition: benchmark/src/benchmark.cc:437
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
end
char * end
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1008
gen_synthetic_protos.label
label
Definition: gen_synthetic_protos.py:102
RunTestTwo
void RunTestTwo()
Definition: benchmark/test/register_benchmark_test.cc:153
BENCHMARK_OVERRIDE
#define BENCHMARK_OVERRIDE
Definition: benchmark/include/benchmark/benchmark.h:271
setup.v
v
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
gmock_output_test._
_
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
ReturnVal
benchmark::internal::Benchmark * ReturnVal
Definition: benchmark/test/register_benchmark_test.cc:60
benchmark::ClearRegisteredBenchmarks
void ClearRegisteredBenchmarks()
Definition: benchmark/src/benchmark_register.cc:459
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
benchmark::Initialize
void Initialize(int *argc, char **argv)
Definition: benchmark/src/benchmark.cc:602
TestCase
Definition: benchmark/test/output_test.h:31
benchmark::ConsoleReporter
Definition: benchmark/include/benchmark/benchmark.h:1571
benchmark::BenchmarkReporter::Run
Definition: benchmark/include/benchmark/benchmark.h:1423
AddCases
int AddCases(TestCaseID ID, std::initializer_list< TestCase > il)
Definition: benchmark/test/output_test_helper.cc:361
ADD_CASES
#define ADD_CASES(...)
Definition: benchmark/test/register_benchmark_test.cc:56
dummy
ReturnVal dummy
Definition: benchmark/test/register_benchmark_test.cc:70
benchmark::internal::Benchmark
Definition: benchmark/include/benchmark/benchmark.h:834
CustomFixture
Definition: benchmark/test/register_benchmark_test.cc:102
main
int main(int argc, char *argv[])
Definition: benchmark/test/register_benchmark_test.cc:179
client.run
def run()
Definition: examples/python/async_streaming/client.py:109
N
#define N
Definition: sync_test.cc:37
RegisterFromFunction
int RegisterFromFunction()
Definition: benchmark/test/register_benchmark_test.cc:86
TestRegistrationAtRuntime
void TestRegistrationAtRuntime()
Definition: benchmark/test/register_benchmark_test.cc:109
benchmark::State
Definition: benchmark/include/benchmark/benchmark.h:503
BM_function
void BM_function(benchmark::State &state)
Definition: benchmark/test/register_benchmark_test.cc:65
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
CustomFixture::operator()
void operator()(benchmark::State &st)
Definition: benchmark/test/register_benchmark_test.cc:103
BENCHMARK
BENCHMARK(BM_function)
googletest-break-on-failure-unittest.Run
def Run(command)
Definition: bloaty/third_party/googletest/googletest/test/googletest-break-on-failure-unittest.py:76
dummy2
int dummy2
Definition: benchmark/test/register_benchmark_test.cc:93
BM_extra_args
void BM_extra_args(benchmark::State &st, const char *label)
Definition: benchmark/test/register_benchmark_test.cc:81


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:02