| 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.parallel; |
| 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 |
import org.reactivestreams.Publisher; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.flowables.GroupedFlowable; |
| 25 |
import io.reactivex.rxjava3.functions.Function; |
| 26 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 27 |
|
| 28 |
@BenchmarkMode(Mode.Throughput) |
| 29 |
@Warmup(iterations = 5) |
| 30 |
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) |
| 31 |
@Fork(value = 1, jvmArgsAppend = { "-XX:MaxInlineLevel=20" }) |
| 32 |
@OutputTimeUnit(TimeUnit.SECONDS) |
| 33 |
@State(Scope.Thread) |
| 34 |
public class ParallelPerf implements Function<Integer, Integer> { |
| 35 |
|
| 36 |
@Param({"10000"}) |
| 37 |
public int count; |
| 38 |
|
| 39 |
@Param({"1", "10", "100", "1000", "10000"}) |
| 40 |
public int compute; |
| 41 |
|
| 42 |
@Param({"1", "2", "3", "4"}) |
| 43 |
public int parallelism; |
| 44 |
|
| 45 |
Flowable<Integer> flatMap; |
| 46 |
|
| 47 |
Flowable<Integer> groupBy; |
| 48 |
|
| 49 |
Flowable<Integer> parallel; |
| 50 |
|
| 51 |
@Override |
| 52 |
public Integer apply(Integer t) { |
| 53 |
Blackhole.consumeCPU(compute); |
| 54 |
return t; |
| 55 |
} |
| 56 |
|
| 57 |
@Setup |
| 58 |
public void setup() { |
| 59 |
|
| 60 |
final int cpu = parallelism; |
| 61 |
|
| 62 |
Integer[] ints = new Integer[count]; |
| 63 |
Arrays.fill(ints, 777); |
| 64 |
|
| 65 |
Flowable<Integer> source = Flowable.fromArray(ints); |
| 66 |
|
| 67 |
flatMap = source.flatMap(new Function<Integer, Publisher<Integer>>() { |
| 68 |
@Override |
| 69 |
public Publisher<Integer> apply(Integer v) { |
| 70 |
return Flowable.just(v).subscribeOn(Schedulers.computation()) |
| 71 |
.map(ParallelPerf.this); |
| 72 |
} |
| 73 |
}, cpu); |
| 74 |
|
| 75 |
groupBy = source.groupBy(new Function<Integer, Integer>() { |
| 76 |
int i; |
| 77 |
@Override |
| 78 |
public Integer apply(Integer v) { |
| 79 |
return (i++) % cpu; |
| 80 |
} |
| 81 |
}) |
| 82 |
.flatMap(new Function<GroupedFlowable<Integer, Integer>, Publisher<Integer>>() { |
| 83 |
@Override |
| 84 |
public Publisher<Integer> apply(GroupedFlowable<Integer, Integer> g) { |
| 85 |
return g.observeOn(Schedulers.computation()).map(ParallelPerf.this); |
| 86 |
} |
| 87 |
}); |
| 88 |
|
| 89 |
parallel = source.parallel(cpu).runOn(Schedulers.computation()).map(this).sequential(); |
| 90 |
} |
| 91 |
|
| 92 |
void subscribe(Flowable<Integer> f, Blackhole bh) { |
| 93 |
PerfAsyncConsumer consumer = new PerfAsyncConsumer(bh); |
| 94 |
f.subscribe(consumer); |
| 95 |
consumer.await(count); |
| 96 |
} |
| 97 |
|
| 98 |
@Benchmark |
| 99 |
public void flatMap(Blackhole bh) { |
| 100 |
subscribe(flatMap, bh); |
| 101 |
} |
| 102 |
|
| 103 |
@Benchmark |
| 104 |
public void groupBy(Blackhole bh) { |
| 105 |
subscribe(groupBy, bh); |
| 106 |
} |
| 107 |
|
| 108 |
@Benchmark |
| 109 |
public void parallel(Blackhole bh) { |
| 110 |
subscribe(parallel, bh); |
| 111 |
} |
| 112 |
} |