| 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.Arrays; |
| 17 |
import java.util.concurrent.TimeUnit; |
| 18 |
|
| 19 |
import org.openjdk.jmh.annotations.*; |
| 20 |
import org.openjdk.jmh.infra.Blackhole; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.functions.Function; |
| 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 ObservableFlatMapPerf { |
| 31 |
@Param({ "1", "10", "100", "1000", "10000", "100000", "1000000" }) |
| 32 |
public int count; |
| 33 |
|
| 34 |
Observable<Integer> source; |
| 35 |
|
| 36 |
@Setup |
| 37 |
public void setup() { |
| 38 |
int d = 1000000 / count; |
| 39 |
|
| 40 |
Integer[] mainArray = new Integer[count]; |
| 41 |
Integer[] innerArray = new Integer[d]; |
| 42 |
|
| 43 |
Arrays.fill(mainArray, 777); |
| 44 |
Arrays.fill(innerArray, 777); |
| 45 |
|
| 46 |
Observable<Integer> outer = Observable.fromArray(mainArray); |
| 47 |
final Observable<Integer> inner = Observable.fromArray(innerArray); |
| 48 |
|
| 49 |
source = outer.flatMap(new Function<Integer, Observable<Integer>>() { |
| 50 |
@Override |
| 51 |
public Observable<Integer> apply(Integer t) { |
| 52 |
return inner; |
| 53 |
} |
| 54 |
}); |
| 55 |
} |
| 56 |
|
| 57 |
@Benchmark |
| 58 |
public void flatMapXRange(Blackhole bh) { |
| 59 |
source.subscribe(new PerfObserver(bh)); |
| 60 |
} |
| 61 |
} |