proxygen
SynchronizedBenchmark.cpp File Reference
#include <folly/Synchronized.h>
#include <folly/Benchmark.h>
#include <folly/portability/GTest.h>
#include <algorithm>
#include <condition_variable>
#include <map>
#include <memory>
#include <mutex>
#include <shared_mutex>
#include <thread>

Go to the source code of this file.

Functions

 DEFINE_uint64 (iterations, 100,"The number of iterations with lock held")
 
 BENCHMARK (UncontendedFollyLock, iters)
 
 BENCHMARK (UncontendedStdLock, iters)
 
 BENCHMARK (UncontendedOrdered, iters)
 
 BENCHMARK (UncontendedReverseOrdered, iters)
 
 BENCHMARK_DRAW_LINE ()
 
 BENCHMARK (ThreeThreadsSimpleFollyLock, iters)
 
 BENCHMARK (ThreeThreadsSimpleStdLock, iters)
 
 BENCHMARK (ThreeThreadsSimpleOrdered, iters)
 
 BENCHMARK (ThreeThreadsSimpleReverseOrdered, iters)
 
 BENCHMARK (ThreeThreadsPathologicalFollyLock, iters)
 
 BENCHMARK (ThreeThreadsPathologicalStdLock, iters)
 
 BENCHMARK (ThreeThreadsPathologicalOrdered, iters)
 
 BENCHMARK (ThreeThreadsPathologicalCarefullyOrdered, iters)
 
 BENCHMARK (TwoThreadsTwoMutexesOrdered, iters)
 
 BENCHMARK (TwoThreadsTwoMutexesSmart, iters)
 
 BENCHMARK (TwoThreadsTwoMutexesPersistent, iters)
 
 BENCHMARK (TwoThreadsFourMutexesOrdered, iters)
 
 BENCHMARK (TwoThreadsFourMutexesSmart, iters)
 
 BENCHMARK (TwoThreadsFourMutexesPersistent, iters)
 
 BENCHMARK (TwoThreadsEightMutexesOrdered, iters)
 
 BENCHMARK (TwoThreadsEightMutexesSmart, iters)
 
 BENCHMARK (TwoThreadsEightMutexesPersistent, iters)
 
 BENCHMARK (TwoThreadsSixteenMutexesOrdered, iters)
 
 BENCHMARK (TwoThreadsSixteenMutexesSmart, iters)
 
 BENCHMARK (TwoThreadsSixteenMutexesPersistent, iters)
 
 BENCHMARK (FourThreadsTwoMutexesOrdered, iters)
 
 BENCHMARK (FourThreadsTwoMutexesSmart, iters)
 
 BENCHMARK (FourThreadsTwoMutexesPersistent, iters)
 
 BENCHMARK (FourThreadsFourMutexesOrdered, iters)
 
 BENCHMARK (FourThreadsFourMutexesSmart, iters)
 
 BENCHMARK (FourThreadsFourMutexesPersistent, iters)
 
 BENCHMARK (FourThreadsEightMutexesOrdered, iters)
 
 BENCHMARK (FourThreadsEightMutexesSmart, iters)
 
 BENCHMARK (FourThreadsEightMutexesPersistent, iters)
 
 BENCHMARK (FourThreadsSixteenMutexesOrdered, iters)
 
 BENCHMARK (FourThreadsSixteenMutexesSmart, iters)
 
 BENCHMARK (FourThreadsSixteenMutexesPersistent, iters)
 
 BENCHMARK (EightThreadsTwoMutexesOrdered, iters)
 
 BENCHMARK (EightThreadsTwoMutexesSmart, iters)
 
 BENCHMARK (EightThreadsTwoMutexesPersistent, iters)
 
 BENCHMARK (EightThreadsFourMutexesOrdered, iters)
 
 BENCHMARK (EightThreadsFourMutexesSmart, iters)
 
 BENCHMARK (EightThreadsFourMutexesPersistent, iters)
 
 BENCHMARK (EightThreadsEightMutexesOrdered, iters)
 
 BENCHMARK (EightThreadsEightMutexesSmart, iters)
 
 BENCHMARK (EightThreadsEightMutexesPersistent, iters)
 
 BENCHMARK (EightThreadsSixteenMutexesOrdered, iters)
 
 BENCHMARK (EightThreadsSixteenMutexesSmart, iters)
 
 BENCHMARK (EightThreadsSixteenMutexesPersistent, iters)
 
 BENCHMARK (SixteenThreadsTwoMutexesOrdered, iters)
 
 BENCHMARK (SixteenThreadsTwoMutexesSmart, iters)
 
 BENCHMARK (SixteenThreadsTwoMutexesPersistent, iters)
 
 BENCHMARK (SixteenThreadsFourMutexesOrdered, iters)
 
 BENCHMARK (SixteenThreadsFourMutexesSmart, iters)
 
 BENCHMARK (SixteenThreadsFourMutexesPersistent, iters)
 
 BENCHMARK (SixteenThreadsEightMutexesOrdered, iters)
 
 BENCHMARK (SixteenThreadsEightMutexesSmart, iters)
 
 BENCHMARK (SixteenThreadsEightMutexesPersistent, iters)
 
 BENCHMARK (SixteenThreadsSixteenMutexesOrdered, iters)
 
 BENCHMARK (SixteenThreadsSixteenMutexesSmart, iters)
 
 BENCHMARK (SixteenThreadsSixteenMutexesPersistent, iters)
 
