copy wangle back into folly
[folly.git] / folly / wangle / rx / test / RxBenchmark.cpp
1 /*
2  * Copyright 2015 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>
18 #include <folly/wangle/rx/Observer.h>
19 #include <folly/wangle/rx/Subject.h>
20 #include <gflags/gflags.h>
21
22 using namespace folly::wangle;
23 using folly::BenchmarkSuspender;
24
25 static std::unique_ptr<Observer<int>> makeObserver() {
26   return Observer<int>::create([&] (int x) {});
27 }
28
29 void subscribeImpl(uint iters, int N, bool countUnsubscribe) {
30   for (uint iter = 0; iter < iters; iter++) {
31     BenchmarkSuspender bs;
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(uint iters, int N) {
51   subscribeImpl(iters, N, true);
52 }
53
54 void subscribe(uint iters, int N) {
55   subscribeImpl(iters, N, false);
56 }
57
58 void observe(uint iters, int N) {
59   for (uint iter = 0; iter < iters; iter++) {
60     BenchmarkSuspender bs;
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(uint iters, int N) {
75   for (uint iter = 0; iter < iters; iter++) {
76     BenchmarkSuspender bs;
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(uint iters, int N) {
94   for (uint iter = 0; iter < iters; iter++) {
95     BenchmarkSuspender bs;
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(uint iters, int N) {
113   for (uint iter = 0; iter < iters; iter++) {
114     BenchmarkSuspender bs;
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
129 BENCHMARK_PARAM(subscribeAndUnsubscribe, 1);
130 BENCHMARK_RELATIVE_PARAM(subscribe, 1);
131 BENCHMARK_RELATIVE_PARAM(observe, 1);
132 BENCHMARK_RELATIVE_PARAM(inlineObserve, 1);
133
134 BENCHMARK_DRAW_LINE();
135
136 BENCHMARK_PARAM(subscribeAndUnsubscribe, 1000);
137 BENCHMARK_RELATIVE_PARAM(subscribe, 1000);
138 BENCHMARK_RELATIVE_PARAM(observe, 1000);
139 BENCHMARK_RELATIVE_PARAM(inlineObserve, 1000);
140
141 BENCHMARK_DRAW_LINE();
142
143 BENCHMARK_PARAM(notifySubscribers, 1);
144 BENCHMARK_RELATIVE_PARAM(notifyInlineObservers, 1);
145
146 BENCHMARK_DRAW_LINE();
147
148 BENCHMARK_PARAM(notifySubscribers, 1000);
149 BENCHMARK_RELATIVE_PARAM(notifyInlineObservers, 1000);
150
151 int main(int argc, char** argv) {
152   gflags::ParseCommandLineFlags(&argc, &argv, true);
153   folly::runBenchmarks();
154   return 0;
155 }