Skip to content

Commit 935b635

Browse files
Baseline Performance Tests
Start of suite of general performance tests for comparing overall changes.
1 parent fb1a806 commit 935b635

File tree

4 files changed

+94
-4
lines changed

4 files changed

+94
-4
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package rx.usecases;
2+
3+
import java.util.Iterator;
4+
5+
import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
6+
7+
public class PerfBaseline {
8+
9+
@GenerateMicroBenchmark
10+
public void forLoopConsumption(UseCaseInput input) throws InterruptedException {
11+
for (int i = 0; i < input.size; i++) {
12+
input.observer.onNext(i);
13+
}
14+
}
15+
16+
@GenerateMicroBenchmark
17+
public void observableConsumption(UseCaseInput input) throws InterruptedException {
18+
input.observable.subscribe(input.observer);
19+
input.awaitCompletion();
20+
}
21+
22+
@GenerateMicroBenchmark
23+
public void iterableViaForLoopConsumption(UseCaseInput input) throws InterruptedException {
24+
for (int i : input.iterable) {
25+
input.observer.onNext(i);
26+
}
27+
}
28+
29+
@GenerateMicroBenchmark
30+
public void iterableViaHasNextConsumption(UseCaseInput input) throws InterruptedException {
31+
Iterator<Integer> iterator = input.iterable.iterator();
32+
while (iterator.hasNext()) {
33+
input.observer.onNext(iterator.next());
34+
}
35+
}
36+
}

rxjava-core/src/perf/java/rx/usecases/PerfObserveOn.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,21 @@
2222
public class PerfObserveOn {
2323

2424
@GenerateMicroBenchmark
25-
public void observeOn(UseCaseInput input) throws InterruptedException {
25+
public void observeOnComputation(UseCaseInput input) throws InterruptedException {
2626
input.observable.observeOn(Schedulers.computation()).subscribe(input.observer);
2727
input.awaitCompletion();
2828
}
2929

30+
@GenerateMicroBenchmark
31+
public void observeOnNewThread(UseCaseInput input) throws InterruptedException {
32+
input.observable.observeOn(Schedulers.newThread()).subscribe(input.observer);
33+
input.awaitCompletion();
34+
}
35+
36+
@GenerateMicroBenchmark
37+
public void observeOnImmediate(UseCaseInput input) throws InterruptedException {
38+
input.observable.observeOn(Schedulers.immediate()).subscribe(input.observer);
39+
input.awaitCompletion();
40+
}
41+
3042
}

rxjava-core/src/perf/java/rx/usecases/PerfTransforms.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,24 @@
1919

2020
import rx.Observable;
2121
import rx.functions.Func1;
22-
import rx.schedulers.Schedulers;
2322

2423
public class PerfTransforms {
2524

2625
@GenerateMicroBenchmark
27-
public void mapTransformation(UseCaseInput input) throws InterruptedException {
26+
public void mapPassThru(UseCaseInput input) throws InterruptedException {
27+
input.observable.map(new Func1<Integer, Integer>() {
28+
29+
@Override
30+
public Integer call(Integer i) {
31+
return i;
32+
}
33+
34+
}).subscribe(input.observer);
35+
input.awaitCompletion();
36+
}
37+
38+
@GenerateMicroBenchmark
39+
public void mapIntStringInt(UseCaseInput input) throws InterruptedException {
2840
input.observable.map(new Func1<Integer, String>() {
2941

3042
@Override
@@ -44,7 +56,7 @@ public Integer call(String i) {
4456
}
4557

4658
@GenerateMicroBenchmark
47-
public void flatMapTransforms(UseCaseInput input) throws InterruptedException {
59+
public void flatMapInt(UseCaseInput input) throws InterruptedException {
4860
input.observable.flatMap(new Func1<Integer, Observable<Integer>>() {
4961

5062
@Override

rxjava-core/src/perf/java/rx/usecases/UseCaseInput.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package rx.usecases;
1717

18+
import java.util.Iterator;
1819
import java.util.concurrent.CountDownLatch;
1920

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

40+
public Iterable<Integer> iterable;
3941
public Observable<Integer> observable;
4042
public Observer<Integer> observer;
4143

@@ -52,6 +54,34 @@ public void call(Subscriber<? super Integer> o) {
5254
o.onCompleted();
5355
}
5456
});
57+
58+
iterable = new Iterable<Integer>() {
59+
60+
@Override
61+
public Iterator<Integer> iterator() {
62+
return new Iterator<Integer>() {
63+
64+
int i=0;
65+
66+
@Override
67+
public boolean hasNext() {
68+
return i < size;
69+
}
70+
71+
@Override
72+
public Integer next() {
73+
return i++;
74+
}
75+
76+
@Override
77+
public void remove() {
78+
79+
}
80+
81+
};
82+
}
83+
84+
};
5585

5686
latch = new CountDownLatch(1);
5787

0 commit comments

Comments
 (0)