|
| 1 | +/** |
| 2 | + * Copyright 2016 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; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.concurrent.TimeUnit; |
| 21 | + |
| 22 | +import org.openjdk.jmh.annotations.*; |
| 23 | +import org.openjdk.jmh.infra.Blackhole; |
| 24 | + |
| 25 | +import rx.functions.*; |
| 26 | +import rx.jmh.LatchedObserver; |
| 27 | + |
| 28 | +/** |
| 29 | + * Benchmark operators that consume their sources completely and signal a single value. |
| 30 | + * <p> |
| 31 | + * gradlew benchmarks "-Pjmh=-f 1 -tu s -bm thrpt -wi 5 -i 5 -r 1 .*DeferredScalarPerf.*" |
| 32 | + * <p> |
| 33 | + * gradlew benchmarks "-Pjmh=-f 1 -tu ns -bm avgt -wi 5 -i 5 -r 1 .*DeferredScalarPerf.*" |
| 34 | + */ |
| 35 | +@BenchmarkMode(Mode.Throughput) |
| 36 | +@OutputTimeUnit(TimeUnit.SECONDS) |
| 37 | +@State(Scope.Thread) |
| 38 | +public class DeferredScalarPerf { |
| 39 | + |
| 40 | + @Param({"1", "10", "100", "1000", "10000", "100000", "1000000"}) |
| 41 | + public int count; |
| 42 | + |
| 43 | + Observable<Integer> last; |
| 44 | + |
| 45 | + Observable<Integer> reduce; |
| 46 | + |
| 47 | + Observable<Integer> reduceSeed; |
| 48 | + |
| 49 | + Observable<int[]> collect; |
| 50 | + |
| 51 | + @Setup |
| 52 | + public void setup() { |
| 53 | + Integer[] array = new Integer[count]; |
| 54 | + Arrays.fill(array, 777); |
| 55 | + |
| 56 | + Observable<Integer> source = Observable.from(array); |
| 57 | + |
| 58 | + reduce = source.reduce(new Func2<Integer, Integer, Integer>() { |
| 59 | + @Override |
| 60 | + public Integer call(Integer a, Integer b) { |
| 61 | + return b; |
| 62 | + } |
| 63 | + }); |
| 64 | + reduceSeed = source.reduce(0, new Func2<Integer, Integer, Integer>() { |
| 65 | + @Override |
| 66 | + public Integer call(Integer a, Integer b) { |
| 67 | + return b; |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + last = source.takeLast(1); |
| 72 | + |
| 73 | + collect = source.collect(new Func0<int[]>() { |
| 74 | + @Override |
| 75 | + public int[] call() { |
| 76 | + return new int[1]; |
| 77 | + } |
| 78 | + }, new Action2<int[], Integer>() { |
| 79 | + @Override |
| 80 | + public void call(int[] a, Integer b) { |
| 81 | + a[0] = b.intValue(); |
| 82 | + } |
| 83 | + } ); |
| 84 | + } |
| 85 | + |
| 86 | +// @Benchmark |
| 87 | + public void reduce(Blackhole bh) { |
| 88 | + reduce.subscribe(new LatchedObserver<Integer>(bh)); |
| 89 | + } |
| 90 | + |
| 91 | +// @Benchmark |
| 92 | + public void reduceSeed(Blackhole bh) { |
| 93 | + reduceSeed.subscribe(new LatchedObserver<Integer>(bh)); |
| 94 | + } |
| 95 | + |
| 96 | +// @Benchmark |
| 97 | + public void last(Blackhole bh) { |
| 98 | + last.subscribe(new LatchedObserver<Integer>(bh)); |
| 99 | + } |
| 100 | + |
| 101 | + @Benchmark |
| 102 | + public void collect(Blackhole bh) { |
| 103 | + collect.subscribe(new LatchedObserver<int[]>(bh)); |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments