Skip to content

Commit

Permalink
Merge pull request #1350 from benjchristensen/perf-tests
Browse files Browse the repository at this point in the history
Baseline Performance Tests
  • Loading branch information
benjchristensen committed Jun 11, 2014
2 parents 057db88 + 935b635 commit 4f32c82
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 4 deletions.
36 changes: 36 additions & 0 deletions rxjava-core/src/perf/java/rx/usecases/PerfBaseline.java
Original file line number Diff line number Diff line change
@@ -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<Integer> iterator = input.iterable.iterator();
while (iterator.hasNext()) {
input.observer.onNext(iterator.next());
}
}
}
14 changes: 13 additions & 1 deletion rxjava-core/src/perf/java/rx/usecases/PerfObserveOn.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}
18 changes: 15 additions & 3 deletions rxjava-core/src/perf/java/rx/usecases/PerfTransforms.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer, Integer>() {

@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<Integer, String>() {

@Override
Expand All @@ -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<Integer, Observable<Integer>>() {

@Override
Expand Down
30 changes: 30 additions & 0 deletions rxjava-core/src/perf/java/rx/usecases/UseCaseInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package rx.usecases;

import java.util.Iterator;
import java.util.concurrent.CountDownLatch;

import org.openjdk.jmh.annotations.Param;
Expand All @@ -36,6 +37,7 @@ public class UseCaseInput {
@Param({ "1", "1024" })
public int size;

public Iterable<Integer> iterable;
public Observable<Integer> observable;
public Observer<Integer> observer;

Expand All @@ -52,6 +54,34 @@ public void call(Subscriber<? super Integer> o) {
o.onCompleted();
}
});

iterable = new Iterable<Integer>() {

@Override
public Iterator<Integer> iterator() {
return new Iterator<Integer>() {

int i=0;

@Override
public boolean hasNext() {
return i < size;
}

@Override
public Integer next() {
return i++;
}

@Override
public void remove() {

}

};
}

};

latch = new CountDownLatch(1);

Expand Down

0 comments on commit 4f32c82

Please sign in to comment.