proxygen
Benchmark.cpp File Reference

Go to the source code of this file.

Classes

struct  Blob< S >
 

Functions

 BENCHMARK (constantFuture)
 
 BENCHMARK_RELATIVE (promiseAndFuture)
 
 BENCHMARK_RELATIVE (withThen)
 
 BENCHMARK_DRAW_LINE ()
 
 BENCHMARK (oneThen)
 
 BENCHMARK_RELATIVE (twoThens)
 
 BENCHMARK_RELATIVE (fourThens)
 
 BENCHMARK_RELATIVE (hundredThens)
 
 BENCHMARK (no_contention)
 
 BENCHMARK_RELATIVE (contention)
 
void throwAndCatchImpl ()
 
void throwAndCatchWrappedImpl ()
 
void throwWrappedAndCatchImpl ()
 
void throwWrappedAndCatchWrappedImpl ()
 
void contend (void(*func)())
 
 BENCHMARK (throwAndCatch)
 
 BENCHMARK_RELATIVE (throwAndCatchWrapped)
 
 BENCHMARK_RELATIVE (throwWrappedAndCatch)
 
 BENCHMARK_RELATIVE (throwWrappedAndCatchWrapped)
 
 BENCHMARK (throwAndCatchContended)
 
 BENCHMARK_RELATIVE (throwAndCatchWrappedContended)
 
 BENCHMARK_RELATIVE (throwWrappedAndCatchContended)
 
 BENCHMARK_RELATIVE (throwWrappedAndCatchWrappedContended)
 
 BENCHMARK (lvalue_get)
 
 BENCHMARK_RELATIVE (rvalue_get)
 
template<class T >
Future< TfGen ()
 
template<class T >
std::vector< Future< T > > fsGen ()
 
template<class T >
void complexBenchmark ()
 
 BENCHMARK (complexUnit)
 
 BENCHMARK_RELATIVE (complexBlob4)
 
 BENCHMARK_RELATIVE (complexBlob8)
 
 BENCHMARK_RELATIVE (complexBlob64)
 
 BENCHMARK_RELATIVE (complexBlob128)
 
 BENCHMARK_RELATIVE (complexBlob256)
 
 BENCHMARK_RELATIVE (complexBlob512)
 
 BENCHMARK_RELATIVE (complexBlob1024)
 
 BENCHMARK_RELATIVE (complexBlob2048)
 
 BENCHMARK_RELATIVE (complexBlob4096)
 
int main (int argc, char **argv)
 

Variables

InlineExecutor exe
 

Function Documentation

BENCHMARK ( constantFuture  )

Definition at line 45 of file Benchmark.cpp.

References folly::makeFuture().

45  {
46  makeFuture(42);
47 }
Future< typename std::decay< T >::type > makeFuture(T &&t)
Definition: Future-inl.h:1310
BENCHMARK ( oneThen  )

Definition at line 66 of file Benchmark.cpp.

66  {
67  someThens(1);
68 }
BENCHMARK ( no_contention  )

Definition at line 90 of file Benchmark.cpp.

References BENCHMARK_SUSPEND, f, folly::gen::move, folly::Baton< MayBlock, Atom >::post(), and folly::Baton< MayBlock, Atom >::wait().

90  {
91  std::vector<Promise<int>> promises(10000);
92  std::vector<Future<int>> futures;
93  std::thread producer, consumer;
94 
96  folly::Baton<> b1, b2;
97  for (auto& p : promises) {
98  futures.push_back(p.getFuture());
99  }
100 
101  consumer = std::thread([&] {
102  b1.post();
103  for (auto& f : futures) {
104  std::move(f).then(incr<int>);
105  }
106  });
107  consumer.join();
108 
109  producer = std::thread([&] {
110  b2.post();
111  for (auto& p : promises) {
112  p.setValue(42);
113  }
114  });
115 
116  b1.wait();
117  b2.wait();
118  }
119 
120  // The only thing we are measuring is how long fulfill + callbacks take
121  producer.join();
122 }
auto f
#define BENCHMARK_SUSPEND
Definition: Benchmark.h:576
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
FOLLY_ALWAYS_INLINE void wait(const WaitOptions &opt=wait_options()) noexcept
Definition: Baton.h:170
void post() noexcept
Definition: Baton.h:123
BENCHMARK ( throwAndCatch  )

