proxygen
SingletonBenchmark.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015-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 /* -*- Mode: C++; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */
17 
18 #include <folly/Singleton.h>
19 
20 #include <iostream>
21 #include <thread>
22 
23 #include <folly/Benchmark.h>
24 #include <folly/Memory.h>
26 
27 FOLLY_GNU_DISABLE_WARNING("-Wdeprecated-declarations")
28 
29 using namespace folly;
30 
31 // Benchmarking a normal singleton vs a Meyers singleton vs a Folly
32 // singleton. Meyers are insanely fast, but (hopefully) Folly
33 // singletons are fast "enough."
35  static auto ret = new int(0);
36  return ret;
37 }
38 
42  return &normal_singleton_value;
43 }
44 
46  int val = 0;
47 };
48 
49 void run4Threads(std::function<void()> f) {
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 }
58 
59 void normalSingleton(size_t n) {
60  for (size_t i = 0; i < n; ++i) {
62  }
63 }
64 
65 BENCHMARK(NormalSingleton, n) {
66  normalSingleton(n);
67 }
68 
69 BENCHMARK(NormalSingleton4Threads, n) {
70  run4Threads([=]() { normalSingleton(n); });
71 }
72 
73 void meyersSingleton(size_t n) {
74  for (size_t i = 0; i < n; ++i) {
76  }
77 }
78 
79 BENCHMARK(MeyersSingleton, n) {
80  meyersSingleton(n);
81 }
82 
83 BENCHMARK(MeyersSingleton4Threads, n) {
84  run4Threads([=]() { meyersSingleton(n); });
85 }
86 
87 struct BenchmarkTag {};
88 template <typename T, typename Tag = detail::DefaultTag>
90 
91 struct GetTag {};
92 struct TryGetTag {};
93 struct TryGetFastTag {};
94 
99 
100 void follySingletonRaw(size_t n) {
101  for (size_t i = 0; i < n; ++i) {
103  }
104 }
105 
106 BENCHMARK(FollySingletonRaw, n) {
108 }
109 
110 BENCHMARK(FollySingletonRaw4Threads, n) {
111  run4Threads([=]() { follySingletonRaw(n); });
112 }
113 
114 void follySingletonTryGet(size_t n) {
115  for (size_t i = 0; i < n; ++i) {
117  }
118 }
119 
120 BENCHMARK(FollySingletonTryGet, n) {
122 }
123 
124 BENCHMARK(FollySingletonTryGet4Threads, n) {
125  run4Threads([=]() { follySingletonTryGet(n); });
126 }
127 
128 void follySingletonTryGetFast(size_t n) {
129  for (size_t i = 0; i < n; ++i) {
131  }
132 }
133 
134 BENCHMARK(FollySingletonTryGetFast, n) {
136 }
137 
138 BENCHMARK(FollySingletonTryGetFast4Threads, n) {
139  run4Threads([=]() { follySingletonTryGetFast(n); });
140 }
141 
142 int main(int argc, char** argv) {
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 }
int main(int argc, char **argv)
#define FOLLY_GNU_DISABLE_WARNING(warningName)
Definition: Portability.h:180
auto f
int * getNormalSingleton()
int normal_singleton_value
BENCHMARK(NormalSingleton, n)
void run4Threads(std::function< void()> f)
void follySingletonTryGet(size_t n)
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
void runBenchmarks()
Definition: Benchmark.cpp:456
void meyersSingleton(size_t n)
std::vector< std::thread::id > threads
char ** argv
void follySingletonRaw(size_t n)
SingletonBenchmark< BenchmarkSingleton, TryGetFastTag > benchmark_singleton_try_get_fast
int * getMeyersSingleton()
SingletonBenchmark< BenchmarkSingleton, GetTag > benchmark_singleton_get
void normalSingleton(size_t n)
static std::shared_ptr< T > try_get()
Definition: Singleton.h:598
SingletonBenchmark< BenchmarkSingleton, TryGetTag > benchmark_singleton_try_get
static folly::ReadMostlySharedPtr< T > try_get_fast()
Definition: Singleton.h:602
static T * get()
Definition: Singleton.h:579
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
void follySingletonTryGetFast(size_t n)