Skip to content

Commit 40bd48b

Browse files
committed
Added the first of the perf tests
1 parent 4db5d8e commit 40bd48b

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

rxjava-core/build.gradle

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
apply plugin: 'maven'
22
apply plugin: 'osgi'
3+
apply plugin:'application'
34

45
sourceCompatibility = JavaVersion.VERSION_1_6
56
targetCompatibility = JavaVersion.VERSION_1_6
@@ -31,3 +32,14 @@ jar {
3132
}
3233
}
3334

35+
task time(type:JavaExec) {
36+
//sourceSets.metaClass.methods.each { println it }
37+
classpath = sourceSets.perf.runtimeClasspath
38+
group 'Application'
39+
description 'Execute the calipser benchmark timing of Rx'
40+
main 'rx.operators.ObservableBenchmark'
41+
// args '--print-config' // dump the caliper configuration before starting
42+
// args '--verbose' // uncomment to have caliper log what it's doing
43+
args '--trials=1' // only run the experiments once
44+
//args '--time-limit=0' // experiments never timeout
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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

Comments
 (0)