a2e2dcc39737d83aef9730cd95391ff8715ddba0
[folly.git] / folly / experimental / test / ReadMostlySharedPtrBenchmark.cpp
1 /*
2  * Copyright 2017 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/experimental/ReadMostlySharedPtr.h>
19
20 #include <iostream>
21 #include <thread>
22
23 #include <folly/Benchmark.h>
24 #include <folly/Memory.h>
25 #include <folly/experimental/RCURefCount.h>
26 #include <folly/portability/GFlags.h>
27
28 template <template<typename> class MainPtr,
29           template<typename> class WeakPtr,
30           size_t threadCount>
31 void benchmark(size_t n) {
32   MainPtr<int> mainPtr(std::make_unique<int>(42));
33
34   std::vector<std::thread> ts;
35
36   for (size_t t = 0; t < threadCount; ++t) {
37     ts.emplace_back([&]() {
38         WeakPtr<int> weakPtr(mainPtr);
39         // Prevent the compiler from hoisting code out of the loop.
40         auto op = [&]() FOLLY_NOINLINE { weakPtr.lock(); };
41
42         for (size_t i = 0; i < n; ++i) {
43           op();
44         }
45       });
46   }
47
48   for (auto& t: ts) {
49     t.join();
50   }
51 }
52
53 template <typename T>
54 using RCUMainPtr = folly::ReadMostlyMainPtr<T, folly::RCURefCount>;
55 template <typename T>
56 using RCUWeakPtr = folly::ReadMostlyWeakPtr<T, folly::RCURefCount>;
57 template <typename T>
58 using TLMainPtr = folly::ReadMostlyMainPtr<T, folly::TLRefCount>;
59 template <typename T>
60 using TLWeakPtr = folly::ReadMostlyWeakPtr<T, folly::TLRefCount>;
61
62
63 BENCHMARK(WeakPtrOneThread, n) {
64   benchmark<std::shared_ptr, std::weak_ptr, 1>(n);
65 }
66
67 BENCHMARK(WeakPtrFourThreads, n) {
68   benchmark<std::shared_ptr, std::weak_ptr, 4>(n);
69 }
70
71 BENCHMARK(RCUReadMostlyWeakPtrOneThread, n) {
72   benchmark<RCUMainPtr, RCUWeakPtr, 1>(n);
73 }
74
75 BENCHMARK(RCUReadMostlyWeakPtrFourThreads, n) {
76   benchmark<RCUMainPtr, RCUWeakPtr, 4>(n);
77 }
78
79 BENCHMARK(TLReadMostlyWeakPtrOneThread, n) {
80   benchmark<TLMainPtr, TLWeakPtr, 1>(n);
81 }
82
83 BENCHMARK(TLReadMostlyWeakPtrFourThreads, n) {
84   benchmark<TLMainPtr, TLWeakPtr, 4>(n);
85 }
86
87 int main(int argc, char** argv) {
88   gflags::ParseCommandLineFlags(&argc, &argv, true);
89   gflags::SetCommandLineOptionWithMode(
90     "bm_min_usec", "100000", gflags::SET_FLAG_IF_DEFAULT
91   );
92
93   folly::runBenchmarks();
94
95   return 0;
96 }