Definition at line 265 of file Benchmark.cpp.

References throwAndCatchImpl().

265  {
267 }
void throwAndCatchImpl()
Definition: Benchmark.cpp:176
BENCHMARK ( throwAndCatchContended  )

Definition at line 283 of file Benchmark.cpp.

References contend(), and throwAndCatchImpl().

283  {
285 }
void throwAndCatchImpl()
Definition: Benchmark.cpp:176
void contend(void(*func)())
Definition: Benchmark.cpp:241
BENCHMARK ( lvalue_get  )

Definition at line 317 of file Benchmark.cpp.

References folly::BenchmarkSuspender::dismissing(), folly::doNotOptimizeAway(), folly::makeFuture(), folly::gen::move, string, and folly::Optional< Value >::value().

317  {
318  BenchmarkSuspender suspender;
319  Optional<Future<Bulky>> future;
320  future = makeFuture(Bulky("Hello"));
321  suspender.dismissing([&] {
322  std::string message = std::move(future.value()).get().message();
323  doNotOptimizeAway(message);
324  });
325 }
Definition: test.c:42
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
const char * string
Definition: Conv.cpp:212
FOLLY_CPP14_CONSTEXPR const Value & value() const &
Definition: Optional.h:268
Future< typename std::decay< T >::type > makeFuture(T &&t)
Definition: Future-inl.h:1310
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( complexUnit  )

Definition at line 377 of file Benchmark.cpp.

377  {
378  complexBenchmark<Unit>();
379 }
BENCHMARK_DRAW_LINE ( )
BENCHMARK_RELATIVE ( promiseAndFuture  )

Definition at line 49 of file Benchmark.cpp.

References f, folly::Promise< T >::getFuture(), folly::Promise< T >::setValue(), and folly::futures::detail::FutureBase< T >::value().

49  {
50  Promise<int> p;
51  Future<int> f = p.getFuture();
52  p.setValue(42);
53  f.value();
54 }
auto f
Future< T > getFuture()
Definition: Promise-inl.h:97
std::enable_if< std::is_same< Unit, B >::value, void >::type setValue()
Definition: Promise.h:326
BENCHMARK_RELATIVE ( withThen  )

Definition at line 56 of file Benchmark.cpp.

References folly::BENCHMARK_DRAW_LINE(), f, folly::Promise< T >::getFuture(), folly::Promise< T >::setValue(), and folly::futures::detail::FutureBase< T >::value().

56  {
57  Promise<int> p;
58  Future<int> f = p.getFuture().then(incr<int>);
59  p.setValue(42);
60  f.value();
61 }
auto f
Future< T > getFuture()
Definition: Promise-inl.h:97
std::enable_if< std::is_same< Unit, B >::value, void >::type setValue()
Definition: Promise.h:326
BENCHMARK_RELATIVE ( twoThens  )

Definition at line 71 of file Benchmark.cpp.

71  {
72  someThens(2);
73 }
BENCHMARK_RELATIVE ( fourThens  )

Definition at line 76 of file Benchmark.cpp.

76  {
77  someThens(4);
78 }
BENCHMARK_RELATIVE ( hundredThens  )

Definition at line 81 of file Benchmark.cpp.

References folly::BENCHMARK_DRAW_LINE().

81  {
82  someThens(100);
83 }
BENCHMARK_RELATIVE ( contention  )

Definition at line 124 of file Benchmark.cpp.

References folly::BENCHMARK_DRAW_LINE(), BENCHMARK_SUSPEND, f, folly::gen::move, folly::Baton< MayBlock, Atom >::post(), and folly::Baton< MayBlock, Atom >::wait().

