proxygen
SingletonBenchmark.cpp File Reference
#include <folly/Singleton.h>
#include <iostream>
#include <thread>
#include <folly/Benchmark.h>
#include <folly/Memory.h>
#include <folly/portability/GFlags.h>

Go to the source code of this file.

Classes

struct  BenchmarkSingleton
 
struct  BenchmarkTag
 
struct  GetTag
 
struct  TryGetTag
 
struct  TryGetFastTag
 

Typedefs

template<typename T , typename Tag = detail::DefaultTag>
using SingletonBenchmark = Singleton< T, Tag, BenchmarkTag >
 

Functions

int * getMeyersSingleton ()
 
int * getNormalSingleton ()
 
void run4Threads (std::function< void()> f)
 
void normalSingleton (size_t n)
 
 BENCHMARK (NormalSingleton, n)
 
 BENCHMARK (NormalSingleton4Threads, n)
 
void meyersSingleton (size_t n)
 
 BENCHMARK (MeyersSingleton, n)
 
 BENCHMARK (MeyersSingleton4Threads, n)
 
void follySingletonRaw (size_t n)
 
 BENCHMARK (FollySingletonRaw, n)
 
 BENCHMARK (FollySingletonRaw4Threads, n)
 
void follySingletonTryGet (size_t n)
 
 BENCHMARK (FollySingletonTryGet, n)
 
 BENCHMARK (FollySingletonTryGet4Threads, n)
 
void follySingletonTryGetFast (size_t n)
 
 BENCHMARK (FollySingletonTryGetFast, n)
 
 BENCHMARK (FollySingletonTryGetFast4Threads, n)
 
int main (int argc, char **argv)
 

Variables

int normal_singleton_value = 0
 
SingletonBenchmark< BenchmarkSingleton, GetTagbenchmark_singleton_get
 
SingletonBenchmark< BenchmarkSingleton, TryGetTagbenchmark_singleton_try_get
 
SingletonBenchmark< BenchmarkSingleton, TryGetFastTagbenchmark_singleton_try_get_fast
 

Typedef Documentation

template<typename T , typename Tag = detail::DefaultTag>
using SingletonBenchmark = Singleton<T, Tag, BenchmarkTag>

Definition at line 89 of file SingletonBenchmark.cpp.

Function Documentation

BENCHMARK ( NormalSingleton  ,
 
)

Definition at line 65 of file SingletonBenchmark.cpp.

References normalSingleton().

65  {
66  normalSingleton(n);
67 }
void normalSingleton(size_t n)
BENCHMARK ( NormalSingleton4Threads  ,
 
)

Definition at line 69 of file SingletonBenchmark.cpp.

References normalSingleton(), and run4Threads().

69  {
70  run4Threads([=]() { normalSingleton(n); });
71 }
void run4Threads(std::function< void()> f)
void normalSingleton(size_t n)
BENCHMARK ( MeyersSingleton  ,
 
)

Definition at line 79 of file SingletonBenchmark.cpp.

References meyersSingleton().

79  {
80  meyersSingleton(n);
81 }
void meyersSingleton(size_t n)
BENCHMARK ( MeyersSingleton4Threads  ,
 
)

Definition at line 83 of file SingletonBenchmark.cpp.

References meyersSingleton(), and run4Threads().

83  {
84  run4Threads([=]() { meyersSingleton(n); });
85 }
void run4Threads(std::function< void()> f)
void meyersSingleton(size_t n)
BENCHMARK ( FollySingletonRaw  ,
 
)

Definition at line 106 of file SingletonBenchmark.cpp.

References follySingletonRaw().

106  {
108 }
void follySingletonRaw(size_t n)
BENCHMARK ( FollySingletonRaw4Threads  ,
 
)

Definition at line 110 of file SingletonBenchmark.cpp.

References follySingletonRaw(), and run4Threads().

110  {
111  run4Threads([=]() { follySingletonRaw(n); });
112 }
void run4Threads(std::function< void()> f)
void follySingletonRaw(size_t n)
BENCHMARK ( FollySingletonTryGet  ,
 
)

Definition at line 120 of file SingletonBenchmark.cpp.

References follySingletonTryGet().

120  {
122 }
void follySingletonTryGet(size_t n)
BENCHMARK ( FollySingletonTryGet4Threads  ,
 
)

Definition at line 124 of file SingletonBenchmark.cpp.

References follySingletonTryGet(), and run4Threads().

