Skip to content

Commit d012a52

Browse files
committed
Small change to convert from Caliper to JMH.
1 parent 40bd48b commit d012a52

File tree

3 files changed

+31
-29
lines changed

3 files changed

+31
-29
lines changed

build.gradle

+1-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ subprojects {
5454
}
5555

5656
dependencies {
57-
perfCompile 'com.google.caliper:caliper:1.0-beta-1'
58-
perfRuntime 'com.sun.jersey:jersey-core:1.11'
57+
perfCompile 'org.openjdk.jmh:jmh-core:0.2'
5958
}
6059

6160
tasks.build {

rxjava-core/build.gradle

-5
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,8 @@ jar {
3333
}
3434

3535
task time(type:JavaExec) {
36-
//sourceSets.metaClass.methods.each { println it }
3736
classpath = sourceSets.perf.runtimeClasspath
3837
group 'Application'
3938
description 'Execute the calipser benchmark timing of Rx'
4039
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
4540
}

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

+30-22
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,59 @@
33
import java.util.concurrent.CountDownLatch;
44
import java.util.concurrent.atomic.AtomicInteger;
55

6+
import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
7+
import org.openjdk.jmh.runner.Runner;
8+
import org.openjdk.jmh.runner.RunnerException;
9+
import org.openjdk.jmh.runner.options.Options;
10+
import org.openjdk.jmh.runner.options.OptionsBuilder;
11+
612
import rx.Observable;
713
import rx.Observable.OnSubscribe;
814
import rx.Observer;
915
import rx.util.functions.Func1;
1016

11-
import com.google.caliper.Benchmark;
12-
import com.google.caliper.runner.CaliperMain;
17+
public class ObservableBenchmark {
1318

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+
@GenerateMicroBenchmark
20+
public void timeBaseline() {
21+
observableOfInts.subscribe(newObserver());
1922
awaitAllObservers();
2023
}
2124

22-
public int timeMapIterate(long reps) {
25+
@GenerateMicroBenchmark
26+
public int timeMapIterate() {
2327
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-
}
28+
for (int j = 0; j < intValues.length; j++) {
29+
// use hash code to make sure the JIT doesn't optimize too much and remove all of
30+
// our code.
31+
x |= ident.call(intValues[j]).hashCode();
3032
}
3133
return x;
3234
}
3335

34-
public void timeMap(long reps) {
35-
timeOperator(reps, new OperatorMap<Integer, Object>(ident));
36+
@GenerateMicroBenchmark
37+
public void timeMap() {
38+
timeOperator(new OperatorMap<Integer, Object>(ident));
3639
}
3740

3841
/**************************************************************************
3942
* Below is internal stuff to avoid object allocation and time overhead of anything that isn't
4043
* being tested.
44+
*
45+
* @throws RunnerException
4146
**************************************************************************/
4247

43-
public static void main(String[] args) {
44-
CaliperMain.main(ObservableBenchmark.class, args);
48+
public static void main(String[] args) throws RunnerException {
49+
Options opt = new OptionsBuilder()
50+
.include(ObservableBenchmark.class.getName()+".*")
51+
.forks(1)
52+
.build();
53+
54+
new Runner(opt).run();
4555
}
4656

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-
}
57+
private void timeOperator(Operator<Object, Integer> op) {
58+
observableOfInts.lift(op).subscribe(newObserver());
5159
awaitAllObservers();
5260
}
5361

0 commit comments

Comments
 (0)