|
| 1 | +package com.netflix.hystrix.perf; |
| 2 | + |
| 3 | +import com.netflix.hystrix.strategy.properties.HystrixProperty; |
| 4 | +import com.netflix.hystrix.util.HystrixRollingNumber; |
| 5 | +import com.netflix.hystrix.util.HystrixRollingNumberEvent; |
| 6 | +import org.openjdk.jmh.annotations.Benchmark; |
| 7 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 8 | +import org.openjdk.jmh.annotations.Group; |
| 9 | +import org.openjdk.jmh.annotations.GroupThreads; |
| 10 | +import org.openjdk.jmh.annotations.Level; |
| 11 | +import org.openjdk.jmh.annotations.Mode; |
| 12 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 13 | +import org.openjdk.jmh.annotations.Scope; |
| 14 | +import org.openjdk.jmh.annotations.Setup; |
| 15 | +import org.openjdk.jmh.annotations.State; |
| 16 | + |
| 17 | +import java.util.Random; |
| 18 | +import java.util.concurrent.TimeUnit; |
| 19 | + |
| 20 | +public class RollingNumberPerfTest { |
| 21 | + @State(Scope.Thread) |
| 22 | + public static class CounterState { |
| 23 | + HystrixRollingNumber counter; |
| 24 | + |
| 25 | + @Setup(Level.Iteration) |
| 26 | + public void setUp() { |
| 27 | + counter = new HystrixRollingNumber( |
| 28 | + HystrixProperty.Factory.asProperty(100), |
| 29 | + HystrixProperty.Factory.asProperty(10)); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + @State(Scope.Thread) |
| 34 | + public static class ValueState { |
| 35 | + final Random r = new Random(); |
| 36 | + |
| 37 | + int value; |
| 38 | + HystrixRollingNumberEvent type; |
| 39 | + |
| 40 | + @Setup(Level.Invocation) |
| 41 | + public void setUp() { |
| 42 | + value = r.nextInt(100); |
| 43 | + int typeInt = r.nextInt(3); |
| 44 | + switch(typeInt) { |
| 45 | + case 0: |
| 46 | + type = HystrixRollingNumberEvent.SUCCESS; |
| 47 | + break; |
| 48 | + case 1: |
| 49 | + type = HystrixRollingNumberEvent.FAILURE; |
| 50 | + break; |
| 51 | + case 2: |
| 52 | + type = HystrixRollingNumberEvent.TIMEOUT; |
| 53 | + break; |
| 54 | + default: throw new RuntimeException("Unexpected : " + typeInt); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Benchmark |
| 60 | + @BenchmarkMode({Mode.Throughput}) |
| 61 | + @OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 62 | + public HystrixRollingNumber writeOnly(CounterState counterState, ValueState valueState) { |
| 63 | + counterState.counter.add(valueState.type, valueState.value); |
| 64 | + return counterState.counter; |
| 65 | + } |
| 66 | + |
| 67 | + @Benchmark |
| 68 | + @BenchmarkMode({Mode.Throughput}) |
| 69 | + @OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 70 | + public long readOnly(CounterState counterState) { |
| 71 | + HystrixRollingNumber counter = counterState.counter; |
| 72 | + return counter.getCumulativeSum(HystrixRollingNumberEvent.SUCCESS) + |
| 73 | + counter.getCumulativeSum(HystrixRollingNumberEvent.FAILURE) + |
| 74 | + counter.getCumulativeSum(HystrixRollingNumberEvent.TIMEOUT) + |
| 75 | + counter.getRollingSum(HystrixRollingNumberEvent.SUCCESS) + |
| 76 | + counter.getRollingSum(HystrixRollingNumberEvent.FAILURE) + |
| 77 | + counter.getRollingSum(HystrixRollingNumberEvent.TIMEOUT); |
| 78 | + } |
| 79 | + |
| 80 | + @Benchmark |
| 81 | + @Group("writeHeavy") |
| 82 | + @GroupThreads(7) |
| 83 | + @BenchmarkMode({Mode.Throughput}) |
| 84 | + @OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 85 | + public HystrixRollingNumber writeHeavyCounterAdd(CounterState counterState, ValueState valueState) { |
| 86 | + counterState.counter.add(valueState.type, valueState.value); |
| 87 | + return counterState.counter; |
| 88 | + } |
| 89 | + |
| 90 | + @Benchmark |
| 91 | + @Group("writeHeavy") |
| 92 | + @GroupThreads(1) |
| 93 | + @BenchmarkMode({Mode.Throughput}) |
| 94 | + @OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 95 | + public long writeHeavyReadMetrics(CounterState counterState) { |
| 96 | + HystrixRollingNumber counter = counterState.counter; |
| 97 | + return counter.getCumulativeSum(HystrixRollingNumberEvent.SUCCESS) + |
| 98 | + counter.getCumulativeSum(HystrixRollingNumberEvent.FAILURE) + |
| 99 | + counter.getCumulativeSum(HystrixRollingNumberEvent.TIMEOUT) + |
| 100 | + counter.getRollingSum(HystrixRollingNumberEvent.SUCCESS) + |
| 101 | + counter.getRollingSum(HystrixRollingNumberEvent.FAILURE) + |
| 102 | + counter.getRollingSum(HystrixRollingNumberEvent.TIMEOUT); |
| 103 | + } |
| 104 | + |
| 105 | + @Benchmark |
| 106 | + @Group("evenSplit") |
| 107 | + @GroupThreads(4) |
| 108 | + @BenchmarkMode({Mode.Throughput}) |
| 109 | + @OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 110 | + public HystrixRollingNumber evenSplitCounterAdd(CounterState counterState, ValueState valueState) { |
| 111 | + counterState.counter.add(valueState.type, valueState.value); |
| 112 | + return counterState.counter; |
| 113 | + } |
| 114 | + |
| 115 | + @Benchmark |
| 116 | + @Group("evenSplit") |
| 117 | + @GroupThreads(4) |
| 118 | + @BenchmarkMode({Mode.Throughput}) |
| 119 | + @OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 120 | + public long evenSplitReadMetrics(CounterState counterState) { |
| 121 | + HystrixRollingNumber counter = counterState.counter; |
| 122 | + return counter.getCumulativeSum(HystrixRollingNumberEvent.SUCCESS) + |
| 123 | + counter.getCumulativeSum(HystrixRollingNumberEvent.FAILURE) + |
| 124 | + counter.getCumulativeSum(HystrixRollingNumberEvent.TIMEOUT) + |
| 125 | + counter.getRollingSum(HystrixRollingNumberEvent.SUCCESS) + |
| 126 | + counter.getRollingSum(HystrixRollingNumberEvent.FAILURE) + |
| 127 | + counter.getRollingSum(HystrixRollingNumberEvent.TIMEOUT); |
| 128 | + } |
| 129 | + |
| 130 | + @Benchmark |
| 131 | + @Group("readHeavy") |
| 132 | + @GroupThreads(1) |
| 133 | + @BenchmarkMode({Mode.Throughput}) |
| 134 | + @OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 135 | + public HystrixRollingNumber readHeavyCounterAdd(CounterState counterState, ValueState valueState) { |
| 136 | + counterState.counter.add(valueState.type, valueState.value); |
| 137 | + return counterState.counter; |
| 138 | + } |
| 139 | + |
| 140 | + @Benchmark |
| 141 | + @Group("readHeavy") |
| 142 | + @GroupThreads(7) |
| 143 | + @BenchmarkMode({Mode.Throughput}) |
| 144 | + @OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 145 | + public long readHeavyReadMetrics(CounterState counterState) { |
| 146 | + HystrixRollingNumber counter = counterState.counter; |
| 147 | + return counter.getCumulativeSum(HystrixRollingNumberEvent.SUCCESS) + |
| 148 | + counter.getCumulativeSum(HystrixRollingNumberEvent.FAILURE) + |
| 149 | + counter.getCumulativeSum(HystrixRollingNumberEvent.TIMEOUT) + |
| 150 | + counter.getRollingSum(HystrixRollingNumberEvent.SUCCESS) + |
| 151 | + counter.getRollingSum(HystrixRollingNumberEvent.FAILURE) + |
| 152 | + counter.getRollingSum(HystrixRollingNumberEvent.TIMEOUT); |
| 153 | + } |
| 154 | +} |
0 commit comments