Debian Codegraph @

src/jmh/java/io/reactivex/rxjava3/core/OperatorMergePerf.java

raw
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.*;
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 import io.reactivex.rxjava3.schedulers.Schedulers;
24
25 @BenchmarkMode(Mode.Throughput)
26 @OutputTimeUnit(TimeUnit.SECONDS)
27 public class OperatorMergePerf {
28
29 // flatMap
30 @Benchmark
31 public void oneStreamOfNthatMergesIn1(final InputMillion input) throws InterruptedException {
32 Flowable<Flowable<Integer>> os = Flowable.range(1, input.size)
33 .map(new Function<Integer, Flowable<Integer>>() {
34 @Override
35 public Flowable<Integer> apply(Integer v) {
36 return Flowable.just(v);
37 }
38 });
39 PerfSubscriber o = input.newLatchedObserver();
40 Flowable.merge(os).subscribe(o);
41
42 if (input.size == 1) {
43 while (o.latch.getCount() != 0) { }
44 } else {
45 o.latch.await();
46 }
47 }
48
49 // flatMap
50 @Benchmark
51 public void merge1SyncStreamOfN(final InputMillion input) throws InterruptedException {
52 Flowable<Flowable<Integer>> os = Flowable.just(1).map(new Function<Integer, Flowable<Integer>>() {
53 @Override
54 public Flowable<Integer> apply(Integer i) {
55 return Flowable.range(0, input.size);
56 }
57 });
58 PerfSubscriber o = input.newLatchedObserver();
59 Flowable.merge(os).subscribe(o);
60
61 if (input.size == 1) {
62 while (o.latch.getCount() != 0) { }
63 } else {
64 o.latch.await();
65 }
66 }
67
68 @Benchmark
69 public void mergeNSyncStreamsOfN(final InputThousand input) throws InterruptedException {
70 Flowable<Flowable<Integer>> os = input.flowable.map(new Function<Integer, Flowable<Integer>>() {
71 @Override
72 public Flowable<Integer> apply(Integer i) {
73 return Flowable.range(0, input.size);
74 }
75 });
76 PerfSubscriber o = input.newLatchedObserver();
77 Flowable.merge(os).subscribe(o);
78 if (input.size == 1) {
79 while (o.latch.getCount() != 0) { }
80 } else {
81 o.latch.await();
82 }
83 }
84
85 @Benchmark
86 public void mergeNAsyncStreamsOfN(final InputThousand input) throws InterruptedException {
87 Flowable<Flowable<Integer>> os = input.flowable.map(new Function<Integer, Flowable<Integer>>() {
88 @Override
89 public Flowable<Integer> apply(Integer i) {
90 return Flowable.range(0, input.size).subscribeOn(Schedulers.computation());
91 }
92 });
93 PerfSubscriber o = input.newLatchedObserver();
94 Flowable.merge(os).subscribe(o);
95 if (input.size == 1) {
96 while (o.latch.getCount() != 0) { }
97 } else {
98 o.latch.await();
99 }
100 }
101
102 @Benchmark
103 public void mergeTwoAsyncStreamsOfN(final InputThousand input) throws InterruptedException {
104 PerfSubscriber o = input.newLatchedObserver();
105 Flowable<Integer> ob = Flowable.range(0, input.size).subscribeOn(Schedulers.computation());
106 Flowable.merge(ob, ob).subscribe(o);
107 if (input.size == 1) {
108 while (o.latch.getCount() != 0) { }
109 } else {
110 o.latch.await();
111 }
112 }
113
114 @Benchmark
115 public void mergeNSyncStreamsOf1(final InputForMergeN input) throws InterruptedException {
116 PerfSubscriber o = input.newLatchedObserver();
117 Flowable.merge(input.observables).subscribe(o);
118 if (input.size == 1) {
119 while (o.latch.getCount() != 0) { }
120 } else {
121 o.latch.await();
122 }
123 }
124
125 @State(Scope.Thread)
126 public static class InputForMergeN {
127 @Param({ "1", "100", "1000" })
128 // @Param({ "1000" })
129 public int size;
130
131 private Blackhole bh;
132 List<Flowable<Integer>> observables;
133
134 @Setup
135 public void setup(final Blackhole bh) {
136 this.bh = bh;
137 observables = new ArrayList<>();
138 for (int i = 0; i < size; i++) {
139 observables.add(Flowable.just(i));
140 }
141 }
142
143 public PerfSubscriber newLatchedObserver() {
144 return new PerfSubscriber(bh);
145 }
146 }
147
148 @State(Scope.Thread)
149 public static class InputMillion extends InputWithIncrementingInteger {
150
151 @Param({ "1", "1000", "1000000" })
152 // @Param({ "1000" })
153 public int size;
154
155 @Override
156 public int getSize() {
157 return size;
158 }
159
160 }
161
162 @State(Scope.Thread)
163 public static class InputThousand extends InputWithIncrementingInteger {
164
165 @Param({ "1", "1000" })
166 // @Param({ "1000" })
167 public int size;
168
169 @Override
170 public int getSize() {
171 return size;
172 }
173
174 }
175 }