Skip to content

Commit fb4241d

Browse files
committed
Merge pull request #3635 from akarnokd/ZipPerf1x
1.x: zip performance measure
2 parents b7b71d1 + 2ee019b commit fb4241d

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ apply plugin: 'java'
1111
dependencies {
1212
testCompile 'junit:junit-dep:4.10'
1313
testCompile 'org.mockito:mockito-core:1.8.5'
14+
15+
perfCompile 'org.openjdk.jmh:jmh-core:1.11.3'
16+
perfCompile 'org.openjdk.jmh:jmh-generator-annprocess:1.11.3'
1417
}
1518

1619
javadoc {
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/**
2+
* Copyright 2014 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package rx.operators;
18+
19+
import java.util.Arrays;
20+
import java.util.concurrent.TimeUnit;
21+
22+
import org.openjdk.jmh.annotations.Benchmark;
23+
import org.openjdk.jmh.annotations.BenchmarkMode;
24+
import org.openjdk.jmh.annotations.Mode;
25+
import org.openjdk.jmh.annotations.OutputTimeUnit;
26+
import org.openjdk.jmh.annotations.Param;
27+
import org.openjdk.jmh.annotations.Scope;
28+
import org.openjdk.jmh.annotations.Setup;
29+
import org.openjdk.jmh.annotations.State;
30+
import org.openjdk.jmh.infra.Blackhole;
31+
32+
import rx.Observable;
33+
import rx.functions.Func2;
34+
import rx.jmh.LatchedObserver;
35+
import rx.schedulers.Schedulers;
36+
37+
/**
38+
* Benchmark the Zip operator.
39+
* <p>
40+
* gradlew benchmarks "-Pjmh=-f 1 -tu s -bm thrpt -wi 5 -i 5 -r 1 .*ZipPerf.*"
41+
* <p>
42+
* gradlew benchmarks "-Pjmh=-f 1 -tu ns -bm avgt -wi 5 -i 5 -r 1 .*ZipPerf.*"
43+
*/
44+
@BenchmarkMode(Mode.Throughput)
45+
@OutputTimeUnit(TimeUnit.SECONDS)
46+
@State(Scope.Thread)
47+
public class ZipPerf {
48+
49+
@Param({"1", "1000", "1000000"})
50+
public int firstLen;
51+
@Param({"1", "1000", "1000000"})
52+
public int secondLen;
53+
54+
Observable<Integer> baseline;
55+
56+
Observable<Integer> bothSync;
57+
Observable<Integer> firstSync;
58+
Observable<Integer> secondSync;
59+
Observable<Integer> bothAsync;
60+
61+
boolean small;
62+
63+
@Setup
64+
public void setup() {
65+
Integer[] array1 = new Integer[firstLen];
66+
Arrays.fill(array1, 777);
67+
Integer[] array2 = new Integer[secondLen];
68+
Arrays.fill(array2, 777);
69+
70+
baseline = Observable.from(firstLen < secondLen? array2 : array1);
71+
72+
Observable<Integer> o1 = Observable.from(array1);
73+
74+
Observable<Integer> o2 = Observable.from(array2);
75+
76+
Func2<Integer, Integer, Integer> plus = new Func2<Integer, Integer, Integer>() {
77+
@Override
78+
public Integer call(Integer a, Integer b) {
79+
return a + b;
80+
}
81+
};
82+
83+
bothSync = Observable.zip(o1, o2, plus);
84+
85+
firstSync = Observable.zip(o1, o2.subscribeOn(Schedulers.computation()), plus);
86+
87+
secondSync = Observable.zip(o1.subscribeOn(Schedulers.computation()), o2, plus);
88+
89+
bothAsync = Observable.zip(o1.subscribeOn(Schedulers.computation()), o2.subscribeOn(Schedulers.computation()), plus);
90+
91+
small = Math.min(firstLen, secondLen) < 100;
92+
}
93+
94+
@Benchmark
95+
public void baseline(Blackhole bh) {
96+
baseline.subscribe(new LatchedObserver<Integer>(bh));
97+
}
98+
99+
@Benchmark
100+
public void syncSync(Blackhole bh) {
101+
bothSync.subscribe(new LatchedObserver<Integer>(bh));
102+
}
103+
104+
@Benchmark
105+
public void syncAsync(Blackhole bh) throws Exception {
106+
LatchedObserver<Integer> o = new LatchedObserver<Integer>(bh);
107+
firstSync.subscribe(o);
108+
109+
if (small) {
110+
while (o.latch.getCount() != 0);
111+
} else {
112+
o.latch.await();
113+
}
114+
}
115+
116+
@Benchmark
117+
public void asyncSync(Blackhole bh) throws Exception {
118+
LatchedObserver<Integer> o = new LatchedObserver<Integer>(bh);
119+
secondSync.subscribe(o);
120+
121+
if (small) {
122+
while (o.latch.getCount() != 0);
123+
} else {
124+
o.latch.await();
125+
}
126+
}
127+
128+
@Benchmark
129+
public void asyncAsync(Blackhole bh) throws Exception {
130+
LatchedObserver<Integer> o = new LatchedObserver<Integer>(bh);
131+
bothAsync.subscribe(o);
132+
133+
if (small) {
134+
while (o.latch.getCount() != 0);
135+
} else {
136+
o.latch.await();
137+
}
138+
}
139+
140+
}

0 commit comments

Comments
 (0)