Debian Codegraph @

src/jmh/java/io/reactivex/rxjava3/core/ReducePerf.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.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.BiFunction;
23
24 @BenchmarkMode(Mode.Throughput)
25 @Warmup(iterations = 5)
26 @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
27 @OutputTimeUnit(TimeUnit.SECONDS)
28 @Fork(value = 1, jvmArgsAppend = { "-XX:MaxInlineLevel=20" })
29 @State(Scope.Thread)
30 public class ReducePerf implements BiFunction<Integer, Integer, Integer> {
31 @Param({ "1", "1000", "1000000" })
32 public int times;
33
34 Single<Integer> obsSingle;
35
36 Single<Integer> flowSingle;
37
38 Maybe<Integer> obsMaybe;
39
40 Maybe<Integer> flowMaybe;
41
42 @Override
43 public Integer apply(Integer t1, Integer t2) {
44 return t1 + t2;
45 }
46
47 @Setup
48 public void setup() {
49 Integer[] array = new Integer[times];
50 Arrays.fill(array, 777);
51
52 obsSingle = Observable.fromArray(array).reduce(0, this);
53
54 obsMaybe = Observable.fromArray(array).reduce(this);
55
56 flowSingle = Flowable.fromArray(array).reduce(0, this);
57
58 flowMaybe = Flowable.fromArray(array).reduce(this);
59 }
60
61 @Benchmark
62 public void obsSingle(Blackhole bh) {
63 obsSingle.subscribe(new PerfConsumer(bh));
64 }
65
66 @Benchmark
67 public void flowSingle(Blackhole bh) {
68 flowSingle.subscribe(new PerfConsumer(bh));
69 }
70
71 @Benchmark
72 public void obsMaybe(Blackhole bh) {
73 obsMaybe.subscribe(new PerfConsumer(bh));
74 }
75
76 @Benchmark
77 public void flowMaybe(Blackhole bh) {
78 flowMaybe.subscribe(new PerfConsumer(bh));
79 }
80 }