| 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.TimeUnit; |
| 17 |
|
| 18 |
import org.openjdk.jmh.annotations.*; |
| 19 |
import org.reactivestreams.Publisher; |
| 20 |
|
| 21 |
import io.reactivex.rxjava3.functions.Function; |
| 22 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 23 |
|
| 24 |
@BenchmarkMode(Mode.Throughput) |
| 25 |
@OutputTimeUnit(TimeUnit.SECONDS) |
| 26 |
@State(Scope.Thread) |
| 27 |
public class OperatorFlatMapPerf { |
| 28 |
|
| 29 |
@State(Scope.Thread) |
| 30 |
public static class Input extends InputWithIncrementingInteger { |
| 31 |
|
| 32 |
@Param({ "1", "1000", "1000000" }) |
| 33 |
public int size; |
| 34 |
|
| 35 |
@Override |
| 36 |
public int getSize() { |
| 37 |
return size; |
| 38 |
} |
| 39 |
|
| 40 |
} |
| 41 |
|
| 42 |
@Benchmark |
| 43 |
public void flatMapIntPassthruSync(Input input) { |
| 44 |
input.flowable.flatMap(new Function<Integer, Publisher<Integer>>() { |
| 45 |
@Override |
| 46 |
public Publisher<Integer> apply(Integer v) { |
| 47 |
return Flowable.just(v); |
| 48 |
} |
| 49 |
}).subscribe(input.newSubscriber()); |
| 50 |
} |
| 51 |
|
| 52 |
@Benchmark |
| 53 |
public void flatMapIntPassthruAsync(Input input) throws InterruptedException { |
| 54 |
PerfSubscriber latchedObserver = input.newLatchedObserver(); |
| 55 |
input.flowable.flatMap(new Function<Integer, Publisher<Integer>>() { |
| 56 |
@Override |
| 57 |
public Publisher<Integer> apply(Integer i) { |
| 58 |
return Flowable.just(i).subscribeOn(Schedulers.computation()); |
| 59 |
} |
| 60 |
}).subscribe(latchedObserver); |
| 61 |
if (input.size == 1) { |
| 62 |
while (latchedObserver.latch.getCount() != 0) { } |
| 63 |
} else { |
| 64 |
latchedObserver.latch.await(); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
@Benchmark |
| 69 |
public void flatMapTwoNestedSync(final Input input) { |
| 70 |
Flowable.range(1, 2).flatMap(new Function<Integer, Publisher<Integer>>() { |
| 71 |
@Override |
| 72 |
public Publisher<Integer> apply(Integer i) { |
| 73 |
return input.flowable; |
| 74 |
} |
| 75 |
}).subscribe(input.newSubscriber()); |
| 76 |
} |
| 77 |
|
| 78 |
// this runs out of memory currently |
| 79 |
// @Benchmark |
| 80 |
// public void flatMapTwoNestedAsync(final Input input) throws InterruptedException { |
| 81 |
// Observable.range(1, 2).flatMap(new Func1<Integer, Observable<Integer>>() { |
| 82 |
// |
| 83 |
// @Override |
| 84 |
// public Observable<Integer> call(Integer i) { |
| 85 |
// return input.observable.subscribeOn(Schedulers.computation()); |
| 86 |
// } |
| 87 |
// |
| 88 |
// }).subscribe(input.observer); |
| 89 |
// } |
| 90 |
|
| 91 |
} |