int main (int argc, char **argv)
 

Function Documentation

BENCHMARK ( UncontendedFollyLock  ,
iters   
)

Definition at line 225 of file SynchronizedBenchmark.cpp.

References folly::lock().

225  {
226  uncontended(iters, [](auto& one, auto& two) { folly::lock(one, two); });
227 }
auto lock(Synchronized< D, M > &synchronized, Args &&...args)
BENCHMARK ( UncontendedStdLock  ,
iters   
)

Definition at line 229 of file SynchronizedBenchmark.cpp.

References folly::detail::lock().

229  {
230  uncontended(iters, [](auto& one, auto& two) { std::lock(one, two); });
231 }
auto lock(SynchronizedLocker...lockersIn) -> std::tuple< typename SynchronizedLocker::LockedPtr... >
Definition: Synchronized.h:871
BENCHMARK ( UncontendedOrdered  ,
iters   
)

Definition at line 233 of file SynchronizedBenchmark.cpp.

233  {
234  uncontended(iters, [](auto& one, auto& two) {
235  one.lock();
236  two.lock();
237  });
238 }
BENCHMARK ( UncontendedReverseOrdered  ,
iters   
)

Definition at line 240 of file SynchronizedBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE().

240  {
241  uncontended(iters, [](auto& one, auto& two) {
242  two.lock();
243  one.lock();
244  });
245 }
BENCHMARK ( ThreeThreadsSimpleFollyLock  ,
iters   
)

Definition at line 249 of file SynchronizedBenchmark.cpp.

References folly::lock(), and simple.

249  {
250  simple(iters, [](auto& one, auto& two) { folly::lock(one, two); });
251 }
static bool simple
auto lock(Synchronized< D, M > &synchronized, Args &&...args)
BENCHMARK ( ThreeThreadsSimpleStdLock  ,
iters   
)

Definition at line 253 of file SynchronizedBenchmark.cpp.

References folly::detail::lock(), and simple.

253  {
254  simple(iters, [](auto& one, auto& two) { std::lock(one, two); });
255 }
static bool simple
auto lock(SynchronizedLocker...lockersIn) -> std::tuple< typename SynchronizedLocker::LockedPtr... >
Definition: Synchronized.h:871
BENCHMARK ( ThreeThreadsSimpleOrdered  ,
iters   
)

Definition at line 257 of file SynchronizedBenchmark.cpp.

References simple.

257  {
258  simple(iters, [](auto& one, auto& two) {
259  one.lock();
260  two.lock();
261  });
262 }
static bool simple
BENCHMARK ( ThreeThreadsSimpleReverseOrdered  ,
iters   
)

Definition at line 264 of file SynchronizedBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE(), and simple.

264  {
265  simple(iters, [](auto& one, auto& two) {
266  two.lock();
267  one.lock();
268  });
269 }
static bool simple
BENCHMARK ( ThreeThreadsPathologicalFollyLock  ,
iters   
)

Definition at line 273 of file SynchronizedBenchmark.cpp.

References folly::lock().

273  {
274  pathological(iters, [](auto& one, auto& two, auto& three) {
275  folly::lock(one, two, three);
276  });
277 }
auto lock(Synchronized< D, M > &synchronized, Args &&...args)
BENCHMARK ( ThreeThreadsPathologicalStdLock  ,
iters   
)

Definition at line 279 of file SynchronizedBenchmark.cpp.

