|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.dromara.dynamictp.benchmark; |
| 19 | + |
| 20 | +import com.google.common.collect.Sets; |
| 21 | +import org.dromara.dynamictp.core.executor.DtpExecutor; |
| 22 | +import org.dromara.dynamictp.core.support.ThreadPoolBuilder; |
| 23 | +import org.dromara.dynamictp.core.support.task.wrapper.TaskWrappers; |
| 24 | +import org.openjdk.jmh.annotations.Benchmark; |
| 25 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 26 | +import org.openjdk.jmh.annotations.Mode; |
| 27 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 28 | +import org.openjdk.jmh.annotations.Param; |
| 29 | +import org.openjdk.jmh.annotations.Scope; |
| 30 | +import org.openjdk.jmh.annotations.Setup; |
| 31 | +import org.openjdk.jmh.annotations.State; |
| 32 | +import org.openjdk.jmh.annotations.TearDown; |
| 33 | +import org.openjdk.jmh.annotations.Threads; |
| 34 | +import org.openjdk.jmh.annotations.Warmup; |
| 35 | +import org.openjdk.jmh.infra.Blackhole; |
| 36 | + |
| 37 | +import java.util.concurrent.LinkedBlockingQueue; |
| 38 | +import java.util.concurrent.ThreadFactory; |
| 39 | +import java.util.concurrent.ThreadPoolExecutor; |
| 40 | +import java.util.concurrent.TimeUnit; |
| 41 | +import java.util.concurrent.atomic.AtomicInteger; |
| 42 | + |
| 43 | +/** |
| 44 | + * ExecutorBenchmark related |
| 45 | + * |
| 46 | + * @author yanhom |
| 47 | + * @since 1.2.1 |
| 48 | + **/ |
| 49 | +@BenchmarkMode(Mode.Throughput) |
| 50 | +@OutputTimeUnit(TimeUnit.SECONDS) |
| 51 | +@State(Scope.Thread) |
| 52 | +@Warmup(iterations = 5) |
| 53 | +public class ExecutorBenchmark { |
| 54 | + |
| 55 | + @Param({"100", "2000", "4000", "6000", "8000"}) |
| 56 | + private int max; |
| 57 | + |
| 58 | + @Param({"MEDIUM"}) |
| 59 | + private TaskType taskType; |
| 60 | + |
| 61 | + private ThreadPoolExecutor standardExecutor; |
| 62 | + private DtpExecutor dtpExecutor; |
| 63 | + |
| 64 | + public enum TaskType { |
| 65 | + // 轻量级任务,执行时间很短 |
| 66 | + LIGHT, |
| 67 | + // 中等任务,有一定计算量 |
| 68 | + MEDIUM, |
| 69 | + // 重量级任务,执行时间较长或有IO操作 |
| 70 | + HEAVY |
| 71 | + } |
| 72 | + |
| 73 | + @Setup |
| 74 | + public void setup() { |
| 75 | + standardExecutor = new ThreadPoolExecutor( |
| 76 | + 4, |
| 77 | + 8, |
| 78 | + 60L, |
| 79 | + TimeUnit.SECONDS, |
| 80 | + new LinkedBlockingQueue<>(1024), |
| 81 | + new ThreadFactory() { |
| 82 | + private final AtomicInteger counter = new AtomicInteger(1); |
| 83 | + @Override |
| 84 | + public Thread newThread(Runnable r) { |
| 85 | + return new Thread(r, "standard-pool-" + counter.getAndIncrement()); |
| 86 | + } |
| 87 | + }, |
| 88 | + new ThreadPoolExecutor.CallerRunsPolicy() |
| 89 | + ); |
| 90 | + |
| 91 | + dtpExecutor = ThreadPoolBuilder.newBuilder() |
| 92 | + .corePoolSize(4) |
| 93 | + .maximumPoolSize(8) |
| 94 | + .keepAliveTime(60) |
| 95 | + .threadFactory("dtp-test-pool") |
| 96 | + .runTimeout(100) |
| 97 | + .queueTimeout(100) |
| 98 | + .queueCapacity(1024) |
| 99 | + .taskWrappers(TaskWrappers.getInstance().getByNames(Sets.newHashSet("ttl", "mdc"))) |
| 100 | + .rejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()) |
| 101 | + .buildDynamic(); |
| 102 | + } |
| 103 | + |
| 104 | + @TearDown |
| 105 | + public void tearDown() { |
| 106 | + standardExecutor.shutdown(); |
| 107 | + dtpExecutor.shutdown(); |
| 108 | + try { |
| 109 | + if (!standardExecutor.awaitTermination(5, TimeUnit.SECONDS)) { |
| 110 | + standardExecutor.shutdownNow(); |
| 111 | + } |
| 112 | + if (!dtpExecutor.awaitTermination(5, TimeUnit.SECONDS)) { |
| 113 | + dtpExecutor.shutdownNow(); |
| 114 | + } |
| 115 | + } catch (InterruptedException e) { |
| 116 | + Thread.currentThread().interrupt(); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @Benchmark |
| 121 | + @Threads(1) |
| 122 | + public void testStandardSubmit(Blackhole bh) { |
| 123 | + executeTasksAndWait(standardExecutor, bh); |
| 124 | + } |
| 125 | + |
| 126 | + @Benchmark |
| 127 | + @Threads(1) |
| 128 | + public void testDtpSubmit(Blackhole bh) { |
| 129 | + executeTasksAndWait(dtpExecutor, bh); |
| 130 | + } |
| 131 | + |
| 132 | + @Benchmark |
| 133 | + @Threads(4) |
| 134 | + public void test8ThreadsStandardSubmit(Blackhole bh) { |
| 135 | + executeTasksAndWait(standardExecutor, bh); |
| 136 | + } |
| 137 | + |
| 138 | + @Benchmark |
| 139 | + @Threads(4) |
| 140 | + public void test8ThreadsDtpSubmit(Blackhole bh) { |
| 141 | + executeTasksAndWait(dtpExecutor, bh); |
| 142 | + } |
| 143 | + |
| 144 | + private void executeTasksAndWait(ThreadPoolExecutor executor, Blackhole bh) { |
| 145 | + executor.submit(() -> { |
| 146 | + try { |
| 147 | + int res = 0; |
| 148 | + switch (taskType) { |
| 149 | + case LIGHT: |
| 150 | + res = max * max; |
| 151 | + break; |
| 152 | + case MEDIUM: |
| 153 | + res = calculatePrimes(max); |
| 154 | + break; |
| 155 | + case HEAVY: |
| 156 | + res = calculatePrimes(max); |
| 157 | + Thread.sleep(2); |
| 158 | + break; |
| 159 | + default: |
| 160 | + break; |
| 161 | + } |
| 162 | + bh.consume(res); |
| 163 | + return res; |
| 164 | + } catch (Exception e) { |
| 165 | + return -1; |
| 166 | + } |
| 167 | + }); |
| 168 | + } |
| 169 | + |
| 170 | + private int calculatePrimes(int max) { |
| 171 | + int count = 0; |
| 172 | + for (int i = 2; i <= max; i++) { |
| 173 | + boolean isPrime = true; |
| 174 | + for (int j = 2; j <= Math.sqrt(i); j++) { |
| 175 | + if (i % j == 0) { |
| 176 | + isPrime = false; |
| 177 | + break; |
| 178 | + } |
| 179 | + } |
| 180 | + if (isPrime) { |
| 181 | + count++; |
| 182 | + } |
| 183 | + } |
| 184 | + return count; |
| 185 | + } |
| 186 | +} |
0 commit comments