|
| 1 | +package com.netflix.hystrix.perf; |
| 2 | + |
| 3 | +import com.netflix.hystrix.HystrixCommand; |
| 4 | +import com.netflix.hystrix.HystrixCommandGroupKey; |
| 5 | +import com.netflix.hystrix.HystrixCommandProperties; |
| 6 | +import com.netflix.hystrix.HystrixThreadPoolProperties; |
| 7 | +import org.openjdk.jmh.annotations.Benchmark; |
| 8 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 9 | +import org.openjdk.jmh.annotations.Group; |
| 10 | +import org.openjdk.jmh.annotations.GroupThreads; |
| 11 | +import org.openjdk.jmh.annotations.Level; |
| 12 | +import org.openjdk.jmh.annotations.Mode; |
| 13 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 14 | +import org.openjdk.jmh.annotations.Param; |
| 15 | +import org.openjdk.jmh.annotations.Scope; |
| 16 | +import org.openjdk.jmh.annotations.Setup; |
| 17 | +import org.openjdk.jmh.annotations.State; |
| 18 | + |
| 19 | +import java.util.concurrent.TimeUnit; |
| 20 | + |
| 21 | +public class MultiThreadedMetricsTest { |
| 22 | + @State(Scope.Thread) |
| 23 | + public static class CommandState { |
| 24 | + HystrixCommand<Integer> command; |
| 25 | + |
| 26 | + @Param({"THREAD", "SEMAPHORE"}) |
| 27 | + public HystrixCommandProperties.ExecutionIsolationStrategy isolationStrategy; |
| 28 | + |
| 29 | + |
| 30 | + @Setup(Level.Invocation) |
| 31 | + public void setUp() { |
| 32 | + command = new HystrixCommand<Integer>( |
| 33 | + HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("PERF")) |
| 34 | + .andCommandPropertiesDefaults( |
| 35 | + HystrixCommandProperties.Setter() |
| 36 | + .withExecutionIsolationStrategy(isolationStrategy) |
| 37 | + .withRequestCacheEnabled(true) |
| 38 | + .withRequestLogEnabled(true) |
| 39 | + .withCircuitBreakerEnabled(true) |
| 40 | + .withCircuitBreakerForceOpen(false) |
| 41 | + ) |
| 42 | + .andThreadPoolPropertiesDefaults( |
| 43 | + HystrixThreadPoolProperties.Setter() |
| 44 | + .withCoreSize(100) |
| 45 | + ) |
| 46 | + ) { |
| 47 | + @Override |
| 48 | + protected Integer run() throws Exception { |
| 49 | + return 1; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + protected Integer getFallback() { |
| 54 | + return 2; |
| 55 | + } |
| 56 | + }; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Benchmark |
| 61 | + @Group("writeHeavy") |
| 62 | + @GroupThreads(7) |
| 63 | + @BenchmarkMode({Mode.Throughput}) |
| 64 | + @OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 65 | + public Integer writeHeavyCommandExecution(CommandState state) { |
| 66 | + return state.command.observe().toBlocking().first(); |
| 67 | + } |
| 68 | + |
| 69 | + @Benchmark |
| 70 | + @Group("writeHeavy") |
| 71 | + @GroupThreads(1) |
| 72 | + @BenchmarkMode({Mode.Throughput}) |
| 73 | + @OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 74 | + public Integer writeHeavyReadMetrics(CommandState state) { |
| 75 | + return state.command.getMetrics().getCurrentConcurrentExecutionCount(); |
| 76 | + } |
| 77 | + |
| 78 | + @Benchmark |
| 79 | + @Group("evenSplit") |
| 80 | + @GroupThreads(4) |
| 81 | + @BenchmarkMode({Mode.Throughput}) |
| 82 | + @OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 83 | + public Integer evenSplitOfWritesAndReadsCommandExecution(CommandState state) { |
| 84 | + return state.command.observe().toBlocking().first(); |
| 85 | + } |
| 86 | + |
| 87 | + @Benchmark |
| 88 | + @Group("evenSplit") |
| 89 | + @GroupThreads(4) |
| 90 | + @BenchmarkMode({Mode.Throughput}) |
| 91 | + @OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 92 | + public Integer evenSplitOfWritesAndReadsReadMetrics(CommandState state) { |
| 93 | + return state.command.getMetrics().getCurrentConcurrentExecutionCount(); |
| 94 | + } |
| 95 | + |
| 96 | + @Benchmark |
| 97 | + @Group("readHeavy") |
| 98 | + @GroupThreads(1) |
| 99 | + @BenchmarkMode({Mode.Throughput}) |
| 100 | + @OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 101 | + public Integer readHeavyCommandExecution(CommandState state) { |
| 102 | + return state.command.observe().toBlocking().first(); |
| 103 | + } |
| 104 | + |
| 105 | + @Benchmark |
| 106 | + @Group("readHeavy") |
| 107 | + @GroupThreads(7) |
| 108 | + @BenchmarkMode({Mode.Throughput}) |
| 109 | + @OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 110 | + public Integer readHeavyReadMetrics(CommandState state) { |
| 111 | + return state.command.getMetrics().getCurrentConcurrentExecutionCount(); |
| 112 | + } |
| 113 | +} |
0 commit comments