124  {
125  std::vector<Promise<int>> promises(10000);
126  std::vector<Future<int>> futures;
127  std::thread producer, consumer;
128  sem_t sem;
129  sem_init(&sem, 0, 0);
130 
132  folly::Baton<> b1, b2;
133  for (auto& p : promises) {
134  futures.push_back(p.getFuture());
135  }
136 
137  consumer = std::thread([&] {
138  b1.post();
139  for (auto& f : futures) {
140  sem_wait(&sem);
141  std::move(f).then(incr<int>);
142  }
143  });
144 
145  producer = std::thread([&] {
146  b2.post();
147  for (auto& p : promises) {
148  sem_post(&sem);
149  p.setValue(42);
150  }
151  });
152 
153  b1.wait();
154  b2.wait();
155  }
156 
157  // The astute reader will notice that we're not *precisely* comparing apples
158  // to apples here. Well, maybe it's like comparing Granny Smith to
159  // Braeburn or something. In the serial version, we waited for the futures
160  // to be all set up, but here we are probably still doing that work
161  // (although in parallel). But even though there is more work (on the order
162  // of 2x), it is being done by two threads. Hopefully most of the difference
163  // we see is due to lock contention and not false parallelism.
164  //
165  // Be warned that if the box is under heavy load, this will greatly skew
166  // these results (scheduling overhead will begin to dwarf lock contention).
167  // I'm not sure but I'd guess in Windtunnel this will mean large variance,
168  // because I expect they load the boxes as much as they can?
169  consumer.join();
170  producer.join();
171 }
auto f
#define BENCHMARK_SUSPEND
Definition: Benchmark.h:576
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
FOLLY_ALWAYS_INLINE void wait(const WaitOptions &opt=wait_options()) noexcept
Definition: Baton.h:170
void post() noexcept
Definition: Baton.h:123
BENCHMARK_RELATIVE ( throwAndCatchWrapped  )

Definition at line 269 of file Benchmark.cpp.

References throwAndCatchWrappedImpl().

269  {
271 }
void throwAndCatchWrappedImpl()
Definition: Benchmark.cpp:196
BENCHMARK_RELATIVE ( throwWrappedAndCatch  )

Definition at line 273 of file Benchmark.cpp.

References throwWrappedAndCatchImpl().

273  {
275 }
void throwWrappedAndCatchImpl()
Definition: Benchmark.cpp:209
BENCHMARK_RELATIVE ( throwWrappedAndCatchWrapped  )

Definition at line 277 of file Benchmark.cpp.

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

277  {
279 }
void throwWrappedAndCatchWrappedImpl()
Definition: Benchmark.cpp:226
BENCHMARK_RELATIVE ( throwAndCatchWrappedContended  )

Definition at line 287 of file Benchmark.cpp.

References contend(), and throwAndCatchWrappedImpl().

287  {
289 }
void throwAndCatchWrappedImpl()
Definition: Benchmark.cpp:196
void contend(void(*func)())
Definition: Benchmark.cpp:241
BENCHMARK_RELATIVE ( throwWrappedAndCatchContended  )

Definition at line 291 of file Benchmark.cpp.

References contend(), and throwWrappedAndCatchImpl().

291  {
293 }
void contend(void(*func)())
Definition: Benchmark.cpp:241
void throwWrappedAndCatchImpl()
Definition: Benchmark.cpp:209
BENCHMARK_RELATIVE ( throwWrappedAndCatchWrappedContended  )

Definition at line 295 of file Benchmark.cpp.

References folly::BENCHMARK_DRAW_LINE(), contend(), message, folly::gen::move, string, and throwWrappedAndCatchWrappedImpl().

295  {
297 }
void throwWrappedAndCatchWrappedImpl()
Definition: Benchmark.cpp:226
void contend(void(*func)())
Definition: Benchmark.cpp:241
BENCHMARK_RELATIVE ( rvalue_get  )

Definition at line 327 of file Benchmark.cpp.

References folly::BenchmarkSuspender::dismissing(), folly::doNotOptimizeAway(), folly::makeFuture(), folly::gen::move, string, and folly::Optional< Value >::value().

327  {
328  BenchmarkSuspender suspender;
329  Optional<Future<Bulky>> future;
330  future = makeFuture(Bulky("Hello"));
331  suspender.dismissing([&] {
332  std::string message = std::move(future.value()).get().message();
333  doNotOptimizeAway(message);
334  });
335 }
Definition: test.c:42
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
const char * string
Definition: Conv.cpp:212
FOLLY_CPP14_CONSTEXPR const Value & value() const &
Definition: Optional.h:268
Future< typename std::decay< T >::type > makeFuture(T &&t)
Definition: Future-inl.h:1310
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK_RELATIVE ( complexBlob4  )

Definition at line 381 of file Benchmark.cpp.

381  {
382  complexBenchmark<Blob<4>>();
383 }
BENCHMARK_RELATIVE ( complexBlob8  )

Definition at line 385 of file Benchmark.cpp.

