|
| 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; |
| 15 | + |
| 16 | +import java.util.concurrent.*; |
| 17 | + |
| 18 | +import org.openjdk.jmh.annotations.*; |
| 19 | + |
| 20 | +import io.reactivex.functions.*; |
| 21 | +import io.reactivex.internal.functions.Functions; |
| 22 | +import io.reactivex.schedulers.Schedulers; |
| 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) |
| 29 | +@State(Scope.Thread) |
| 30 | +@AuxCounters |
| 31 | +public class TakeUntilPerf implements Consumer<Integer> { |
| 32 | + |
| 33 | + public volatile int items; |
| 34 | + |
| 35 | + static final int count = 10000; |
| 36 | + |
| 37 | + Flowable<Integer> flowable; |
| 38 | + |
| 39 | + Observable<Integer> observable; |
| 40 | + |
| 41 | + @Override |
| 42 | + public void accept(Integer t) throws Exception { |
| 43 | + items++; |
| 44 | + } |
| 45 | + |
| 46 | + @Setup |
| 47 | + public void setup() { |
| 48 | + |
| 49 | + flowable = Flowable.range(1, 1000 * 1000).takeUntil(Flowable.fromCallable(new Callable<Object>() { |
| 50 | + @Override |
| 51 | + public Object call() throws Exception { |
| 52 | + int c = count; |
| 53 | + while (items < c) { } |
| 54 | + return 1; |
| 55 | + } |
| 56 | + }).subscribeOn(Schedulers.single())); |
| 57 | + |
| 58 | + observable = Observable.range(1, 1000 * 1000).takeUntil(Observable.fromCallable(new Callable<Object>() { |
| 59 | + @Override |
| 60 | + public Object call() throws Exception { |
| 61 | + int c = count; |
| 62 | + while (items < c) { } |
| 63 | + return 1; |
| 64 | + } |
| 65 | + }).subscribeOn(Schedulers.single())); |
| 66 | + } |
| 67 | + |
| 68 | + @Benchmark |
| 69 | + public void flowable() { |
| 70 | + final CountDownLatch cdl = new CountDownLatch(1); |
| 71 | + |
| 72 | + flowable.subscribe(this, Functions.emptyConsumer(), new Action() { |
| 73 | + @Override |
| 74 | + public void run() throws Exception { |
| 75 | + cdl.countDown(); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + while (cdl.getCount() != 0) { } |
| 80 | + } |
| 81 | + |
| 82 | + @Benchmark |
| 83 | + public void observable() { |
| 84 | + final CountDownLatch cdl = new CountDownLatch(1); |
| 85 | + |
| 86 | + observable.subscribe(this, Functions.emptyConsumer(), new Action() { |
| 87 | + @Override |
| 88 | + public void run() throws Exception { |
| 89 | + cdl.countDown(); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + while (cdl.getCount() != 0) { } |
| 94 | + } |
| 95 | +} |
0 commit comments