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