References folly::detail::lock().

279  {
280  pathological(iters, [](auto& one, auto& two, auto& three) {
281  std::lock(one, two, three);
282  });
283 }
auto lock(SynchronizedLocker...lockersIn) -> std::tuple< typename SynchronizedLocker::LockedPtr... >
Definition: Synchronized.h:871
BENCHMARK ( ThreeThreadsPathologicalOrdered  ,
iters   
)

Definition at line 285 of file SynchronizedBenchmark.cpp.

285  {
286  pathological(iters, [](auto& one, auto& two, auto& three) {
287  one.lock();
288  two.lock();
289  three.lock();
290  });
291 }
BENCHMARK ( ThreeThreadsPathologicalCarefullyOrdered  ,
iters   
)

Definition at line 293 of file SynchronizedBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE().

293  {
294  pathological(iters, [](auto& one, auto& two, auto& three) {
295  two.lock();
296  three.lock();
297  one.lock();
298  });
299 }
BENCHMARK ( TwoThreadsTwoMutexesOrdered  ,
iters   
)

Definition at line 303 of file SynchronizedBenchmark.cpp.

303  {
304  ordered(iters, 2, makeVector<std::mutex>(2));
305 }
BENCHMARK ( TwoThreadsTwoMutexesSmart  ,
iters   
)

Definition at line 306 of file SynchronizedBenchmark.cpp.

306  {
307  smart(iters, 2, makeVector<std::mutex>(2));
308 }
BENCHMARK ( TwoThreadsTwoMutexesPersistent  ,
iters   
)

Definition at line 309 of file SynchronizedBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE().

309  {
310  persistent(iters, 2, makeVector<std::mutex>(2));
311 }
BENCHMARK ( TwoThreadsFourMutexesOrdered  ,
iters   
)

Definition at line 315 of file SynchronizedBenchmark.cpp.

315  {
316  ordered(iters, 2, makeVector<std::mutex>(4));
317 }
BENCHMARK ( TwoThreadsFourMutexesSmart  ,
iters   
)

Definition at line 318 of file SynchronizedBenchmark.cpp.

318  {
319  smart(iters, 2, makeVector<std::mutex>(4));
320 }
BENCHMARK ( TwoThreadsFourMutexesPersistent  ,
iters   
)

Definition at line 321 of file SynchronizedBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE().

321  {
322  persistent(iters, 2, makeVector<std::mutex>(4));
323 }
BENCHMARK ( TwoThreadsEightMutexesOrdered  ,
iters   
)

Definition at line 327 of file SynchronizedBenchmark.cpp.

327  {
328  ordered(iters, 2, makeVector<std::mutex>(8));
329 }
BENCHMARK ( TwoThreadsEightMutexesSmart  ,
iters   
)

Definition at line 330 of file SynchronizedBenchmark.cpp.

330  {
331  smart(iters, 2, makeVector<std::mutex>(8));
332 }
BENCHMARK ( TwoThreadsEightMutexesPersistent  ,
iters   
)

Definition at line 333 of file SynchronizedBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE().

333  {
334  persistent(iters, 2, makeVector<std::mutex>(8));
335 }
BENCHMARK ( TwoThreadsSixteenMutexesOrdered  ,
iters   
)

Definition at line 339 of file SynchronizedBenchmark.cpp.

339  {
340  ordered(iters, 2, makeVector<std::mutex>(16));
341 }
BENCHMARK ( TwoThreadsSixteenMutexesSmart  ,
iters   
)

Definition at line 342 of file SynchronizedBenchmark.cpp.

342  {
343  smart(iters, 2, makeVector<std::mutex>(16));
344 }
BENCHMARK ( TwoThreadsSixteenMutexesPersistent  ,
iters   
)

Definition at line 345 of file SynchronizedBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE().

345  {
346  persistent(iters, 2, makeVector<std::mutex>(16));
347 }
BENCHMARK ( FourThreadsTwoMutexesOrdered  ,
iters   
)

Definition at line 351 of file SynchronizedBenchmark.cpp.

351  {
352  ordered(iters, 4, makeVector<std::mutex>(2));
353 }
BENCHMARK ( FourThreadsTwoMutexesSmart  ,
iters   
)

Definition at line 354 of file SynchronizedBenchmark.cpp.

354  {
355  smart(iters, 4, makeVector<std::mutex>(2));
356 }
BENCHMARK ( FourThreadsTwoMutexesPersistent  ,
iters   
)

