| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.core; |
| 15 |
|
| 16 |
import java.util.concurrent.*; |
| 17 |
|
| 18 |
import org.openjdk.jmh.annotations.*; |
| 19 |
import org.openjdk.jmh.infra.Blackhole; |
| 20 |
|
| 21 |
import io.reactivex.rxjava3.internal.schedulers.SingleScheduler; |
| 22 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 23 |
|
| 24 |
@BenchmarkMode(Mode.Throughput) |
| 25 |
@Warmup(iterations = 5) |
| 26 |
@Measurement(iterations = 5, time = 5, timeUnit = TimeUnit.SECONDS) |
| 27 |
@OutputTimeUnit(TimeUnit.SECONDS) |
| 28 |
@Fork(value = 1) |
| 29 |
@State(Scope.Thread) |
| 30 |
public class CallableAsyncPerf { |
| 31 |
|
| 32 |
Flowable<Integer> subscribeOnFlowable; |
| 33 |
|
| 34 |
Flowable<Integer> observeOnFlowable; |
| 35 |
|
| 36 |
Flowable<Integer> pipelineFlowable; |
| 37 |
|
| 38 |
Observable<Integer> subscribeOnObservable; |
| 39 |
|
| 40 |
Observable<Integer> observeOnObservable; |
| 41 |
|
| 42 |
Observable<Integer> pipelineObservable; |
| 43 |
|
| 44 |
Single<Integer> observeOnSingle; |
| 45 |
|
| 46 |
Single<Integer> subscribeOnSingle; |
| 47 |
|
| 48 |
Single<Integer> pipelineSingle; |
| 49 |
|
| 50 |
Completable observeOnCompletable; |
| 51 |
|
| 52 |
Completable subscribeOnCompletable; |
| 53 |
|
| 54 |
Completable pipelineCompletable; |
| 55 |
|
| 56 |
Maybe<Integer> observeOnMaybe; |
| 57 |
|
| 58 |
Maybe<Integer> subscribeOnMaybe; |
| 59 |
|
| 60 |
Maybe<Integer> pipelineMaybe; |
| 61 |
|
| 62 |
@Setup |
| 63 |
public void setup() { |
| 64 |
|
| 65 |
Scheduler s = Schedulers.single(); |
| 66 |
|
| 67 |
Scheduler s2 = new SingleScheduler(); |
| 68 |
|
| 69 |
Callable<Integer> c = new Callable<Integer>() { |
| 70 |
@Override |
| 71 |
public Integer call() { |
| 72 |
return 1; |
| 73 |
} |
| 74 |
}; |
| 75 |
|
| 76 |
subscribeOnFlowable = Flowable.fromCallable(c).subscribeOn(s); |
| 77 |
|
| 78 |
observeOnFlowable = Flowable.fromCallable(c).observeOn(s); |
| 79 |
|
| 80 |
pipelineFlowable = Flowable.fromCallable(c).subscribeOn(s).observeOn(s2); |
| 81 |
|
| 82 |
// ---- |
| 83 |
|
| 84 |
subscribeOnObservable = Observable.fromCallable(c).subscribeOn(s); |
| 85 |
|
| 86 |
observeOnObservable = Observable.fromCallable(c).observeOn(s); |
| 87 |
|
| 88 |
pipelineObservable = Observable.fromCallable(c).subscribeOn(s).observeOn(s2); |
| 89 |
|
| 90 |
// ---- |
| 91 |
|
| 92 |
observeOnSingle = Single.fromCallable(c).observeOn(s); |
| 93 |
|
| 94 |
subscribeOnSingle = Single.fromCallable(c).subscribeOn(s); |
| 95 |
|
| 96 |
pipelineSingle = Single.fromCallable(c).subscribeOn(s).observeOn(s2); |
| 97 |
|
| 98 |
// ---- |
| 99 |
|
| 100 |
observeOnCompletable = Completable.fromCallable(c).observeOn(s); |
| 101 |
|
| 102 |
subscribeOnCompletable = Completable.fromCallable(c).subscribeOn(s); |
| 103 |
|
| 104 |
pipelineCompletable = Completable.fromCallable(c).subscribeOn(s).observeOn(s2); |
| 105 |
|
| 106 |
// ---- |
| 107 |
|
| 108 |
observeOnMaybe = Maybe.fromCallable(c).observeOn(s); |
| 109 |
|
| 110 |
subscribeOnMaybe = Maybe.fromCallable(c).subscribeOn(s); |
| 111 |
|
| 112 |
pipelineMaybe = Maybe.fromCallable(c).subscribeOn(s).observeOn(s2); |
| 113 |
} |
| 114 |
|
| 115 |
@Benchmark |
| 116 |
public void subscribeOnFlowable(Blackhole bh) { |
| 117 |
subscribeOnFlowable.subscribeWith(new PerfAsyncConsumer(bh)).await(1); |
| 118 |
} |
| 119 |
|
| 120 |
@Benchmark |
| 121 |
public void observeOnFlowable(Blackhole bh) { |
| 122 |
observeOnFlowable.subscribeWith(new PerfAsyncConsumer(bh)).await(1); |
| 123 |
} |
| 124 |
|
| 125 |
@Benchmark |
| 126 |
public void pipelineFlowable(Blackhole bh) { |
| 127 |
pipelineFlowable.subscribeWith(new PerfAsyncConsumer(bh)).await(1); |
| 128 |
} |
| 129 |
|
| 130 |
@Benchmark |
| 131 |
public void subscribeOnObservable(Blackhole bh) { |
| 132 |
subscribeOnObservable.subscribeWith(new PerfAsyncConsumer(bh)).await(1); |
| 133 |
} |
| 134 |
|
| 135 |
@Benchmark |
| 136 |
public void observeOnObservable(Blackhole bh) { |
| 137 |
observeOnObservable.subscribeWith(new PerfAsyncConsumer(bh)).await(1); |
| 138 |
} |
| 139 |
|
| 140 |
@Benchmark |
| 141 |
public void pipelineObservable(Blackhole bh) { |
| 142 |
pipelineObservable.subscribeWith(new PerfAsyncConsumer(bh)).await(1); |
| 143 |
} |
| 144 |
|
| 145 |
@Benchmark |
| 146 |
public void observeOnSingle(Blackhole bh) { |
| 147 |
observeOnSingle.subscribeWith(new PerfAsyncConsumer(bh)).await(1); |
| 148 |
} |
| 149 |
|
| 150 |
@Benchmark |
| 151 |
public void subscribeOnSingle(Blackhole bh) { |
| 152 |
subscribeOnSingle.subscribeWith(new PerfAsyncConsumer(bh)).await(1); |
| 153 |
} |
| 154 |
|
| 155 |
@Benchmark |
| 156 |
public void pipelineSingle(Blackhole bh) { |
| 157 |
pipelineSingle.subscribeWith(new PerfAsyncConsumer(bh)).await(1); |
| 158 |
} |
| 159 |
|
| 160 |
@Benchmark |
| 161 |
public void observeOnCompletable(Blackhole bh) { |
| 162 |
observeOnCompletable.subscribeWith(new PerfAsyncConsumer(bh)).await(1); |
| 163 |
} |
| 164 |
|
| 165 |
@Benchmark |
| 166 |
public void subscribeOnCompletable(Blackhole bh) { |
| 167 |
subscribeOnCompletable.subscribeWith(new PerfAsyncConsumer(bh)).await(1); |
| 168 |
} |
| 169 |
|
| 170 |
@Benchmark |
| 171 |
public void pipelineCompletable(Blackhole bh) { |
| 172 |
pipelineCompletable.subscribeWith(new PerfAsyncConsumer(bh)).await(1); |
| 173 |
} |
| 174 |
|
| 175 |
@Benchmark |
| 176 |
public void observeOnMaybe(Blackhole bh) { |
| 177 |
observeOnMaybe.subscribeWith(new PerfAsyncConsumer(bh)).await(1); |
| 178 |
} |
| 179 |
|
| 180 |
@Benchmark |
| 181 |
public void subscribeOnMaybe(Blackhole bh) { |
| 182 |
subscribeOnMaybe.subscribeWith(new PerfAsyncConsumer(bh)).await(1); |
| 183 |
} |
| 184 |
|
| 185 |
@Benchmark |
| 186 |
public void pipelineMaybe(Blackhole bh) { |
| 187 |
pipelineMaybe.subscribeWith(new PerfAsyncConsumer(bh)).await(1); |
| 188 |
} |
| 189 |
|
| 190 |
} |