proxygen
BenchmarkTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2012-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <folly/Benchmark.h>
18 #include <folly/String.h>
20 #include <algorithm>
21 #include <iostream>
22 #include <numeric>
23 #include <random>
24 #include <vector>
25 using namespace folly;
26 using namespace std;
27 
28 void fun() {
29  static double x = 1;
30  ++x;
32 }
33 BENCHMARK(bmFun) {
34  fun();
35 }
36 BENCHMARK(bmRepeatedFun, n) {
37  FOR_EACH_RANGE (i, 0, n) { fun(); }
38 }
40 
41 BENCHMARK(gun) {
42  static double x = 1;
43  x *= 2000;
45 }
46 
48 
49 BENCHMARK(optimizerCanDiscardTrivial, n) {
50  long x = 0;
51  for (long i = 0; i < n; ++i) {
52  for (long j = 0; j < 10000; ++j) {
53  x += j;
54  }
55  }
56 }
57 
58 BENCHMARK(optimizerCanPowerReduceInner1Trivial, n) {
59  long x = 0;
60  for (long i = 0; i < n; ++i) {
61  for (long j = 0; j < 10000; ++j) {
62  x += i + j;
63  }
65  }
66 }
67 
68 BENCHMARK(optimizerCanPowerReduceInner2Trivial, n) {
69  long x = 0;
70  for (long i = 0; i < n; ++i) {
72  for (long j = 0; j < 10000; ++j) {
73  x += i + j;
74  }
75  }
77 }
78 
79 BENCHMARK(optimizerDisabled1Trivial, n) {
80  long x = 0;
81  for (long i = 0; i < n; ++i) {
82  for (long j = 0; j < 10000; ++j) {
83  x += i + j;
85  }
86  }
87 }
88 
89 BENCHMARK(optimizerDisabled2Trivial, n) {
90  long x = 0;
91  for (long i = 0; i < n; ++i) {
93  for (long j = 0; j < 10000; ++j) {
95  x += i + j;
96  }
97  }
99 }
100 
101 BENCHMARK(optimizerCanPowerReduceInner1TrivialPtr, n) {
102  long x = 0;
103  for (long i = 0; i < n; ++i) {
104  for (long j = 0; j < 10000; ++j) {
105  x += i + j;
106  }
107  doNotOptimizeAway(&x);
108  }
109 }
110 
111 BENCHMARK(optimizerCanPowerReduceInner2TrivialPtr, n) {
112  long x = 0;
113  for (long i = 0; i < n; ++i) {
115  for (long j = 0; j < 10000; ++j) {
116  x += i + j;
117  }
118  }
119  doNotOptimizeAway(&x);
120 }
121 
122 BENCHMARK(optimizerDisabled1TrivialPtr, n) {
123  long x = 0;
124  for (long i = 0; i < n; ++i) {
125  for (long j = 0; j < 10000; ++j) {
126  x += i + j;
127  doNotOptimizeAway(&x);
128  }
129  }
130 }
131 
132 namespace {
133 class NonTrivialLong {
134  public:
135  explicit NonTrivialLong(long v) : value_(v) {}
136  virtual ~NonTrivialLong() {}
137 
138  void operator++() {
139  ++value_;
140  }
141  void operator+=(long rhs) {
142  value_ += rhs;
143  }
144  void operator+=(const NonTrivialLong& rhs) {
145  value_ += rhs.value_;
146  }
147  bool operator<(long rhs) {
148  return value_ < rhs;
149  }
150  NonTrivialLong operator+(const NonTrivialLong& rhs) {
151  return NonTrivialLong(value_ + rhs.value_);
152  }
153 
154  private:
155  long value_;
156  long otherStuff_[3];
157 };
158 } // namespace
159 
160 BENCHMARK(optimizerCanDiscardNonTrivial, n) {
161  NonTrivialLong x(0);
162  for (NonTrivialLong i(0); i < n; ++i) {
163  for (NonTrivialLong j(0); j < 10000; ++j) {
164  x += j;
165  }
166  }
167 }
168 
169 BENCHMARK(optimizerCanPowerReduceInner1NonTrivial, n) {
170  NonTrivialLong x(0);
171  for (NonTrivialLong i(0); i < n; ++i) {
172  for (NonTrivialLong j(0); j < 10000; ++j) {
173  x += i + j;
174  }
176  }
177 }
178 
179 BENCHMARK(optimizerCanPowerReduceInner2NonTrivial, n) {
180  NonTrivialLong x(0);
181  for (NonTrivialLong i(0); i < n; ++i) {
183  for (NonTrivialLong j(0); j < 10000; ++j) {
184  x += i + j;
185  }
186  }
188 }
189 
190 BENCHMARK(optimizerDisabled1NonTrivial, n) {
191  NonTrivialLong x(0);
192  for (NonTrivialLong i(0); i < n; ++i) {
193  for (NonTrivialLong j(0); j < 10000; ++j) {
194  x += i + j;
196  }
197  }
198 }
199 
200 BENCHMARK(optimizerDisabled2NonTrivial, n) {
201  NonTrivialLong x(0);
202  for (NonTrivialLong i(0); i < n; ++i) {
204  for (NonTrivialLong j(0); j < 10000; ++j) {
206  x += i + j;
207  }
208  }
210 }
211 
212 BENCHMARK(optimizerCanPowerReduceInner1NonTrivialPtr, n) {
213  NonTrivialLong x(0);
214  for (NonTrivialLong i(0); i < n; ++i) {
215  for (NonTrivialLong j(0); j < 10000; ++j) {
216  x += i + j;
217  }
218  doNotOptimizeAway(&x);
219  }
220 }
221 
222 BENCHMARK(optimizerCanPowerReduceInner2NonTrivialPtr, n) {
223  NonTrivialLong x(0);
224  for (NonTrivialLong i(0); i < n; ++i) {
226  for (NonTrivialLong j(0); j < 10000; ++j) {
227  x += i + j;
228  }
229  }
230  doNotOptimizeAway(&x);
231 }
232 
233 BENCHMARK(optimizerDisabled1NonTrivialPtr, n) {
234  NonTrivialLong x(0);
235  for (NonTrivialLong i(0); i < n; ++i) {
236  for (NonTrivialLong j(0); j < 10000; ++j) {
237  x += i + j;
238  doNotOptimizeAway(&x);
239  }
240  }
241 }
242 
244 
245 BENCHMARK(baselinevector) {
246  vector<int> v;
247 
249  v.resize(1000);
250  }
251 
252  FOR_EACH_RANGE (i, 0, 100) { v.push_back(42); }
253 }
254 
256  vector<int> v;
257  FOR_EACH_RANGE (i, 0, 100) { v.resize(v.size() + 1, 42); }
258 }
259 
261 
262 BENCHMARK(superslow) {
263  sleep(1);
264 }
265 
267 
268 BENCHMARK(noMulti) {
269  fun();
270 }
271 
272 BENCHMARK_MULTI(multiSimple) {
273  FOR_EACH_RANGE (i, 0, 10) { fun(); }
274  return 10;
275 }
276 
277 BENCHMARK_RELATIVE_MULTI(multiSimpleRel) {
278  FOR_EACH_RANGE (i, 0, 10) {
279  fun();
280  fun();
281  }
282  return 10;
283 }
284 
285 BENCHMARK_MULTI(multiIterArgs, iter) {
286  FOR_EACH_RANGE (i, 0, 10 * iter) { fun(); }
287  return 10 * iter;
288 }
289 
290 BENCHMARK_RELATIVE_MULTI(multiIterArgsRel, iter) {
291  FOR_EACH_RANGE (i, 0, 10 * iter) {
292  fun();
293  fun();
294  }
295  return 10 * iter;
296 }
297 
298 unsigned paramMulti(unsigned iter, unsigned num) {
299  for (unsigned i = 0; i < iter; ++i) {
300  for (unsigned j = 0; j < num; ++j) {
301  fun();
302  }
303  }
304  return num * iter;
305 }
306 
307 unsigned paramMultiRel(unsigned iter, unsigned num) {
308  for (unsigned i = 0; i < iter; ++i) {
309  for (unsigned j = 0; j < num; ++j) {
310  fun();
311  fun();
312  }
313  }
314  return num * iter;
315 }
316 
319 
322 
324 
325 BENCHMARK(BenchmarkSuspender_dismissing_void, iter) {
326  BenchmarkSuspender braces;
327  mt19937_64 rng;
328  while (iter--) {
329  vector<size_t> v(1 << 12, 0);
330  iota(v.begin(), v.end(), 0);
331  shuffle(v.begin(), v.end(), rng);
332  braces.dismissing([&] { sort(v.begin(), v.end()); });
333  }
334 }
335 
336 BENCHMARK(BenchmarkSuspender_dismissing_value, iter) {
337  BenchmarkSuspender braces;
338  mt19937_64 rng;
339  while (iter--) {
340  vector<size_t> v(1 << 12, 0);
341  iota(v.begin(), v.end(), 0);
342  shuffle(v.begin(), v.end(), rng);
343  auto s = braces.dismissing([&] {
344  sort(v.begin(), v.end());
345  return accumulate(
346  v.begin(), v.end(), 0, [](size_t a, size_t e) { return a + e; });
347  });
349  }
350 }
351 
352 int main(int argc, char** argv) {
353  gflags::ParseCommandLineFlags(&argc, &argv, true);
354  runBenchmarks();
356 }
Definition: InvokeTest.cpp:58
#define BENCHMARK_RELATIVE_PARAM_MULTI(name, param)
Definition: Benchmark.h:525
auto v
void accumulate(std::vector< std::size_t > &a, std::vector< std::size_t > const &d)
Definition: F14TestUtil.h:58
#define BENCHMARK_SUSPEND
Definition: Benchmark.h:576
Future< Unit > sleep(Duration dur, Timekeeper *tk)
Definition: Future.cpp:42
const int x
STL namespace.
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
void runBenchmarks()
Definition: Benchmark.cpp:456
FOLLY_PUSH_WARNING RHS rhs
Definition: Traits.h:649
auto rng
Definition: CollectTest.cpp:31
#define FOR_EACH_RANGE(i, begin, end)
Definition: Foreach.h:313
BENCHMARK_RELATIVE(bmVector)
unsigned paramMultiRel(unsigned iter, unsigned num)
char ** argv
#define BENCHMARK_PARAM_MULTI(name, param)
Definition: Benchmark.h:423
BENCHMARK_MULTI(multiSimple)
char a
BENCHMARK(fbFollyGlobalBenchmarkBaseline)
Definition: Benchmark.cpp:84
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
void fun()
unsigned paramMulti(unsigned iter, unsigned num)
bool runBenchmarksOnFlag()
Definition: Benchmark.h:48
BENCHMARK_DRAW_LINE()
static set< string > s
basic_fbstring< E, T, A, S > operator+(const basic_fbstring< E, T, A, S > &lhs, const basic_fbstring< E, T, A, S > &rhs)
Definition: FBString.h:2447
LogLevel & operator+=(LogLevel &level, uint32_t value)
Definition: LogLevel.h:115
auto makeUnpredictable(T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:285
int main(int argc, char **argv)
BENCHMARK_RELATIVE_MULTI(multiSimpleRel)
std::enable_if< IsLessThanComparable< Value >::value, bool >::type operator<(const Expected< Value, Error > &lhs, const Expected< Value, Error > &rhs)
Definition: Expected.h:1321
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258