|
| 1 | +package rx.operators; |
| 2 | + |
| 3 | +import java.util.concurrent.CountDownLatch; |
| 4 | +import java.util.concurrent.atomic.AtomicInteger; |
| 5 | + |
| 6 | +import rx.Observable; |
| 7 | +import rx.Observable.OnSubscribe; |
| 8 | +import rx.Observer; |
| 9 | +import rx.util.functions.Func1; |
| 10 | + |
| 11 | +import com.google.caliper.Benchmark; |
| 12 | +import com.google.caliper.runner.CaliperMain; |
| 13 | + |
| 14 | +public class ObservableBenchmark extends Benchmark { |
| 15 | + public void timeBaseline(int reps) { |
| 16 | + for (int i = 0; i < reps; i++) { |
| 17 | + observableOfInts.subscribe(newObserver()); |
| 18 | + } |
| 19 | + awaitAllObservers(); |
| 20 | + } |
| 21 | + |
| 22 | + public int timeMapIterate(long reps) { |
| 23 | + int x = 0; |
| 24 | + for (int i = 0; i < reps; i++) { |
| 25 | + for (int j = 0; j < intValues.length; j++) { |
| 26 | + // use hash code to make sure the JIT doesn't optimize too much and remove all of |
| 27 | + // our code. |
| 28 | + x |= ident.call(intValues[j]).hashCode(); |
| 29 | + } |
| 30 | + } |
| 31 | + return x; |
| 32 | + } |
| 33 | + |
| 34 | + public void timeMap(long reps) { |
| 35 | + timeOperator(reps, new OperatorMap<Integer, Object>(ident)); |
| 36 | + } |
| 37 | + |
| 38 | + /************************************************************************** |
| 39 | + * Below is internal stuff to avoid object allocation and time overhead of anything that isn't |
| 40 | + * being tested. |
| 41 | + **************************************************************************/ |
| 42 | + |
| 43 | + public static void main(String[] args) { |
| 44 | + CaliperMain.main(ObservableBenchmark.class, args); |
| 45 | + } |
| 46 | + |
| 47 | + private void timeOperator(long reps, Operator<Object, Integer> op) { |
| 48 | + for (int i = 0; i < reps; i++) { |
| 49 | + observableOfInts.lift(op).subscribe(newObserver()); |
| 50 | + } |
| 51 | + awaitAllObservers(); |
| 52 | + } |
| 53 | + |
| 54 | + private final static AtomicInteger outstanding = new AtomicInteger(0); |
| 55 | + private final static CountDownLatch latch = new CountDownLatch(1); |
| 56 | + |
| 57 | + private static <T> Observer<T> newObserver() { |
| 58 | + outstanding.incrementAndGet(); |
| 59 | + return new Observer<T>() { |
| 60 | + @Override |
| 61 | + public void onCompleted() { |
| 62 | + int left = outstanding.decrementAndGet(); |
| 63 | + if (left == 0) { |
| 64 | + latch.countDown(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void onError(Throwable e) { |
| 70 | + int left = outstanding.decrementAndGet(); |
| 71 | + if (left == 0) { |
| 72 | + latch.countDown(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void onNext(T t) { |
| 78 | + // do nothing |
| 79 | + } |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + private static void awaitAllObservers() { |
| 84 | + try { |
| 85 | + latch.await(); |
| 86 | + } catch (InterruptedException e) { |
| 87 | + return; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private static final Integer[] intValues = new Integer[1000]; |
| 92 | + static { |
| 93 | + for (int i = 0; i < intValues.length; i++) { |
| 94 | + intValues[i] = i; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private static final Observable<Integer> observableOfInts = Observable.create(new OnSubscribe<Integer>() { |
| 99 | + @Override |
| 100 | + public void call(Observer<? super Integer> o) { |
| 101 | + for (int i = 0; i < intValues.length; i++) { |
| 102 | + if (o.isUnsubscribed()) |
| 103 | + return; |
| 104 | + o.onNext(intValues[i]); |
| 105 | + } |
| 106 | + o.onCompleted(); |
| 107 | + } |
| 108 | + }); |
| 109 | + private static final Func1<Integer, Object> ident = new Func1<Integer, Object>() { |
| 110 | + @Override |
| 111 | + public Object call(Integer t) { |
| 112 | + return t; |
| 113 | + } |
| 114 | + }; |
| 115 | +} |
0 commit comments