Skip to content

Commit a9e7975

Browse files
Merge pull request #1030 from daschl/subj-bench
Benchmarking: Add JMH benchmark for ReplaySubject.
2 parents 33819b5 + 329a093 commit a9e7975

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed

rxjava-core/src/perf/java/rx/jmh/Baseline.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
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+
*/
116
package rx.jmh;
217

318
import org.openjdk.jmh.annotations.GenerateMicroBenchmark;

rxjava-core/src/perf/java/rx/operators/OperatorMapPerf.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
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+
*/
116
package rx.operators;
217

318
import java.util.concurrent.CountDownLatch;

rxjava-core/src/perf/java/rx/operators/OperatorSerializePerf.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
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+
*/
116
package rx.operators;
217

318
import java.util.concurrent.CountDownLatch;
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
package rx.subjects;
17+
18+
import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
19+
import org.openjdk.jmh.annotations.Param;
20+
import org.openjdk.jmh.annotations.Scope;
21+
import org.openjdk.jmh.annotations.State;
22+
import org.openjdk.jmh.logic.BlackHole;
23+
import rx.Observer;
24+
25+
import java.util.concurrent.CountDownLatch;
26+
import java.util.concurrent.atomic.AtomicLong;
27+
28+
/**
29+
* Benchmarks the {@link ReplaySubject}.
30+
*/
31+
public class ReplaySubjectPerf {
32+
33+
@State(Scope.Thread)
34+
public static class Input {
35+
@Param({ "1", "512", "1024", "1048576" })
36+
public int nextRuns;
37+
}
38+
39+
@GenerateMicroBenchmark
40+
public void subscribeBeforeEvents(final Input input, final BlackHole bh) throws Exception {
41+
ReplaySubject<Object> subject = ReplaySubject.create();
42+
final CountDownLatch latch = new CountDownLatch(1);
43+
final AtomicLong sum = new AtomicLong();
44+
45+
subject.subscribe(new Observer<Object>() {
46+
@Override
47+
public void onCompleted() {
48+
latch.countDown();
49+
}
50+
51+
@Override
52+
public void onError(Throwable e) {
53+
}
54+
55+
@Override
56+
public void onNext(Object o) {
57+
sum.incrementAndGet();
58+
}
59+
});
60+
for (int i = 0; i < input.nextRuns; i++) {
61+
subject.onNext("Response");
62+
}
63+
subject.onCompleted();
64+
latch.await();
65+
bh.consume(sum);
66+
}
67+
68+
@GenerateMicroBenchmark
69+
public void subscribeAfterEvents(final Input input, final BlackHole bh) throws Exception {
70+
ReplaySubject<Object> subject = ReplaySubject.create();
71+
final CountDownLatch latch = new CountDownLatch(1);
72+
final AtomicLong sum = new AtomicLong();
73+
74+
for (int i = 0; i < input.nextRuns; i++) {
75+
subject.onNext("Response");
76+
}
77+
subject.onCompleted();
78+
79+
subject.subscribe(new Observer<Object>() {
80+
@Override
81+
public void onCompleted() {
82+
latch.countDown();
83+
}
84+
85+
@Override
86+
public void onError(Throwable e) {
87+
}
88+
89+
@Override
90+
public void onNext(Object o) {
91+
sum.incrementAndGet();
92+
}
93+
});
94+
latch.await();
95+
bh.consume(sum);
96+
}
97+
98+
}

0 commit comments

Comments
 (0)