Debian Codegraph @

src/jmh/java/io/reactivex/rxjava3/core/RangePerf.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.concurrent.TimeUnit;
17
18 import org.openjdk.jmh.annotations.*;
19 import org.openjdk.jmh.infra.Blackhole;
20
21 import io.reactivex.rxjava3.internal.schedulers.SingleScheduler;
22 import io.reactivex.rxjava3.schedulers.Schedulers;
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 RangePerf {
31 @Param({ "1", "1000", "1000000" })
32 public int times;
33
34 Flowable<Integer> range;
35
36 Flowable<Integer> rangeAsync;
37
38 Flowable<Integer> rangeAsyncPipeline;
39
40 @Setup
41 public void setup() {
42 range = Flowable.range(1, times);
43
44 rangeAsync = range.observeOn(Schedulers.single());
45
46 rangeAsyncPipeline = range.subscribeOn(new SingleScheduler()).observeOn(Schedulers.single());
47 }
48
49 @Benchmark
50 public Object rangeSync(Blackhole bh) {
51 PerfSubscriber lo = new PerfSubscriber(bh);
52
53 range.subscribe(lo);
54
55 return lo;
56 }
57
58 // @Benchmark
59 public void rangeAsync(Blackhole bh) throws Exception {
60 PerfSubscriber lo = new PerfSubscriber(bh);
61
62 rangeAsync.subscribe(lo);
63
64 if (times == 1) {
65 while (lo.latch.getCount() != 0) { }
66 } else {
67 lo.latch.await();
68 }
69 }
70
71 // @Benchmark
72 public void rangePipeline(Blackhole bh) throws Exception {
73 PerfSubscriber lo = new PerfSubscriber(bh);
74
75 rangeAsyncPipeline.subscribe(lo);
76
77 if (times == 1) {
78 while (lo.latch.getCount() != 0) { }
79 } else {
80 lo.latch.await();
81 }
82 }
83
84 }