diff --git a/rxjava-core/src/perf/java/rx/usecases/PerfBaseline.java b/rxjava-core/src/perf/java/rx/usecases/PerfBaseline.java new file mode 100644 index 0000000000..c349253f84 --- /dev/null +++ b/rxjava-core/src/perf/java/rx/usecases/PerfBaseline.java @@ -0,0 +1,36 @@ +package rx.usecases; + +import java.util.Iterator; + +import org.openjdk.jmh.annotations.GenerateMicroBenchmark; + +public class PerfBaseline { + + @GenerateMicroBenchmark + public void forLoopConsumption(UseCaseInput input) throws InterruptedException { + for (int i = 0; i < input.size; i++) { + input.observer.onNext(i); + } + } + + @GenerateMicroBenchmark + public void observableConsumption(UseCaseInput input) throws InterruptedException { + input.observable.subscribe(input.observer); + input.awaitCompletion(); + } + + @GenerateMicroBenchmark + public void iterableViaForLoopConsumption(UseCaseInput input) throws InterruptedException { + for (int i : input.iterable) { + input.observer.onNext(i); + } + } + + @GenerateMicroBenchmark + public void iterableViaHasNextConsumption(UseCaseInput input) throws InterruptedException { + Iterator iterator = input.iterable.iterator(); + while (iterator.hasNext()) { + input.observer.onNext(iterator.next()); + } + } +} diff --git a/rxjava-core/src/perf/java/rx/usecases/PerfObserveOn.java b/rxjava-core/src/perf/java/rx/usecases/PerfObserveOn.java index 3b6aa1d2ea..6200cae9d4 100644 --- a/rxjava-core/src/perf/java/rx/usecases/PerfObserveOn.java +++ b/rxjava-core/src/perf/java/rx/usecases/PerfObserveOn.java @@ -22,9 +22,21 @@ public class PerfObserveOn { @GenerateMicroBenchmark - public void observeOn(UseCaseInput input) throws InterruptedException { + public void observeOnComputation(UseCaseInput input) throws InterruptedException { input.observable.observeOn(Schedulers.computation()).subscribe(input.observer); input.awaitCompletion(); } + @GenerateMicroBenchmark + public void observeOnNewThread(UseCaseInput input) throws InterruptedException { + input.observable.observeOn(Schedulers.newThread()).subscribe(input.observer); + input.awaitCompletion(); + } + + @GenerateMicroBenchmark + public void observeOnImmediate(UseCaseInput input) throws InterruptedException { + input.observable.observeOn(Schedulers.immediate()).subscribe(input.observer); + input.awaitCompletion(); + } + } diff --git a/rxjava-core/src/perf/java/rx/usecases/PerfTransforms.java b/rxjava-core/src/perf/java/rx/usecases/PerfTransforms.java index 8b56734e5d..ee8150c3d0 100644 --- a/rxjava-core/src/perf/java/rx/usecases/PerfTransforms.java +++ b/rxjava-core/src/perf/java/rx/usecases/PerfTransforms.java @@ -19,12 +19,24 @@ import rx.Observable; import rx.functions.Func1; -import rx.schedulers.Schedulers; public class PerfTransforms { @GenerateMicroBenchmark - public void mapTransformation(UseCaseInput input) throws InterruptedException { + public void mapPassThru(UseCaseInput input) throws InterruptedException { + input.observable.map(new Func1() { + + @Override + public Integer call(Integer i) { + return i; + } + + }).subscribe(input.observer); + input.awaitCompletion(); + } + + @GenerateMicroBenchmark + public void mapIntStringInt(UseCaseInput input) throws InterruptedException { input.observable.map(new Func1() { @Override @@ -44,7 +56,7 @@ public Integer call(String i) { } @GenerateMicroBenchmark - public void flatMapTransforms(UseCaseInput input) throws InterruptedException { + public void flatMapInt(UseCaseInput input) throws InterruptedException { input.observable.flatMap(new Func1>() { @Override diff --git a/rxjava-core/src/perf/java/rx/usecases/UseCaseInput.java b/rxjava-core/src/perf/java/rx/usecases/UseCaseInput.java index c18bfcc0ed..b3b7958118 100644 --- a/rxjava-core/src/perf/java/rx/usecases/UseCaseInput.java +++ b/rxjava-core/src/perf/java/rx/usecases/UseCaseInput.java @@ -15,6 +15,7 @@ */ package rx.usecases; +import java.util.Iterator; import java.util.concurrent.CountDownLatch; import org.openjdk.jmh.annotations.Param; @@ -36,6 +37,7 @@ public class UseCaseInput { @Param({ "1", "1024" }) public int size; + public Iterable iterable; public Observable observable; public Observer observer; @@ -52,6 +54,34 @@ public void call(Subscriber o) { o.onCompleted(); } }); + + iterable = new Iterable() { + + @Override + public Iterator iterator() { + return new Iterator() { + + int i=0; + + @Override + public boolean hasNext() { + return i < size; + } + + @Override + public Integer next() { + return i++; + } + + @Override + public void remove() { + + } + + }; + } + + }; latch = new CountDownLatch(1);