benchmark/test/benchmark_test.cc
Go to the documentation of this file.
1 #include "benchmark/benchmark.h"
2 
3 #include <assert.h>
4 #include <math.h>
5 #include <stdint.h>
6 
7 #include <chrono>
8 #include <cstdlib>
9 #include <iostream>
10 #include <limits>
11 #include <list>
12 #include <map>
13 #include <mutex>
14 #include <set>
15 #include <sstream>
16 #include <string>
17 #include <thread>
18 #include <utility>
19 #include <vector>
20 
21 #if defined(__GNUC__)
22 #define BENCHMARK_NOINLINE __attribute__((noinline))
23 #else
24 #define BENCHMARK_NOINLINE
25 #endif
26 
27 namespace {
28 
30  return (n == 1) ? 1 : n * Factorial(n - 1);
31 }
32 
33 double CalculatePi(int depth) {
34  double pi = 0.0;
35  for (int i = 0; i < depth; ++i) {
36  double numerator = static_cast<double>(((i % 2) * 2) - 1);
37  double denominator = static_cast<double>((2 * i) - 1);
38  pi += numerator / denominator;
39  }
40  return (pi - 1.0) * 4;
41 }
42 
43 std::set<int64_t> ConstructRandomSet(int64_t size) {
44  std::set<int64_t> s;
45  for (int i = 0; i < size; ++i) s.insert(s.end(), i);
46  return s;
47 }
48 
49 std::mutex test_vector_mu;
50 std::vector<int>* test_vector = nullptr;
51 
52 } // end namespace
53 
55  int fac_42 = 0;
56  for (auto _ : state) fac_42 = Factorial(8);
57  // Prevent compiler optimizations
58  std::stringstream ss;
59  ss << fac_42;
60  state.SetLabel(ss.str());
61 }
63 BENCHMARK(BM_Factorial)->UseRealTime();
64 
66  double pi = 0.0;
67  for (auto _ : state) pi = CalculatePi(static_cast<int>(state.range(0)));
68  std::stringstream ss;
69  ss << pi;
70  state.SetLabel(ss.str());
71 }
72 BENCHMARK_RANGE(BM_CalculatePiRange, 1, 1024 * 1024);
73 
75  static const int depth = 1024;
76  for (auto _ : state) {
77  benchmark::DoNotOptimize(CalculatePi(static_cast<int>(depth)));
78  }
79 }
80 BENCHMARK(BM_CalculatePi)->Threads(8);
81 BENCHMARK(BM_CalculatePi)->ThreadRange(1, 32);
82 BENCHMARK(BM_CalculatePi)->ThreadPerCpu();
83 
85  std::set<int64_t> data;
86  for (auto _ : state) {
87  state.PauseTiming();
88  data = ConstructRandomSet(state.range(0));
89  state.ResumeTiming();
90  for (int j = 0; j < state.range(1); ++j) data.insert(rand());
91  }
92  state.SetItemsProcessed(state.iterations() * state.range(1));
93  state.SetBytesProcessed(state.iterations() * state.range(1) * sizeof(int));
94 }
95 
96 // Test many inserts at once to reduce the total iterations needed. Otherwise, the slower,
97 // non-timed part of each iteration will make the benchmark take forever.
98 BENCHMARK(BM_SetInsert)->Ranges({{1 << 10, 8 << 10}, {128, 512}});
99 
100 template <typename Container,
101  typename ValueType = typename Container::value_type>
103  ValueType v = 42;
104  for (auto _ : state) {
105  Container c;
106  for (int64_t i = state.range(0); --i;) c.push_back(v);
107  }
108  const int64_t items_processed = state.iterations() * state.range(0);
109  state.SetItemsProcessed(items_processed);
110  state.SetBytesProcessed(items_processed * sizeof(v));
111 }
112 BENCHMARK_TEMPLATE2(BM_Sequential, std::vector<int>, int)
113  ->Range(1 << 0, 1 << 10);
114 BENCHMARK_TEMPLATE(BM_Sequential, std::list<int>)->Range(1 << 0, 1 << 10);
115 // Test the variadic version of BENCHMARK_TEMPLATE in C++11 and beyond.
116 #ifdef BENCHMARK_HAS_CXX11
117 BENCHMARK_TEMPLATE(BM_Sequential, std::vector<int>, int)->Arg(512);
118 #endif
119 
121  size_t len = static_cast<size_t>(state.range(0));
122  std::string s1(len, '-');
123  std::string s2(len, '-');
124  for (auto _ : state) benchmark::DoNotOptimize(s1.compare(s2));
125 }
126 BENCHMARK(BM_StringCompare)->Range(1, 1 << 20);
127 
129  if (state.thread_index() == 0) {
130  // No need to lock test_vector_mu here as this is running single-threaded.
131  test_vector = new std::vector<int>();
132  }
133  int i = 0;
134  for (auto _ : state) {
135  std::lock_guard<std::mutex> l(test_vector_mu);
136  if (i % 2 == 0)
137  test_vector->push_back(i);
138  else
139  test_vector->pop_back();
140  ++i;
141  }
142  if (state.thread_index() == 0) {
143  delete test_vector;
144  }
145 }
146 BENCHMARK(BM_SetupTeardown)->ThreadPerCpu();
147 
149  double tracker = 0.0;
150  for (auto _ : state) {
151  for (int i = 0; i < state.range(0); ++i)
153  }
154 }
155 BENCHMARK(BM_LongTest)->Range(1 << 16, 1 << 28);
156 
158  int64_t size = state.range(0) / static_cast<int64_t>(sizeof(int));
159  int thread_size = static_cast<int>(size) / state.threads();
160  int from = thread_size * state.thread_index();
161  int to = from + thread_size;
162 
163  if (state.thread_index() == 0) {
164  test_vector = new std::vector<int>(static_cast<size_t>(size));
165  }
166 
167  for (auto _ : state) {
168  for (int i = from; i < to; i++) {
169  // No need to lock test_vector_mu as ranges
170  // do not overlap between threads.
172  }
173  }
174 
175  if (state.thread_index() == 0) {
176  delete test_vector;
177  }
178 }
179 BENCHMARK(BM_ParallelMemset)->Arg(10 << 20)->ThreadRange(1, 4);
180 
182  int64_t slept_for = 0;
183  int64_t microseconds = state.range(0);
184  std::chrono::duration<double, std::micro> sleep_duration{
185  static_cast<double>(microseconds)};
186 
187  for (auto _ : state) {
189  // Simulate some useful workload with a sleep
190  std::this_thread::sleep_for(
191  std::chrono::duration_cast<std::chrono::nanoseconds>(sleep_duration));
193 
194  auto elapsed =
195  std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
196 
197  state.SetIterationTime(elapsed.count());
198  slept_for += microseconds;
199  }
200  state.SetItemsProcessed(slept_for);
201 }
202 BENCHMARK(BM_ManualTiming)->Range(1, 1 << 14)->UseRealTime();
203 BENCHMARK(BM_ManualTiming)->Range(1, 1 << 14)->UseManualTime();
204 
205 #ifdef BENCHMARK_HAS_CXX11
206 
207 template <class... Args>
208 void BM_with_args(benchmark::State& state, Args&&...) {
209  for (auto _ : state) {
210  }
211 }
212 BENCHMARK_CAPTURE(BM_with_args, int_test, 42, 43, 44);
213 BENCHMARK_CAPTURE(BM_with_args, string_and_pair_test, std::string("abc"),
214  std::pair<int, double>(42, 3.8));
215 
216 void BM_non_template_args(benchmark::State& state, int, double) {
217  while(state.KeepRunning()) {}
218 }
219 BENCHMARK_CAPTURE(BM_non_template_args, basic_test, 0, 0);
220 
221 #endif // BENCHMARK_HAS_CXX11
222 
224  switch (st.range(0)) {
225  case 1:
226  assert(st.threads() == 1 || st.threads() == 2 || st.threads() == 3);
227  break;
228  case 2:
229  assert(st.threads() == 1 || st.threads() == 3 || st.threads() == 4);
230  break;
231  case 3:
232  assert(st.threads() == 5 || st.threads() == 8 || st.threads() == 11 ||
233  st.threads() == 14);
234  break;
235  default:
236  assert(false && "Invalid test case number");
237  }
238  while (st.KeepRunning()) {
239  }
240 }
241 BENCHMARK(BM_DenseThreadRanges)->Arg(1)->DenseThreadRange(1, 3);
242 BENCHMARK(BM_DenseThreadRanges)->Arg(2)->DenseThreadRange(1, 4, 2);
243 BENCHMARK(BM_DenseThreadRanges)->Arg(3)->DenseThreadRange(5, 14, 3);
244 
now
static double now(void)
Definition: test/core/fling/client.cc:130
BM_CalculatePiRange
static void BM_CalculatePiRange(benchmark::State &state)
Definition: benchmark/test/benchmark_test.cc:65
BM_Sequential
static void BM_Sequential(benchmark::State &state)
Definition: benchmark/test/benchmark_test.cc:102
BM_StringCompare
static void BM_StringCompare(benchmark::State &state)
Definition: benchmark/test/benchmark_test.cc:120
mutex
static uv_mutex_t mutex
Definition: threadpool.c:34
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
benchmark::DoNotOptimize
BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const &value)
Definition: benchmark/include/benchmark/benchmark.h:375
to
size_t to
Definition: abseil-cpp/absl/container/internal/layout_test.cc:1385
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
from
size_t from
Definition: abseil-cpp/absl/container/internal/layout_test.cc:1384
start
static uint64_t start
Definition: benchmark-pound.c:74
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
BM_LongTest
static void BM_LongTest(benchmark::State &state)
Definition: benchmark/test/benchmark_test.cc:148
xds_interop_client.int
int
Definition: xds_interop_client.py:113
end
char * end
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1008
hpack_encoder_fixtures::Args
Args({0, 16384})
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
BENCHMARK_MAIN
BENCHMARK_MAIN()
test_vector
static void test_vector(const char *raw, size_t raw_length, const char *encoded, size_t encoded_length, grpc_core::PercentEncodingType type)
Definition: percent_encoding_test.cc:37
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
BENCHMARK_RANGE
BENCHMARK_RANGE(BM_CalculatePiRange, 1, 1024 *1024)
absl::compare_internal::value_type
int8_t value_type
Definition: abseil-cpp/absl/types/compare.h:45
BM_Factorial
static void BM_Factorial(benchmark::State &state)
Definition: benchmark/test/benchmark_test.cc:54
BENCHMARK_TEMPLATE2
#define BENCHMARK_TEMPLATE2(n, a, b)
Definition: benchmark/include/benchmark/benchmark.h:1218
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
BM_CalculatePi
static void BM_CalculatePi(benchmark::State &state)
Definition: benchmark/test/benchmark_test.cc:74
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
Json::ValueType
ValueType
Type of the value held by a Value object.
Definition: third_party/bloaty/third_party/protobuf/conformance/third_party/jsoncpp/json.h:463
stdint.h
BM_DenseThreadRanges
static void BM_DenseThreadRanges(benchmark::State &st)
Definition: benchmark/test/benchmark_test.cc:223
Factorial
int Factorial(int n)
Definition: bloaty/third_party/googletest/googletest/samples/sample1.cc:35
BM_SetInsert
static void BM_SetInsert(benchmark::State &state)
Definition: benchmark/test/benchmark_test.cc:84
benchmark::State
Definition: benchmark/include/benchmark/benchmark.h:503
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
BM_SetupTeardown
static void BM_SetupTeardown(benchmark::State &state)
Definition: benchmark/test/benchmark_test.cc:128
benchmark::State::threads
const int threads
Definition: bloaty/third_party/protobuf/third_party/benchmark/include/benchmark/benchmark.h:557
BM_ManualTiming
static void BM_ManualTiming(benchmark::State &state)
Definition: benchmark/test/benchmark_test.cc:181
tracker
SessionTracker * tracker
Definition: ssl_session_cache_test.cc:38
BENCHMARK_NOINLINE
#define BENCHMARK_NOINLINE
Definition: benchmark/test/benchmark_test.cc:24
BENCHMARK_TEMPLATE
BENCHMARK_TEMPLATE(BM_Sequential, std::list< int >) -> Range(1<< 0, 1<< 10)
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
BM_ParallelMemset
static void BM_ParallelMemset(benchmark::State &state)
Definition: benchmark/test/benchmark_test.cc:157
run_grpclb_interop_tests.l
dictionary l
Definition: run_grpclb_interop_tests.py:410
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
mkowners.depth
depth
Definition: mkowners.py:114
BENCHMARK
BENCHMARK(BM_Factorial)
benchmark::State::KeepRunning
bool KeepRunning()
Definition: benchmark/include/benchmark/benchmark.h:740
benchmark::State::range
BENCHMARK_ALWAYS_INLINE int64_t range(std::size_t pos=0) const
Definition: benchmark/include/benchmark/benchmark.h:663
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:46