124  {
125  run4Threads([=]() { follySingletonTryGet(n); });
126 }
void run4Threads(std::function< void()> f)
void follySingletonTryGet(size_t n)
BENCHMARK ( FollySingletonTryGetFast  ,
 
)

Definition at line 134 of file SingletonBenchmark.cpp.

References follySingletonTryGetFast().

134  {
136 }
void follySingletonTryGetFast(size_t n)
BENCHMARK ( FollySingletonTryGetFast4Threads  ,
 
)

Definition at line 138 of file SingletonBenchmark.cpp.

References follySingletonTryGetFast(), and run4Threads().

138  {
139  run4Threads([=]() { follySingletonTryGetFast(n); });
140 }
void run4Threads(std::function< void()> f)
void follySingletonTryGetFast(size_t n)
void follySingletonRaw ( size_t  n)

Definition at line 100 of file SingletonBenchmark.cpp.

References folly::Singleton< T, Tag, VaultTag >::get(), and i.

Referenced by BENCHMARK().

100  {
101  for (size_t i = 0; i < n; ++i) {
103  }
104 }
static T * get()
Definition: Singleton.h:579
void follySingletonTryGet ( size_t  n)

Definition at line 114 of file SingletonBenchmark.cpp.

References i, and folly::Singleton< T, Tag, VaultTag >::try_get().

Referenced by BENCHMARK().

114  {
115  for (size_t i = 0; i < n; ++i) {
117  }
118 }
static std::shared_ptr< T > try_get()
Definition: Singleton.h:598
void follySingletonTryGetFast ( size_t  n)

Definition at line 128 of file SingletonBenchmark.cpp.

References i, and folly::Singleton< T, Tag, VaultTag >::try_get_fast().

Referenced by BENCHMARK().

128  {
129  for (size_t i = 0; i < n; ++i) {
131  }
132 }
static folly::ReadMostlySharedPtr< T > try_get_fast()
Definition: Singleton.h:602
int* getMeyersSingleton ( )

Definition at line 34 of file SingletonBenchmark.cpp.

Referenced by meyersSingleton().

34  {
35  static auto ret = new int(0);
36  return ret;
37 }
int* getNormalSingleton ( )

Definition at line 40 of file SingletonBenchmark.cpp.

References folly::doNotOptimizeAway(), and normal_singleton_value.

Referenced by normalSingleton().

40  {
42  return &normal_singleton_value;
43 }
int normal_singleton_value
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 142 of file SingletonBenchmark.cpp.

References folly::runBenchmarks().

142  {
143  gflags::ParseCommandLineFlags(&argc, &argv, true);
144  gflags::SetCommandLineOptionWithMode(
145  "bm_min_usec", "100000", gflags::SET_FLAG_IF_DEFAULT);
146 
148 
149  return 0;
150 }
void runBenchmarks()
Definition: Benchmark.cpp:456
char ** argv
void meyersSingleton ( size_t  n)

Definition at line 73 of file SingletonBenchmark.cpp.

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

Referenced by BENCHMARK().

73  {
74  for (size_t i = 0; i < n; ++i) {
76  }
77 }
int * getMeyersSingleton()
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
void normalSingleton ( size_t  n)

Definition at line 59 of file SingletonBenchmark.cpp.

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

Referenced by BENCHMARK().

59  {
60  for (size_t i = 0; i < n; ++i) {
62  }
63 }
int * getNormalSingleton()
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
void run4Threads ( std::function< void()>  f)

Definition at line 49 of file SingletonBenchmark.cpp.

References f, i, and threads.

Referenced by BENCHMARK().

49  {
50  std::vector<std::thread> threads;
51  for (size_t i = 0; i < 4; ++i) {
52  threads.emplace_back(f);
53  }
54  for (auto& thread : threads) {
55  thread.join();
56  }
57 }
auto f
std::vector< std::thread::id > threads

Variable Documentation

SingletonBenchmark<BenchmarkSingleton, GetTag> benchmark_singleton_get

Definition at line 95 of file SingletonBenchmark.cpp.

SingletonBenchmark<BenchmarkSingleton, TryGetTag> benchmark_singleton_try_get

Definition at line 96 of file SingletonBenchmark.cpp.

SingletonBenchmark<BenchmarkSingleton, TryGetFastTag> benchmark_singleton_try_get_fast

Definition at line 98 of file SingletonBenchmark.cpp.

int normal_singleton_value = 0

Definition at line 39 of file SingletonBenchmark.cpp.

Referenced by getNormalSingleton().