Skip to content

Commit e68ed42

Browse files
committed
Merge pull request #803 from mattrjacobs/add-jmh-rolling-number
Add jmh for rolling number and rolling max
2 parents 726466d + 4616fb0 commit e68ed42

File tree

3 files changed

+275
-1
lines changed

3 files changed

+275
-1
lines changed

hystrix-core/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jmh {
3838
fork = 10
3939
iterations = 3
4040
jmhVersion = '1.9.3'
41-
profilers = ['gc']
41+
//profilers = ['gc']
4242
threads = 8
4343
warmup = '1s'
4444
warmupBatchSize = 1
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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 RollingMaxPerfTest {
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+
39+
@Setup(Level.Invocation)
40+
public void setUp() {
41+
value = r.nextInt(100);
42+
}
43+
}
44+
45+
@Benchmark
46+
@BenchmarkMode({Mode.Throughput})
47+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
48+
public HystrixRollingNumber writeOnly(CounterState counterState, ValueState valueState) {
49+
counterState.counter.updateRollingMax(HystrixRollingNumberEvent.COMMAND_MAX_ACTIVE, valueState.value);
50+
return counterState.counter;
51+
}
52+
53+
@Benchmark
54+
@BenchmarkMode({Mode.Throughput})
55+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
56+
public long readOnly(CounterState counterState) {
57+
HystrixRollingNumber counter = counterState.counter;
58+
return counter.getRollingMaxValue(HystrixRollingNumberEvent.COMMAND_MAX_ACTIVE);
59+
}
60+
61+
@Benchmark
62+
@Group("writeHeavy")
63+
@GroupThreads(7)
64+
@BenchmarkMode({Mode.Throughput})
65+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
66+
public HystrixRollingNumber writeHeavyUpdateMax(CounterState counterState, ValueState valueState) {
67+
counterState.counter.updateRollingMax(HystrixRollingNumberEvent.COMMAND_MAX_ACTIVE, valueState.value);
68+
return counterState.counter;
69+
}
70+
71+
@Benchmark
72+
@Group("writeHeavy")
73+
@GroupThreads(1)
74+
@BenchmarkMode({Mode.Throughput})
75+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
76+
public long writeHeavyReadMetrics(CounterState counterState) {
77+
HystrixRollingNumber counter = counterState.counter;
78+
return counter.getRollingMaxValue(HystrixRollingNumberEvent.COMMAND_MAX_ACTIVE);
79+
}
80+
81+
@Benchmark
82+
@Group("evenSplit")
83+
@GroupThreads(4)
84+
@BenchmarkMode({Mode.Throughput})
85+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
86+
public HystrixRollingNumber evenSplitUpdateMax(CounterState counterState, ValueState valueState) {
87+
counterState.counter.updateRollingMax(HystrixRollingNumberEvent.COMMAND_MAX_ACTIVE, valueState.value);
88+
return counterState.counter;
89+
}
90+
91+
@Benchmark
92+
@Group("evenSplit")
93+
@GroupThreads(4)
94+
@BenchmarkMode({Mode.Throughput})
95+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
96+
public long evenSplitReadMetrics(CounterState counterState) {
97+
HystrixRollingNumber counter = counterState.counter;
98+
return counter.getRollingMaxValue(HystrixRollingNumberEvent.COMMAND_MAX_ACTIVE);
99+
}
100+
101+
@Benchmark
102+
@Group("readHeavy")
103+
@GroupThreads(1)
104+
@BenchmarkMode({Mode.Throughput})
105+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
106+
public HystrixRollingNumber readHeavyUpdateMax(CounterState counterState, ValueState valueState) {
107+
counterState.counter.updateRollingMax(HystrixRollingNumberEvent.COMMAND_MAX_ACTIVE, valueState.value);
108+
return counterState.counter;
109+
}
110+
111+
@Benchmark
112+
@Group("readHeavy")
113+
@GroupThreads(7)
114+
@BenchmarkMode({Mode.Throughput})
115+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
116+
public long readHeavyReadMetrics(CounterState counterState) {
117+
HystrixRollingNumber counter = counterState.counter;
118+
return counter.getRollingMaxValue(HystrixRollingNumberEvent.COMMAND_MAX_ACTIVE);
119+
}
120+
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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

Comments
 (0)