385  {
386  complexBenchmark<Blob<8>>();
387 }
BENCHMARK_RELATIVE ( complexBlob64  )

Definition at line 389 of file Benchmark.cpp.

389  {
390  complexBenchmark<Blob<64>>();
391 }
BENCHMARK_RELATIVE ( complexBlob128  )

Definition at line 393 of file Benchmark.cpp.

393  {
394  complexBenchmark<Blob<128>>();
395 }
BENCHMARK_RELATIVE ( complexBlob256  )

Definition at line 397 of file Benchmark.cpp.

397  {
398  complexBenchmark<Blob<256>>();
399 }
BENCHMARK_RELATIVE ( complexBlob512  )

Definition at line 401 of file Benchmark.cpp.

401  {
402  complexBenchmark<Blob<512>>();
403 }
BENCHMARK_RELATIVE ( complexBlob1024  )

Definition at line 405 of file Benchmark.cpp.

405  {
406  complexBenchmark<Blob<1024>>();
407 }
BENCHMARK_RELATIVE ( complexBlob2048  )

Definition at line 409 of file Benchmark.cpp.

409  {
410  complexBenchmark<Blob<2048>>();
411 }
BENCHMARK_RELATIVE ( complexBlob4096  )

Definition at line 413 of file Benchmark.cpp.

413  {
414  complexBenchmark<Blob<4096>>();
415 }
template<class T >
void complexBenchmark ( )

Definition at line 362 of file Benchmark.cpp.

References folly::BENCHMARK_DRAW_LINE(), folly::collect(), folly::collectAll(), folly::collectAny(), folly::makeFuture(), folly::futures::map(), folly::pushmi::detail::t, and folly::T.

362  {
363  collect(fsGen<T>());
364  collectAll(fsGen<T>());
365  collectAny(fsGen<T>());
366  futures::map(fsGen<T>(), [](const T& t) { return t; });
367  futures::map(fsGen<T>(), [](const T& t) { return makeFuture(T(t)); });
368 }
std::enable_if< !std::is_same< invoke_result_t< typename std::iterator_traits< InputIterator >::value_type >, void >::value, typename std::pair< size_t, invoke_result_t< typename std::iterator_traits< InputIterator >::value_type > > >::type collectAny(InputIterator first, InputIterator last)
Definition: WhenN-inl.h:192
#define T(v)
Definition: http_parser.c:233
std::vector< typename std::enable_if< !std::is_same< invoke_result_t< typename std::iterator_traits< InputIterator >::value_type >, void >::value, invoke_result_t< typename std::iterator_traits< InputIterator >::value_type > >::type > collectAll(InputIterator first, InputIterator last)
Definition: WhenN-inl.h:147
static auto collect
Definition: StringTest.cpp:37
static Map map(mapCap)
Future< typename std::decay< T >::type > makeFuture(T &&t)
Definition: Future-inl.h:1310
void contend ( void(*)()  func)

Definition at line 241 of file Benchmark.cpp.

References folly::BenchmarkSuspender::dismiss(), i, folly::BenchmarkSuspender::rehire(), s, folly::pushmi::detail::t, and threads.

Referenced by BENCHMARK(), and BENCHMARK_RELATIVE().

241  {
243  const int N = 100;
244  const int iters = 1000;
245  pthread_barrier_t barrier;
246  pthread_barrier_init(&barrier, nullptr, N + 1);
247  std::vector<std::thread> threads;
248  for (int i = 0; i < N; i++) {
249  threads.push_back(std::thread([&]() {
250  pthread_barrier_wait(&barrier);
251  for (int j = 0; j < iters; j++) {
252  func();
253  }
254  }));
255  }
256  pthread_barrier_wait(&barrier);
257  s.dismiss();
258  for (auto& t : threads) {
259  t.join();
260  }
261  s.rehire();
262  pthread_barrier_destroy(&barrier);
263 }
std::vector< std::thread::id > threads
static set< string > s
template<class T >
Future<T> fGen ( )

Definition at line 340 of file Benchmark.cpp.

References f, folly::Promise< T >::getFuture(), folly::makeFuture(), folly::gen::move, folly::Promise< T >::setValue(), folly::pushmi::detail::t, folly::T, and folly::via().

