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 
29 int BENCHMARK_NOINLINE Factorial(uint32_t n) {
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<int> ConstructRandomSet(int size) {
44  std::set<int> s;
45  for (int i = 0; i < size; ++i) s.insert(i);
46  return s;
47 }
48 
49 std::mutex test_vector_mu;
50 std::vector<int>* test_vector = nullptr;
51 
52 } // end namespace
53 
54 static void BM_Factorial(benchmark::State& state) {
55  int fac_42 = 0;
56  while (state.KeepRunning()) 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  while (state.KeepRunning()) pi = CalculatePi(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 
74 static void BM_CalculatePi(benchmark::State& state) {
75  static const int depth = 1024;
76  while (state.KeepRunning()) {
77  benchmark::DoNotOptimize(CalculatePi(depth));
78  }
79 }
80 BENCHMARK(BM_CalculatePi)->Threads(8);
81 BENCHMARK(BM_CalculatePi)->ThreadRange(1, 32);
82 BENCHMARK(BM_CalculatePi)->ThreadPerCpu();
83 
84 static void BM_SetInsert(benchmark::State& state) {
85  while (state.KeepRunning()) {
86  state.PauseTiming();
87  std::set<int> data = ConstructRandomSet(state.range(0));
88  state.ResumeTiming();
89  for (int j = 0; j < state.range(1); ++j) data.insert(rand());
90  }
91  state.SetItemsProcessed(state.iterations() * state.range(1));
92  state.SetBytesProcessed(state.iterations() * state.range(1) * sizeof(int));
93 }
94 BENCHMARK(BM_SetInsert)->Ranges({{1 << 10, 8 << 10}, {1, 10}});
95 
96 template <typename Container,
97  typename ValueType = typename Container::value_type>
98 static void BM_Sequential(benchmark::State& state) {
99  ValueType v = 42;
100  while (state.KeepRunning()) {
101  Container c;
102  for (int i = state.range(0); --i;) c.push_back(v);
103  }
104  const size_t items_processed = state.iterations() * state.range(0);
105  state.SetItemsProcessed(items_processed);
106  state.SetBytesProcessed(items_processed * sizeof(v));
107 }
108 BENCHMARK_TEMPLATE2(BM_Sequential, std::vector<int>, int)
109  ->Range(1 << 0, 1 << 10);
110 BENCHMARK_TEMPLATE(BM_Sequential, std::list<int>)->Range(1 << 0, 1 << 10);
111 // Test the variadic version of BENCHMARK_TEMPLATE in C++11 and beyond.
112 #if __cplusplus >= 201103L
113 BENCHMARK_TEMPLATE(BM_Sequential, std::vector<int>, int)->Arg(512);
114 #endif
115 
116 static void BM_StringCompare(benchmark::State& state) {
117  std::string s1(state.range(0), '-');
118  std::string s2(state.range(0), '-');
119  while (state.KeepRunning()) benchmark::DoNotOptimize(s1.compare(s2));
120 }
121 BENCHMARK(BM_StringCompare)->Range(1, 1 << 20);
122 
123 static void BM_SetupTeardown(benchmark::State& state) {
124  if (state.thread_index == 0) {
125  // No need to lock test_vector_mu here as this is running single-threaded.
126  test_vector = new std::vector<int>();
127  }
128  int i = 0;
129  while (state.KeepRunning()) {
130  std::lock_guard<std::mutex> l(test_vector_mu);
131  if (i % 2 == 0)
132  test_vector->push_back(i);
133  else
134  test_vector->pop_back();
135  ++i;
136  }
137  if (state.thread_index == 0) {
138  delete test_vector;
139  }
140 }
141 BENCHMARK(BM_SetupTeardown)->ThreadPerCpu();
142 
143 static void BM_LongTest(benchmark::State& state) {
144  double tracker = 0.0;
145  while (state.KeepRunning()) {
146  for (int i = 0; i < state.range(0); ++i)
147  benchmark::DoNotOptimize(tracker += i);
148  }
149 }
150 BENCHMARK(BM_LongTest)->Range(1 << 16, 1 << 28);
151 
152 static void BM_ParallelMemset(benchmark::State& state) {
153  int size = state.range(0) / static_cast<int>(sizeof(int));
154  int thread_size = size / state.threads;
155  int from = thread_size * state.thread_index;
156  int to = from + thread_size;
157 
158  if (state.thread_index == 0) {
159  test_vector = new std::vector<int>(size);
160  }
161 
162  while (state.KeepRunning()) {
163  for (int i = from; i < to; i++) {
164  // No need to lock test_vector_mu as ranges
165  // do not overlap between threads.
166  benchmark::DoNotOptimize(test_vector->at(i) = 1);
167  }
168  }
169 
170  if (state.thread_index == 0) {
171  delete test_vector;
172  }
173 }
174 BENCHMARK(BM_ParallelMemset)->Arg(10 << 20)->ThreadRange(1, 4);
175 
176 static void BM_ManualTiming(benchmark::State& state) {
177  size_t slept_for = 0;
178  int microseconds = state.range(0);
179  std::chrono::duration<double, std::micro> sleep_duration{
180  static_cast<double>(microseconds)};
181 
182  while (state.KeepRunning()) {
183  auto start = std::chrono::high_resolution_clock::now();
184  // Simulate some useful workload with a sleep
185  std::this_thread::sleep_for(
186  std::chrono::duration_cast<std::chrono::nanoseconds>(sleep_duration));
187  auto end = std::chrono::high_resolution_clock::now();
188 
189  auto elapsed =
190  std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
191 
192  state.SetIterationTime(elapsed.count());
193  slept_for += microseconds;
194  }
195  state.SetItemsProcessed(slept_for);
196 }
197 BENCHMARK(BM_ManualTiming)->Range(1, 1 << 14)->UseRealTime();
198 BENCHMARK(BM_ManualTiming)->Range(1, 1 << 14)->UseManualTime();
199 
200 #if __cplusplus >= 201103L
201 
202 template <class... Args>
203 void BM_with_args(benchmark::State& state, Args&&...) {
204  while (state.KeepRunning()) {
205  }
206 }
207 BENCHMARK_CAPTURE(BM_with_args, int_test, 42, 43, 44);
208 BENCHMARK_CAPTURE(BM_with_args, string_and_pair_test, std::string("abc"),
209  std::pair<int, double>(42, 3.8));
210 
211 void BM_non_template_args(benchmark::State& state, int, double) {
212  while(state.KeepRunning()) {}
213 }
214 BENCHMARK_CAPTURE(BM_non_template_args, basic_test, 0, 0);
215 
216 #endif // __cplusplus >= 201103L
217 
219  switch (st.range(0)) {
220  case 1:
221  assert(st.threads == 1 || st.threads == 2 || st.threads == 3);
222  break;
223  case 2:
224  assert(st.threads == 1 || st.threads == 3 || st.threads == 4);
225  break;
226  case 3:
227  assert(st.threads == 5 || st.threads == 8 || st.threads == 11 ||
228  st.threads == 14);
229  break;
230  default:
231  assert(false && "Invalid test case number");
232  }
233  while (st.KeepRunning()) {
234  }
235 }
236 BENCHMARK(BM_DenseThreadRanges)->Arg(1)->DenseThreadRange(1, 3);
237 BENCHMARK(BM_DenseThreadRanges)->Arg(2)->DenseThreadRange(1, 4, 2);
238 BENCHMARK(BM_DenseThreadRanges)->Arg(3)->DenseThreadRange(5, 14, 3);
239 
BENCHMARK_TEMPLATE2
BENCHMARK_TEMPLATE2(BM_template2, int, long)
benchmark::State::SetLabel
void SetLabel(const char *label)
Definition: benchmark.cc:449
end
GLuint GLuint end
Definition: glcorearb.h:2858
BM_ParallelMemset
static void BM_ParallelMemset(benchmark::State &state)
Definition: benchmark_test.cc:152
s
XmlRpcServer s
Factorial
int Factorial(int n)
Definition: sample1.cc:35
benchmark::DoNotOptimize
BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const &value)
Definition: benchmark.h:328
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
BM_Factorial
static void BM_Factorial(benchmark::State &state)
Definition: benchmark_test.cc:54
benchmark::State::PauseTiming
void PauseTiming()
Definition: benchmark.cc:420
BENCHMARK_RANGE
BENCHMARK_RANGE(BM_CalculatePiRange, 1, 1024 *1024)
BM_CalculatePiRange
static void BM_CalculatePiRange(benchmark::State &state)
Definition: benchmark_test.cc:65
BM_CalculatePi
static void BM_CalculatePi(benchmark::State &state)
Definition: benchmark_test.cc:74
BENCHMARK_NOINLINE
#define BENCHMARK_NOINLINE
Definition: benchmark_test.cc:24
BENCHMARK_MAIN
#define BENCHMARK_MAIN()
Definition: benchmark.h:988
BM_ManualTiming
static void BM_ManualTiming(benchmark::State &state)
Definition: benchmark_test.cc:176
start
GLuint start
Definition: glcorearb.h:2858
benchmark::State::iterations
BENCHMARK_ALWAYS_INLINE size_t iterations() const
Definition: benchmark.h:535
size
#define size
Definition: glcorearb.h:2944
benchmark::State::SetIterationTime
void SetIterationTime(double seconds)
Definition: benchmark.cc:445
BM_LongTest
static void BM_LongTest(benchmark::State &state)
Definition: benchmark_test.cc:143
BM_Sequential
static void BM_Sequential(benchmark::State &state)
Definition: benchmark_test.cc:98
BM_StringCompare
static void BM_StringCompare(benchmark::State &state)
Definition: benchmark_test.cc:116
benchmark::State::KeepRunning
bool KeepRunning()
Definition: benchmark.h:404
Json::ValueType
ValueType
Type of the value held by a Value object.
Definition: json.h:463
depth
GLint GLint GLsizei GLsizei GLsizei depth
Definition: glcorearb.h:2859
BM_SetupTeardown
static void BM_SetupTeardown(benchmark::State &state)
Definition: benchmark_test.cc:123
n
GLdouble n
Definition: glcorearb.h:4153
i
int i
Definition: gmock-matchers_test.cc:764
benchmark::State::range
BENCHMARK_ALWAYS_INLINE int range(std::size_t pos=0) const
Definition: benchmark.h:523
v
const GLdouble * v
Definition: glcorearb.h:3106
Args
Args({7, 6, 3})
value_type
zend_class_entry * value_type
Definition: php/ext/google/protobuf/message.c:2546
benchmark::State
Definition: benchmark.h:399
size
GLsizeiptr size
Definition: glcorearb.h:2943
BM_DenseThreadRanges
static void BM_DenseThreadRanges(benchmark::State &st)
Definition: benchmark_test.cc:218
benchmark::State::SetItemsProcessed
BENCHMARK_ALWAYS_INLINE void SetItemsProcessed(size_t items)
Definition: benchmark.h:498
BENCHMARK_TEMPLATE
BENCHMARK_TEMPLATE(BM_Sequential, std::list< int >) -> Range(1<< 0, 1<< 10)
benchmark::State::threads
const int threads
Definition: benchmark.h:557
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: glcorearb.h:2879
benchmark::State::thread_index
const int thread_index
Definition: benchmark.h:555
BENCHMARK
BENCHMARK(BM_Factorial)
assert.h
BM_SetInsert
static void BM_SetInsert(benchmark::State &state)
Definition: benchmark_test.cc:84
benchmark::State::ResumeTiming
void ResumeTiming()
Definition: benchmark.cc:426
benchmark.h
benchmark::State::SetBytesProcessed
BENCHMARK_ALWAYS_INLINE void SetBytesProcessed(size_t bytes)
Definition: benchmark.h:475


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:48