| 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.Iterator; |
| 17 |
|
| 18 |
import org.openjdk.jmh.annotations.Setup; |
| 19 |
import org.openjdk.jmh.infra.Blackhole; |
| 20 |
import org.reactivestreams.*; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.internal.subscriptions.EmptySubscription; |
| 23 |
import io.reactivex.rxjava3.subscribers.DefaultSubscriber; |
| 24 |
|
| 25 |
/** |
| 26 |
* Exposes an Observable and Observer that increments n Integers and consumes them in a Blackhole. |
| 27 |
*/ |
| 28 |
public abstract class InputWithIncrementingInteger { |
| 29 |
final class DefaultSubscriberImpl extends DefaultSubscriber<Integer> { |
| 30 |
@Override |
| 31 |
public void onComplete() { |
| 32 |
|
| 33 |
} |
| 34 |
|
| 35 |
@Override |
| 36 |
public void onError(Throwable e) { |
| 37 |
|
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
public void onNext(Integer t) { |
| 42 |
bh.consume(t); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
static final class IncrementingIterable implements Iterable<Integer> { |
| 47 |
|
| 48 |
final class IncrementingIterator implements Iterator<Integer> { |
| 49 |
int i; |
| 50 |
|
| 51 |
@Override |
| 52 |
public boolean hasNext() { |
| 53 |
return i < size; |
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
public Integer next() { |
| 58 |
Blackhole.consumeCPU(10); |
| 59 |
return i++; |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
public void remove() { |
| 64 |
|
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
final int size; |
| 69 |
|
| 70 |
IncrementingIterable(int size) { |
| 71 |
this.size = size; |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public Iterator<Integer> iterator() { |
| 76 |
return new IncrementingIterator(); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
static final class IncrementingPublisher implements Publisher<Integer> { |
| 81 |
|
| 82 |
final int size; |
| 83 |
|
| 84 |
IncrementingPublisher(int size) { |
| 85 |
this.size = size; |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public void subscribe(Subscriber<? super Integer> s) { |
| 90 |
s.onSubscribe(EmptySubscription.INSTANCE); |
| 91 |
for (int i = 0; i < size; i++) { |
| 92 |
s.onNext(i); |
| 93 |
} |
| 94 |
s.onComplete(); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
public Iterable<Integer> iterable; |
| 99 |
public Flowable<Integer> flowable; |
| 100 |
public Flowable<Integer> firehose; |
| 101 |
public Blackhole bh; |
| 102 |
|
| 103 |
public abstract int getSize(); |
| 104 |
|
| 105 |
@Setup |
| 106 |
public void setup(final Blackhole bh) { |
| 107 |
this.bh = bh; |
| 108 |
final int size = getSize(); |
| 109 |
flowable = Flowable.range(0, size); |
| 110 |
|
| 111 |
firehose = Flowable.unsafeCreate(new IncrementingPublisher(size)); |
| 112 |
iterable = new IncrementingIterable(size); |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
public PerfSubscriber newLatchedObserver() { |
| 117 |
return new PerfSubscriber(bh); |
| 118 |
} |
| 119 |
|
| 120 |
public FlowableSubscriber<Integer> newSubscriber() { |
| 121 |
return new DefaultSubscriberImpl(); |
| 122 |
} |
| 123 |
|
| 124 |
} |