forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScalarJustPerf.java
More file actions
197 lines (167 loc) · 5.89 KB
/
ScalarJustPerf.java
File metadata and controls
197 lines (167 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* Copyright 2014 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import rx.functions.Func1;
import rx.jmh.LatchedObserver;
import rx.schedulers.Schedulers;
/**
* Benchmark the cost of just and its various optimizations.
* <p>
* gradlew benchmarks "-Pjmh=-f 1 -tu s -bm thrpt -wi 5 -i 5 -r 1 .*ScalarJustPerf.*"
* <p>
* gradlew benchmarks "-Pjmh=-f 1 -tu ns -bm avgt -wi 5 -i 5 -r 1 .*ScalarJustPerf.*"
*/
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
@State(Scope.Thread)
public class ScalarJustPerf {
/** A subscriber without a CountDownLatch; use it for synchronous testing only. */
static final class PlainSubscriber extends Subscriber<Integer> {
final Blackhole bh;
public PlainSubscriber(Blackhole bh) {
this.bh = bh;
}
@Override
public void onNext(Integer t) {
bh.consume(t);
}
@Override
public void onError(Throwable e) {
bh.consume(e);
}
@Override
public void onCompleted() {
bh.consume(false);
}
}
/** This is a simple just. */
Observable<Integer> simple;
/**
* This is a simple just observed on the computation scheduler.
* The current computation scheduler supports direct scheduling and should have
* lower overhead than a regular createWorker-use-unsubscribe.
*/
Observable<Integer> observeOn;
/** This is a simple just observed on the IO thread. */
Observable<Integer> observeOnIO;
/**
* This is a simple just subscribed to on the computation scheduler.
* In theory, for non-backpressured just(), this should be the
* same as observeOn.
*/
Observable<Integer> subscribeOn;
/** This is a simple just subscribed to on the IO scheduler. */
Observable<Integer> subscribeOnIO;
/** This is a just mapped to itself which should skip the operator flatMap completely. */
Observable<Integer> justFlatMapJust;
/**
* This is a just mapped to a range of 2 elements; it tests the case where the inner
* Observable isn't a just().
*/
Observable<Integer> justFlatMapRange;
@Setup
public void setup() {
simple = Observable.just(1);
observeOn = simple.observeOn(Schedulers.computation());
observeOnIO = simple.observeOn(Schedulers.io());
subscribeOn = simple.subscribeOn(Schedulers.computation());
subscribeOnIO = simple.subscribeOn(Schedulers.io());
justFlatMapJust = simple.flatMap(new Func1<Integer, Observable<Integer>>() {
@Override
public Observable<Integer> call(Integer v) {
return Observable.just(v);
}
});
justFlatMapRange = simple.flatMap(new Func1<Integer, Observable<Integer>>() {
@Override
public Observable<Integer> call(Integer v) {
return Observable.range(v, 2);
}
});
}
/**
* Common routine to create a latched observer, subscribe it to the
* given source and spin-wait for its completion.
* <p>Don't use this with long sources. The spin-wait is there
* to avoid operating-system level scheduling-wakeup granularity problems with
* short sources.
* @param bh the black hole to sink values and prevent dead code elimination
* @param source the source observable to observe
*/
void runAsync(Blackhole bh, Observable<Integer> source) {
LatchedObserver<Integer> lo = new LatchedObserver<Integer>(bh);
source.subscribe(lo);
while (lo.latch.getCount() != 0L);
}
@Benchmark
public void simple(Blackhole bh) {
PlainSubscriber s = new PlainSubscriber(bh);
simple.subscribe(s);
}
@Benchmark
public void simpleEscape(Blackhole bh) {
PlainSubscriber s = new PlainSubscriber(bh);
bh.consume(s);
simple.subscribe(s);
}
@Benchmark
public Object simpleEscapeAll(Blackhole bh) {
PlainSubscriber s = new PlainSubscriber(bh);
bh.consume(s);
return simple.subscribe(s);
}
@Benchmark
public void observeOn(Blackhole bh) {
runAsync(bh, observeOn);
}
@Benchmark
public void observeOnIO(Blackhole bh) {
runAsync(bh, observeOnIO);
}
@Benchmark
public void subscribeOn(Blackhole bh) {
runAsync(bh, subscribeOn);
}
@Benchmark
public void subscribeOnIO(Blackhole bh) {
runAsync(bh, subscribeOnIO);
}
@Benchmark
public void justFlatMapJust(Blackhole bh) {
PlainSubscriber s = new PlainSubscriber(bh);
justFlatMapJust.subscribe(s);
}
@Benchmark
public void justFlatMapJustEscape(Blackhole bh) {
PlainSubscriber s = new PlainSubscriber(bh);
bh.consume(s);
justFlatMapJust.subscribe(s);
}
@Benchmark
public void justFlatMapRange(Blackhole bh) {
PlainSubscriber s = new PlainSubscriber(bh);
justFlatMapRange.subscribe(s);
}
@Benchmark
public void justFlatMapRangeEscape(Blackhole bh) {
PlainSubscriber s = new PlainSubscriber(bh);
bh.consume(s);
justFlatMapRange.subscribe(s);
}
}