Definition at line 357 of file SynchronizedBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE().

357  {
358  persistent(iters, 4, makeVector<std::mutex>(2));
359 }
BENCHMARK ( FourThreadsFourMutexesOrdered  ,
iters   
)

Definition at line 363 of file SynchronizedBenchmark.cpp.

363  {
364  ordered(iters, 4, makeVector<std::mutex>(4));
365 }
BENCHMARK ( FourThreadsFourMutexesSmart  ,
iters   
)

Definition at line 366 of file SynchronizedBenchmark.cpp.

366  {
367  smart(iters, 4, makeVector<std::mutex>(4));
368 }
BENCHMARK ( FourThreadsFourMutexesPersistent  ,
iters   
)

Definition at line 369 of file SynchronizedBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE().

369  {
370  persistent(iters, 4, makeVector<std::mutex>(4));
371 }
BENCHMARK ( FourThreadsEightMutexesOrdered  ,
iters   
)

Definition at line 375 of file SynchronizedBenchmark.cpp.

375  {
376  ordered(iters, 4, makeVector<std::mutex>(8));
377 }
BENCHMARK ( FourThreadsEightMutexesSmart  ,
iters   
)

Definition at line 378 of file SynchronizedBenchmark.cpp.

378  {
379  smart(iters, 4, makeVector<std::mutex>(8));
380 }
BENCHMARK ( FourThreadsEightMutexesPersistent  ,
iters   
)

Definition at line 381 of file SynchronizedBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE().

381  {
382  persistent(iters, 4, makeVector<std::mutex>(8));
383 }
BENCHMARK ( FourThreadsSixteenMutexesOrdered  ,
iters   
)

Definition at line 387 of file SynchronizedBenchmark.cpp.

387  {
388  ordered(iters, 4, makeVector<std::mutex>(16));
389 }
BENCHMARK ( FourThreadsSixteenMutexesSmart  ,
iters   
)

Definition at line 390 of file SynchronizedBenchmark.cpp.

390  {
391  smart(iters, 4, makeVector<std::mutex>(16));
392 }
BENCHMARK ( FourThreadsSixteenMutexesPersistent  ,
iters   
)

Definition at line 393 of file SynchronizedBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE().

393  {
394  persistent(iters, 4, makeVector<std::mutex>(16));
395 }
BENCHMARK ( EightThreadsTwoMutexesOrdered  ,
iters   
)

Definition at line 399 of file SynchronizedBenchmark.cpp.

399  {
400  ordered(iters, 8, makeVector<std::mutex>(2));
401 }
BENCHMARK ( EightThreadsTwoMutexesSmart  ,
iters   
)

Definition at line 402 of file SynchronizedBenchmark.cpp.

402  {
403  smart(iters, 8, makeVector<std::mutex>(2));
404 }
BENCHMARK ( EightThreadsTwoMutexesPersistent  ,
iters   
)

Definition at line 405 of file SynchronizedBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE().

405  {
406  persistent(iters, 8, makeVector<std::mutex>(2));
407 }
BENCHMARK ( EightThreadsFourMutexesOrdered  ,
iters   
)

Definition at line 411 of file SynchronizedBenchmark.cpp.

411  {
412  ordered(iters, 8, makeVector<std::mutex>(4));
413 }
BENCHMARK ( EightThreadsFourMutexesSmart  ,
iters   
)

Definition at line 414 of file SynchronizedBenchmark.cpp.

414  {
415  smart(iters, 8, makeVector<std::mutex>(4));
416 }
BENCHMARK ( EightThreadsFourMutexesPersistent  ,
iters   
)

Definition at line 417 of file SynchronizedBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE().

417  {
418  persistent(iters, 8, makeVector<std::mutex>(4));
419 }
BENCHMARK ( EightThreadsEightMutexesOrdered  ,
iters   
)

Definition at line 423 of file SynchronizedBenchmark.cpp.

423  {
424  ordered(iters, 8, makeVector<std::mutex>(8));
425 }
BENCHMARK ( EightThreadsEightMutexesSmart  ,
iters   
)

Definition at line 426 of file SynchronizedBenchmark.cpp.

426  {
427  smart(iters, 8, makeVector<std::mutex>(8));
428 }
BENCHMARK ( EightThreadsEightMutexesPersistent  ,
iters   
)

Definition at line 429 of file SynchronizedBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE().

