proxygen
RxBenchmark.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017-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 
17 #include <folly/Benchmark.h>
20 #include <gflags/gflags.h>
21 
22 using namespace wangle;
24 
25 static std::unique_ptr<Observer<int>> makeObserver() {
26  return Observer<int>::create([&] (int) {});
27 }
28 
29 void subscribeImpl(uint32_t iters, int N, bool countUnsubscribe) {
30  for (uint32_t iter = 0; iter < iters; iter++) {
32  Subject<int> subject;
33  std::vector<std::unique_ptr<Observer<int>>> observers;
34  std::vector<Subscription<int>> subscriptions;
35  subscriptions.reserve(N);
36  for (int i = 0; i < N; i++) {
37  observers.push_back(makeObserver());
38  }
39  bs.dismiss();
40  for (int i = 0; i < N; i++) {
41  subscriptions.push_back(subject.subscribe(std::move(observers[i])));
42  }
43  if (countUnsubscribe) {
44  subscriptions.clear();
45  }
46  bs.rehire();
47  }
48 }
49 
50 void subscribeAndUnsubscribe(uint32_t iters, int N) {
51  subscribeImpl(iters, N, true);
52 }
53 
54 void subscribe(uint32_t iters, int N) {
55  subscribeImpl(iters, N, false);
56 }
57 
58 void observe(uint32_t iters, int N) {
59  for (uint32_t iter = 0; iter < iters; iter++) {
61  Subject<int> subject;
62  std::vector<std::unique_ptr<Observer<int>>> observers;
63  for (int i = 0; i < N; i++) {
64  observers.push_back(makeObserver());
65  }
66  bs.dismiss();
67  for (int i = 0; i < N; i++) {
68  subject.observe(std::move(observers[i]));
69  }
70  bs.rehire();
71  }
72 }
73 
74 void inlineObserve(uint32_t iters, int N) {
75  for (uint32_t iter = 0; iter < iters; iter++) {
77  Subject<int> subject;
78  std::vector<Observer<int>*> observers;
79  for (int i = 0; i < N; i++) {
80  observers.push_back(makeObserver().release());
81  }
82  bs.dismiss();
83  for (int i = 0; i < N; i++) {
84  subject.observe(observers[i]);
85  }
86  bs.rehire();
87  for (int i = 0; i < N; i++) {
88  delete observers[i];
89  }
90  }
91 }
92 
93 void notifySubscribers(uint32_t iters, int N) {
94  for (uint32_t iter = 0; iter < iters; iter++) {
96  Subject<int> subject;
97  std::vector<std::unique_ptr<Observer<int>>> observers;
98  std::vector<Subscription<int>> subscriptions;
99  subscriptions.reserve(N);
100  for (int i = 0; i < N; i++) {
101  observers.push_back(makeObserver());
102  }
103  for (int i = 0; i < N; i++) {
104  subscriptions.push_back(subject.subscribe(std::move(observers[i])));
105  }
106  bs.dismiss();
107  subject.onNext(42);
108  bs.rehire();
109  }
110 }
111 
112 void notifyInlineObservers(uint32_t iters, int N) {
113  for (uint32_t iter = 0; iter < iters; iter++) {
115  Subject<int> subject;
116  std::vector<Observer<int>*> observers;
117  for (int i = 0; i < N; i++) {
118  observers.push_back(makeObserver().release());
119  }
120  for (int i = 0; i < N; i++) {
121  subject.observe(observers[i]);
122  }
123  bs.dismiss();
124  subject.onNext(42);
125  bs.rehire();
126  }
127 }
128 
133 
135 
140 
142 
145 
147 
150 
151 int main(int argc, char** argv) {
152  gflags::ParseCommandLineFlags(&argc, &argv, true);
154  return 0;
155 }
virtual void observe(ObserverPtr< T > observer)
Definition: Observable.h:68
void subscribeAndUnsubscribe(uint32_t iters, int N)
Definition: RxBenchmark.cpp:50
virtual Subscription< T > subscribe(ObserverPtr< T > observer)
Definition: Observable.h:64
void subscribe(uint32_t iters, int N)
Definition: RxBenchmark.cpp:54
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void runBenchmarks()
Definition: Benchmark.cpp:456
BENCHMARK_DRAW_LINE()
void notifySubscribers(uint32_t iters, int N)
Definition: RxBenchmark.cpp:93
void observe(uint32_t iters, int N)
Definition: RxBenchmark.cpp:58
static std::unique_ptr< Observer< int > > makeObserver()
Definition: RxBenchmark.cpp:25
void onNext(const T &val) override
Definition: Subject.h:30
char ** argv
int main(int argc, char **argv)
void notifyInlineObservers(uint32_t iters, int N)
void inlineObserve(uint32_t iters, int N)
Definition: RxBenchmark.cpp:74
#define BENCHMARK_PARAM(name, param)
Definition: Benchmark.h:417
#define BENCHMARK_RELATIVE_PARAM(name, param)
Definition: Benchmark.h:518
void subscribeImpl(uint32_t iters, int N, bool countUnsubscribe)
Definition: RxBenchmark.cpp:29
static std::unique_ptr< Observer > create(N &&onNextFn, E &&onErrorFn, C &&onCompletedFn)
Definition: Observer.h:48