|
3 | 3 | import java.util.concurrent.CountDownLatch;
|
4 | 4 | import java.util.concurrent.atomic.AtomicInteger;
|
5 | 5 |
|
| 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 | + |
6 | 12 | import rx.Observable;
|
7 | 13 | import rx.Observable.OnSubscribe;
|
8 | 14 | import rx.Observer;
|
9 | 15 | import rx.util.functions.Func1;
|
10 | 16 |
|
11 |
| -import com.google.caliper.Benchmark; |
12 |
| -import com.google.caliper.runner.CaliperMain; |
| 17 | +public class ObservableBenchmark { |
13 | 18 |
|
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()); |
19 | 22 | awaitAllObservers();
|
20 | 23 | }
|
21 | 24 |
|
22 |
| - public int timeMapIterate(long reps) { |
| 25 | + @GenerateMicroBenchmark |
| 26 | + public int timeMapIterate() { |
23 | 27 | 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(); |
30 | 32 | }
|
31 | 33 | return x;
|
32 | 34 | }
|
33 | 35 |
|
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)); |
36 | 39 | }
|
37 | 40 |
|
38 | 41 | /**************************************************************************
|
39 | 42 | * Below is internal stuff to avoid object allocation and time overhead of anything that isn't
|
40 | 43 | * being tested.
|
| 44 | + * |
| 45 | + * @throws RunnerException |
41 | 46 | **************************************************************************/
|
42 | 47 |
|
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(); |
45 | 55 | }
|
46 | 56 |
|
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()); |
51 | 59 | awaitAllObservers();
|
52 | 60 | }
|
53 | 61 |
|
|
0 commit comments