proxygen
BenchmarkTest.cpp File Reference
#include <folly/Benchmark.h>
#include <folly/String.h>
#include <folly/container/Foreach.h>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <random>
#include <vector>

Go to the source code of this file.

Functions

void fun ()
 
 BENCHMARK (bmFun)
 
 BENCHMARK (bmRepeatedFun, n)
 
 BENCHMARK_DRAW_LINE ()
 
 BENCHMARK (gun)
 
 BENCHMARK (optimizerCanDiscardTrivial, n)
 
 BENCHMARK (optimizerCanPowerReduceInner1Trivial, n)
 
 BENCHMARK (optimizerCanPowerReduceInner2Trivial, n)
 
 BENCHMARK (optimizerDisabled1Trivial, n)
 
 BENCHMARK (optimizerDisabled2Trivial, n)
 
 BENCHMARK (optimizerCanPowerReduceInner1TrivialPtr, n)
 
 BENCHMARK (optimizerCanPowerReduceInner2TrivialPtr, n)
 
 BENCHMARK (optimizerDisabled1TrivialPtr, n)
 
 BENCHMARK (optimizerCanDiscardNonTrivial, n)
 
 BENCHMARK (optimizerCanPowerReduceInner1NonTrivial, n)
 
 BENCHMARK (optimizerCanPowerReduceInner2NonTrivial, n)
 
 BENCHMARK (optimizerDisabled1NonTrivial, n)
 
 BENCHMARK (optimizerDisabled2NonTrivial, n)
 
 BENCHMARK (optimizerCanPowerReduceInner1NonTrivialPtr, n)
 
 BENCHMARK (optimizerCanPowerReduceInner2NonTrivialPtr, n)
 
 BENCHMARK (optimizerDisabled1NonTrivialPtr, n)
 
 BENCHMARK (baselinevector)
 
 BENCHMARK_RELATIVE (bmVector)
 
 BENCHMARK (superslow)
 
 BENCHMARK (noMulti)
 
 BENCHMARK_MULTI (multiSimple)
 
 BENCHMARK_RELATIVE_MULTI (multiSimpleRel)
 
 BENCHMARK_MULTI (multiIterArgs, iter)
 
 BENCHMARK_RELATIVE_MULTI (multiIterArgsRel, iter)
 
unsigned paramMulti (unsigned iter, unsigned num)
 
unsigned paramMultiRel (unsigned iter, unsigned num)
 
 BENCHMARK (BenchmarkSuspender_dismissing_void, iter)
 
 BENCHMARK (BenchmarkSuspender_dismissing_value, iter)
 
int main (int argc, char **argv)
 

Function Documentation

BENCHMARK ( bmFun  )

Definition at line 33 of file BenchmarkTest.cpp.

References fun().

33  {
34  fun();
35 }
void fun()
BENCHMARK ( bmRepeatedFun  ,
 
)

Definition at line 36 of file BenchmarkTest.cpp.

References folly::BENCHMARK_DRAW_LINE(), FOR_EACH_RANGE, fun(), and i.

36  {
37  FOR_EACH_RANGE (i, 0, n) { fun(); }
38 }
#define FOR_EACH_RANGE(i, begin, end)
Definition: Foreach.h:313
void fun()
BENCHMARK ( gun  )

Definition at line 41 of file BenchmarkTest.cpp.

References folly::BENCHMARK_DRAW_LINE(), and folly::doNotOptimizeAway().

41  {
42  static double x = 1;
43  x *= 2000;
45 }
Definition: InvokeTest.cpp:58
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( optimizerCanDiscardTrivial  ,
 
)

Definition at line 49 of file BenchmarkTest.cpp.

References i.

49  {
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 }
Definition: InvokeTest.cpp:58
BENCHMARK ( optimizerCanPowerReduceInner1Trivial  ,
 
)

Definition at line 58 of file BenchmarkTest.cpp.

References folly::doNotOptimizeAway(), and i.

58  {
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 }
Definition: InvokeTest.cpp:58
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( optimizerCanPowerReduceInner2Trivial  ,
 
)

Definition at line 68 of file BenchmarkTest.cpp.

References folly::doNotOptimizeAway(), i, and folly::makeUnpredictable().

