|
| 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.Func1; |
| 34 | +import rx.internal.util.UtilityFunctions; |
| 35 | +import rx.jmh.LatchedObserver; |
| 36 | + |
| 37 | +/** |
| 38 | + * Benchmark typical atomic operations on volatile fields and AtomicXYZ classes. |
| 39 | + * <p> |
| 40 | + * gradlew benchmarks "-Pjmh=-f 1 -tu s -bm thrpt -wi 5 -i 5 -r 1 .*RedoPerf.*" |
| 41 | + * <p> |
| 42 | + * gradlew benchmarks "-Pjmh=-f 1 -tu ns -bm avgt -wi 5 -i 5 -r 1 .*RedoPerf.*" |
| 43 | + */ |
| 44 | +@BenchmarkMode(Mode.Throughput) |
| 45 | +@OutputTimeUnit(TimeUnit.SECONDS) |
| 46 | +@State(Scope.Thread) |
| 47 | +public class RedoPerf { |
| 48 | + @Param({"1,1", "1,1000", "1,1000000", "1000,1", "1000,1000", "1000000,1"}) |
| 49 | + public String params; |
| 50 | + |
| 51 | + public int len; |
| 52 | + public int repeat; |
| 53 | + |
| 54 | + Observable<Integer> sourceRepeating; |
| 55 | + |
| 56 | + Observable<Integer> sourceRetrying; |
| 57 | + |
| 58 | + Observable<Integer> redoRepeating; |
| 59 | + |
| 60 | + Observable<Integer> redoRetrying; |
| 61 | + |
| 62 | + Observable<Integer> baseline; |
| 63 | + |
| 64 | + @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 65 | + @Setup |
| 66 | + public void setup() { |
| 67 | + String[] ps = params.split(","); |
| 68 | + len = Integer.parseInt(ps[0]); |
| 69 | + repeat = Integer.parseInt(ps[1]); |
| 70 | + |
| 71 | + Integer[] values = new Integer[len]; |
| 72 | + Arrays.fill(values, 777); |
| 73 | + |
| 74 | + Observable<Integer> source = Observable.from(values); |
| 75 | + |
| 76 | + Observable<Integer> error = source.concatWith(Observable.<Integer>error(new RuntimeException())); |
| 77 | + |
| 78 | + Integer[] values2 = new Integer[len * repeat]; |
| 79 | + Arrays.fill(values2, 777); |
| 80 | + |
| 81 | + baseline = Observable.from(values2); |
| 82 | + |
| 83 | + sourceRepeating = source.repeat(repeat); |
| 84 | + |
| 85 | + sourceRetrying = error.retry(repeat); |
| 86 | + |
| 87 | + redoRepeating = source.repeatWhen((Func1)UtilityFunctions.identity()).take(len * repeat); |
| 88 | + |
| 89 | + redoRetrying = error.retryWhen((Func1)UtilityFunctions.identity()).take(len * repeat); |
| 90 | + } |
| 91 | + |
| 92 | + @Benchmark |
| 93 | + public void baseline(Blackhole bh) { |
| 94 | + baseline.subscribe(new LatchedObserver<Integer>(bh)); |
| 95 | + } |
| 96 | + |
| 97 | + @Benchmark |
| 98 | + public void repeatCounted(Blackhole bh) { |
| 99 | + sourceRepeating.subscribe(new LatchedObserver<Integer>(bh)); |
| 100 | + } |
| 101 | + |
| 102 | + @Benchmark |
| 103 | + public void retryCounted(Blackhole bh) { |
| 104 | + sourceRetrying.subscribe(new LatchedObserver<Integer>(bh)); |
| 105 | + } |
| 106 | + |
| 107 | + @Benchmark |
| 108 | + public void repeatWhen(Blackhole bh) { |
| 109 | + redoRepeating.subscribe(new LatchedObserver<Integer>(bh)); |
| 110 | + } |
| 111 | + |
| 112 | + @Benchmark |
| 113 | + public void retryWhen(Blackhole bh) { |
| 114 | + redoRetrying.subscribe(new LatchedObserver<Integer>(bh)); |
| 115 | + } |
| 116 | +} |
0 commit comments