340  {
341  Promise<T> p;
342  auto f = p.getFuture()
343  .then([](T&& t) { return std::move(t); })
344  .then([](T&& t) { return makeFuture(std::move(t)); })
345  .via(&exe)
346  .then([](T&& t) { return std::move(t); })
347  .then([](T&& t) { return makeFuture(std::move(t)); });
348  p.setValue(T());
349  return f;
350 }
#define T(v)
Definition: http_parser.c:233
auto f
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
PUSHMI_INLINE_VAR constexpr detail::via_fn via
Definition: via.h:166
Future< T > getFuture()
Definition: Promise-inl.h:97
std::enable_if< std::is_same< Unit, B >::value, void >::type setValue()
Definition: Promise.h:326
InlineExecutor exe
Definition: Benchmark.cpp:337
Future< typename std::decay< T >::type > makeFuture(T &&t)
Definition: Future-inl.h:1310
template<class T >
std::vector<Future<T> > fsGen ( )

Definition at line 353 of file Benchmark.cpp.

References i.

353  {
354  std::vector<Future<T>> fs;
355  for (auto i = 0; i < 10; i++) {
356  fs.push_back(fGen<T>());
357  }
358  return fs;
359 }
int main ( int  argc,
char **  argv 
)

Definition at line 417 of file Benchmark.cpp.

References folly::runBenchmarks().

417  {
418  gflags::ParseCommandLineFlags(&argc, &argv, true);
420  return 0;
421 }
void runBenchmarks()
Definition: Benchmark.cpp:456
char ** argv
void throwAndCatchImpl ( )

Definition at line 176 of file Benchmark.cpp.

References folly::makeFuture(), and folly::pushmi::detail::t.

Referenced by BENCHMARK().

176  {
177  makeFuture()
178  .then([](Try<Unit>&&) { throw std::runtime_error("oh no"); })
179  .then([](Try<Unit>&& t) {
180  try {
181  t.value();
182  } catch (const std::runtime_error& e) {
183  // ...
184  return;
185  }
186  CHECK(false);
187  });
188 }
Definition: Try.h:51
Future< typename std::decay< T >::type > makeFuture(T &&t)
Definition: Future-inl.h:1310
void throwAndCatchWrappedImpl ( )

Definition at line 196 of file Benchmark.cpp.

References folly::makeFuture(), and folly::pushmi::detail::t.

Referenced by BENCHMARK_RELATIVE().

196  {
197  makeFuture()
198  .then([](Try<Unit>&&) { throw std::runtime_error("oh no"); })
199  .then([](Try<Unit>&& t) {
200  auto caught = t.withException<std::runtime_error>(
201  [](const std::runtime_error& /* e */) {
202  // ...
203  });
204  CHECK(caught);
205  });
206 }
Definition: Try.h:51
Future< typename std::decay< T >::type > makeFuture(T &&t)
Definition: Future-inl.h:1310
void throwWrappedAndCatchImpl ( )

Definition at line 209 of file Benchmark.cpp.

References folly::makeFuture(), and folly::pushmi::detail::t.

Referenced by BENCHMARK_RELATIVE().

209  {
210  makeFuture()
211  .then([](Try<Unit>&&) {
212  return makeFuture<Unit>(std::runtime_error("oh no"));
213  })
214  .then([](Try<Unit>&& t) {
215  try {
216  t.value();
217  } catch (const std::runtime_error& e) {
218  // ...
219  return;
220  }
221  CHECK(false);
222  });
223 }
Definition: Try.h:51
Future< typename std::decay< T >::type > makeFuture(T &&t)
Definition: Future-inl.h:1310
void throwWrappedAndCatchWrappedImpl ( )

Definition at line 226 of file Benchmark.cpp.

References folly::makeFuture(), and folly::pushmi::detail::t.

Referenced by BENCHMARK_RELATIVE().

226  {
227  makeFuture()
228  .then([](Try<Unit>&&) {
229  return makeFuture<Unit>(std::runtime_error("oh no"));
230  })
231  .then([](Try<Unit>&& t) {
232  auto caught = t.withException<std::runtime_error>(
233  [](const std::runtime_error& /* e */) {
234  // ...
235  });
236  CHECK(caught);
237  });
238 }
Definition: Try.h:51
Future< typename std::decay< T >::type > makeFuture(T &&t)
Definition: Future-inl.h:1310

Variable Documentation