68  {
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 }
Definition: InvokeTest.cpp:58
auto makeUnpredictable(T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:285
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( optimizerDisabled1Trivial  ,
 
)

Definition at line 79 of file BenchmarkTest.cpp.

References folly::doNotOptimizeAway(), and i.

79  {
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 }
Definition: InvokeTest.cpp:58
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( optimizerDisabled2Trivial  ,
 
)

Definition at line 89 of file BenchmarkTest.cpp.

References folly::doNotOptimizeAway(), i, and folly::makeUnpredictable().

89  {
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 }
Definition: InvokeTest.cpp:58
auto makeUnpredictable(T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:285
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( optimizerCanPowerReduceInner1TrivialPtr  ,
 
)

Definition at line 101 of file BenchmarkTest.cpp.

References folly::doNotOptimizeAway(), and i.

101  {
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 }
Definition: InvokeTest.cpp:58
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( optimizerCanPowerReduceInner2TrivialPtr  ,
 
)

Definition at line 111 of file BenchmarkTest.cpp.

References folly::doNotOptimizeAway(), i, and folly::makeUnpredictable().

111  {
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 }
Definition: InvokeTest.cpp:58
auto makeUnpredictable(T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:285
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( optimizerDisabled1TrivialPtr  ,
 
)

Definition at line 122 of file BenchmarkTest.cpp.

References folly::doNotOptimizeAway(), i, folly::operator+(), folly::operator+=(), folly::operator<(), and folly::detail::rhs.

122  {
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 }
Definition: InvokeTest.cpp:58
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( optimizerCanDiscardNonTrivial  ,
 
)

Definition at line 160 of file BenchmarkTest.cpp.

References i, and x.

160  {
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 }
Definition: InvokeTest.cpp:58
const int x
BENCHMARK ( optimizerCanPowerReduceInner1NonTrivial  ,
 
)

Definition at line 169 of file BenchmarkTest.cpp.

References folly::doNotOptimizeAway(), i, and x.

169  {
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 }
Definition: InvokeTest.cpp:58
const int x
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( optimizerCanPowerReduceInner2NonTrivial  ,
 
)

Definition at line 179 of file BenchmarkTest.cpp.

References folly::doNotOptimizeAway(), i, folly::makeUnpredictable(), and x.

179  {
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 }
Definition: InvokeTest.cpp:58
const int x
auto makeUnpredictable(T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:285
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( optimizerDisabled1NonTrivial  ,
 
)

Definition at line 190 of file BenchmarkTest.cpp.

References folly::doNotOptimizeAway(), i, and x.

190  {
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 }
Definition: InvokeTest.cpp:58
const int x
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( optimizerDisabled2NonTrivial  ,
 
)

Definition at line 200 of file BenchmarkTest.cpp.

References folly::doNotOptimizeAway(), i, folly::makeUnpredictable(), and x.

200  {
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 }
Definition: InvokeTest.cpp:58
const int x
auto makeUnpredictable(T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:285
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( optimizerCanPowerReduceInner1NonTrivialPtr  ,
 
)

Definition at line 212 of file BenchmarkTest.cpp.

References folly::doNotOptimizeAway(), i, and x.

212  {
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  }
219  }
220 }
Definition: InvokeTest.cpp:58
const int x
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( optimizerCanPowerReduceInner2NonTrivialPtr  ,
 
)

Definition at line 222 of file BenchmarkTest.cpp.

References folly::doNotOptimizeAway(), i, folly::makeUnpredictable(), and x.

222  {
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  }
231 }
Definition: InvokeTest.cpp:58
const int x
auto makeUnpredictable(T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:285
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( optimizerDisabled1NonTrivialPtr  ,
 
)

Definition at line 233 of file BenchmarkTest.cpp.

References folly::BENCHMARK_DRAW_LINE(), folly::doNotOptimizeAway(), i, and x.

233  {
234  NonTrivialLong x(0);
235  for (NonTrivialLong i(0); i < n; ++i) {
236  for (NonTrivialLong j(0); j < 10000; ++j) {
237  x += i + j;
239  }
240  }
241 }
Definition: InvokeTest.cpp:58
const int x
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( baselinevector  )

Definition at line 245 of file BenchmarkTest.cpp.

References BENCHMARK_SUSPEND, FOR_EACH_RANGE, i, and v.

245  {
246  vector<int> v;
247 
249  v.resize(1000);
250  }
251 
252  FOR_EACH_RANGE (i, 0, 100) { v.push_back(42); }
253 }
auto v
#define BENCHMARK_SUSPEND
Definition: Benchmark.h:576
#define FOR_EACH_RANGE(i, begin, end)
Definition: Foreach.h:313
BENCHMARK ( superslow  )

Definition at line 262 of file BenchmarkTest.cpp.

References folly::BENCHMARK_DRAW_LINE(), and folly::futures::sleep().

262  {
263  sleep(1);
264 }
Future< Unit > sleep(Duration dur, Timekeeper *tk)
Definition: Future.cpp:42
BENCHMARK ( noMulti  )

Definition at line 268 of file BenchmarkTest.cpp.

References fun().

268  {
269  fun();
270 }
void fun()
BENCHMARK ( BenchmarkSuspender_dismissing_void  ,
iter   
)

Definition at line 325 of file BenchmarkTest.cpp.

References folly::BenchmarkSuspender::dismissing(), rng, and v.

325  {
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 }
auto v
auto rng
Definition: CollectTest.cpp:31
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
BENCHMARK ( BenchmarkSuspender_dismissing_value  ,
iter   
)

Definition at line 336 of file BenchmarkTest.cpp.

References a, folly::f14::accumulate(), folly::BenchmarkSuspender::dismissing(), folly::doNotOptimizeAway(), rng, s, and v.

336  {
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 }
auto v
void accumulate(std::vector< std::size_t > &a, std::vector< std::size_t > const &d)
Definition: F14TestUtil.h:58
auto rng
Definition: CollectTest.cpp:31
char a
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
static set< string > s
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK_DRAW_LINE ( )
BENCHMARK_MULTI ( multiSimple  )

Definition at line 272 of file BenchmarkTest.cpp.

References FOR_EACH_RANGE, fun(), and i.

272  {
273  FOR_EACH_RANGE (i, 0, 10) { fun(); }
274  return 10;
275 }
#define FOR_EACH_RANGE(i, begin, end)
Definition: Foreach.h:313
void fun()
BENCHMARK_MULTI ( multiIterArgs  ,
iter   
)

Definition at line 285 of file BenchmarkTest.cpp.

References FOR_EACH_RANGE, fun(), and i.

285  {
286  FOR_EACH_RANGE (i, 0, 10 * iter) { fun(); }
287  return 10 * iter;
288 }
#define FOR_EACH_RANGE(i, begin, end)
Definition: Foreach.h:313
void fun()
BENCHMARK_RELATIVE ( bmVector  )

Definition at line 255 of file BenchmarkTest.cpp.

References folly::BENCHMARK_DRAW_LINE(), FOR_EACH_RANGE, i, and v.

255  {
256  vector<int> v;
257  FOR_EACH_RANGE (i, 0, 100) { v.resize(v.size() + 1, 42); }
258 }
auto v
#define FOR_EACH_RANGE(i, begin, end)
Definition: Foreach.h:313
BENCHMARK_RELATIVE_MULTI ( multiSimpleRel  )

Definition at line 277 of file BenchmarkTest.cpp.

References FOR_EACH_RANGE, fun(), and i.

277  {
278  FOR_EACH_RANGE (i, 0, 10) {
279  fun();
280  fun();
281  }
282  return 10;
283 }
#define FOR_EACH_RANGE(i, begin, end)
Definition: Foreach.h:313
void fun()
BENCHMARK_RELATIVE_MULTI ( multiIterArgsRel  ,
iter   
)

Definition at line 290 of file BenchmarkTest.cpp.

References FOR_EACH_RANGE, fun(), and i.

290  {
291  FOR_EACH_RANGE (i, 0, 10 * iter) {
292  fun();
293  fun();
294  }
295  return 10 * iter;
296 }
#define FOR_EACH_RANGE(i, begin, end)
Definition: Foreach.h:313
void fun()
void fun ( )

Definition at line 28 of file BenchmarkTest.cpp.

References folly::doNotOptimizeAway(), and x.

Referenced by BENCHMARK(), BENCHMARK_MULTI(), BENCHMARK_RELATIVE_MULTI(), folly::Function< std::vector< ResultT >(std::vector< InputT > &&)>::Function(), folly::getGlobalBenchmarkBaselineIndex(), folly::lazy(), folly::Function< std::vector< ResultT >(std::vector< InputT > &&)>::operator=(), paramMulti(), paramMultiRel(), folly::runBenchmarkGetNSPerIteration(), and TEST().

28  {
29  static double x = 1;
30  ++x;
32 }
Definition: InvokeTest.cpp:58
const int x
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
int main ( int  argc,
char **  argv 
)

Definition at line 352 of file BenchmarkTest.cpp.

References folly::runBenchmarks(), and folly::runBenchmarksOnFlag().

352  {
353  gflags::ParseCommandLineFlags(&argc, &argv, true);
354  runBenchmarks();
356 }
void runBenchmarks()
Definition: Benchmark.cpp:456
char ** argv
bool runBenchmarksOnFlag()
Definition: Benchmark.h:48
unsigned paramMulti ( unsigned  iter,
unsigned  num 
)

Definition at line 298 of file BenchmarkTest.cpp.

References fun(), and i.

Referenced by paramMultiRel().

298  {
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 }
void fun()
unsigned paramMultiRel ( unsigned  iter,
unsigned  num 
)

Definition at line 307 of file BenchmarkTest.cpp.

References folly::BENCHMARK_DRAW_LINE(), BENCHMARK_PARAM_MULTI, BENCHMARK_RELATIVE_PARAM_MULTI, fun(), i, and paramMulti().

307  {
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 }
void fun()