429  {
430  persistent(iters, 8, makeVector<std::mutex>(8));
431 }
BENCHMARK ( EightThreadsSixteenMutexesOrdered  ,
iters   
)

Definition at line 435 of file SynchronizedBenchmark.cpp.

435  {
436  ordered(iters, 8, makeVector<std::mutex>(16));
437 }
BENCHMARK ( EightThreadsSixteenMutexesSmart  ,
iters   
)

Definition at line 438 of file SynchronizedBenchmark.cpp.

438  {
439  smart(iters, 8, makeVector<std::mutex>(16));
440 }
BENCHMARK ( EightThreadsSixteenMutexesPersistent  ,
iters   
)

Definition at line 441 of file SynchronizedBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE().

441  {
442  persistent(iters, 8, makeVector<std::mutex>(16));
443 }
BENCHMARK ( SixteenThreadsTwoMutexesOrdered  ,
iters   
)

Definition at line 447 of file SynchronizedBenchmark.cpp.

447  {
448  ordered(iters, 16, makeVector<std::mutex>(2));
449 }
BENCHMARK ( SixteenThreadsTwoMutexesSmart  ,
iters   
)

Definition at line 450 of file SynchronizedBenchmark.cpp.

450  {
451  smart(iters, 16, makeVector<std::mutex>(2));
452 }
BENCHMARK ( SixteenThreadsTwoMutexesPersistent  ,
iters   
)

Definition at line 453 of file SynchronizedBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE().

453  {
454  persistent(iters, 16, makeVector<std::mutex>(2));
455 }
BENCHMARK ( SixteenThreadsFourMutexesOrdered  ,
iters   
)

Definition at line 459 of file SynchronizedBenchmark.cpp.

459  {
460  ordered(iters, 16, makeVector<std::mutex>(4));
461 }
BENCHMARK ( SixteenThreadsFourMutexesSmart  ,
iters   
)

Definition at line 462 of file SynchronizedBenchmark.cpp.

462  {
463  smart(iters, 16, makeVector<std::mutex>(4));
464 }
BENCHMARK ( SixteenThreadsFourMutexesPersistent  ,
iters   
)

Definition at line 465 of file SynchronizedBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE().

465  {
466  persistent(iters, 16, makeVector<std::mutex>(4));
467 }
BENCHMARK ( SixteenThreadsEightMutexesOrdered  ,
iters   
)

Definition at line 471 of file SynchronizedBenchmark.cpp.

471  {
472  ordered(iters, 16, makeVector<std::mutex>(8));
473 }
BENCHMARK ( SixteenThreadsEightMutexesSmart  ,
iters   
)

Definition at line 474 of file SynchronizedBenchmark.cpp.

474  {
475  smart(iters, 16, makeVector<std::mutex>(8));
476 }
BENCHMARK ( SixteenThreadsEightMutexesPersistent  ,
iters   
)

Definition at line 477 of file SynchronizedBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE().

477  {
478  persistent(iters, 16, makeVector<std::mutex>(8));
479 }
BENCHMARK ( SixteenThreadsSixteenMutexesOrdered  ,
iters   
)

Definition at line 483 of file SynchronizedBenchmark.cpp.

483  {
484  ordered(iters, 16, makeVector<std::mutex>(16));
485 }
BENCHMARK ( SixteenThreadsSixteenMutexesSmart  ,
iters   
)

Definition at line 486 of file SynchronizedBenchmark.cpp.

486  {
487  smart(iters, 16, makeVector<std::mutex>(16));
488 }
BENCHMARK ( SixteenThreadsSixteenMutexesPersistent  ,
iters   
)

Definition at line 489 of file SynchronizedBenchmark.cpp.

489  {
490  persistent(iters, 16, makeVector<std::mutex>(16));
491 }
BENCHMARK_DRAW_LINE ( )
DEFINE_uint64 ( iterations  ,
100  ,
"The number of iterations with lock held"   
)
int main ( int  argc,
char **  argv 
)

Definition at line 493 of file SynchronizedBenchmark.cpp.

References folly::doNotOptimizeAway(), i, mutex, folly::runBenchmarks(), simple, folly::detail::distributed_mutex::spin(), folly::f14::swap(), threads, and folly::fibers::yield().

493  {
494  gflags::ParseCommandLineFlags(&argc, &argv, true);
496 }
void runBenchmarks()
Definition: Benchmark.cpp:456
char ** argv