Skip to content

Commit 38ddfb5

Browse files
authored
Merge pull request #558 from yanhom1314/springboot3
refactor alarm related, springboot3 [issues#442]
2 parents 0d2ce6c + 847c383 commit 38ddfb5

File tree

61 files changed

+1604
-378
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1604
-378
lines changed

.github/checkstyle/checkstyle.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@
166166
</module>
167167
<!-- 检查未被注释的main方法,排除以Application结尾命名的类 -->
168168
<module name="UncommentedMain">
169-
<property name="excludedClasses" value=".*Application$"/>
169+
<property name="excludedClasses" value=".*Application$|.*BenchmarkRunner"/>
170170
</module>
171171
<!-- 禁止使用System.out.println -->
172172
<module name="Regexp">

.github/workflows/build-jvmti.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on: [push]
44

55
jobs:
66
linux:
7-
runs-on: ubuntu-20.04
7+
runs-on: ubuntu-latest
88
steps:
99
- uses: actions/checkout@v3
1010
- name: Set up JDK 17

README.md

+9-8
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,14 @@ protected void afterExecute(Runnable r, Throwable t);
182182

183183
---
184184

185+
## 知识星球
186+
187+
<a href="https://t.zsxq.com/annca" target="_blank">
188+
<img class="no-zoom" src="/resources/img/zsxq.png" width="50%" height="50%"/>
189+
</a>
190+
191+
---
192+
185193
## Star History
186194

187195
[![Star History Chart](https://api.star-history.com/svg?repos=dromara/dynamic-tp&type=Date)](https://star-history.com/#dromara/dynamic-tp&Date)
@@ -202,7 +210,7 @@ protected void afterExecute(Runnable r, Throwable t);
202210

203211
使用过程中有任何问题,或者对项目有什么想法或者建议,可以加入社群,跟 1500+ 群友一起交流讨论。
204212

205-
微信群已满 200 人,可以关注微信公众号,加我个人微信拉群(备注:dynamic-tp)。
213+
微信群均已满 200 人,可以关注微信公众号,加我个人微信拉群(备注:dynamic-tp 拉群)。
206214

207215
![](resources/img/contact.jpg)
208216

@@ -216,13 +224,6 @@ protected void afterExecute(Runnable r, Throwable t);
216224

217225
---
218226

219-
## 特别赞助
220-
221-
**JNPF低代码开发平台**
222-
223-
<a href="https://www.jnpfsoft.com/?from=dynamic-tp"><img src="/resources/img/jnpfsoft.png"></a>
224-
225-
---
226227
## 鸣谢
227228

228229
感谢 JetBrains 对开源项目的支持

adapter/adapter-common/src/main/java/org/dromara/dynamictp/adapter/common/DtpAdapterListener.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ protected void doAlarmCheck(DtpProperties dtpProperties) {
100100
}
101101
handlerMap.forEach((k, v) -> {
102102
val executorWrapper = v.getExecutorWrappers();
103-
executorWrapper.forEach((kk, vv) -> AlarmManager.tryAlarmAsync(vv, SCHEDULE_NOTIFY_ITEMS));
103+
executorWrapper.forEach((kk, vv) -> AlarmManager.checkAndTryAlarmAsync(vv, SCHEDULE_NOTIFY_ITEMS));
104104
});
105105
}
106106
}

benchmark/pom.xml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.dromara.dynamictp</groupId>
7+
<artifactId>dynamic-tp-all</artifactId>
8+
<version>${revision}</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>dynamic-tp-benchmark</artifactId>
12+
13+
<dependencies>
14+
<dependency>
15+
<groupId>org.dromara.dynamictp</groupId>
16+
<artifactId>dynamic-tp-core</artifactId>
17+
</dependency>
18+
19+
<dependency>
20+
<groupId>org.dromara.dynamictp</groupId>
21+
<artifactId>dynamic-tp-extension-agent</artifactId>
22+
</dependency>
23+
24+
<dependency>
25+
<groupId>org.openjdk.jmh</groupId>
26+
<artifactId>jmh-core</artifactId>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.openjdk.jmh</groupId>
30+
<artifactId>jmh-generator-annprocess</artifactId>
31+
</dependency>
32+
33+
</dependencies>
34+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 org.openjdk.jmh.results.format.ResultFormatType;
21+
import org.openjdk.jmh.runner.Runner;
22+
import org.openjdk.jmh.runner.RunnerException;
23+
import org.openjdk.jmh.runner.options.Options;
24+
import org.openjdk.jmh.runner.options.OptionsBuilder;
25+
26+
/**
27+
* BenchmarkRunner related
28+
*
29+
* @author yanhom
30+
* @since 1.2.1
31+
**/
32+
public class BenchmarkRunner {
33+
34+
public static void main(String[] args) throws RunnerException {
35+
Options options = new OptionsBuilder()
36+
.include(ExecutorBenchmark.class.getSimpleName())
37+
.resultFormat(ResultFormatType.JSON)
38+
.result("executor-benchmark-results.json")
39+
.build();
40+
41+
new Runner(options).run();
42+
}
43+
}
44+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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

Comments
 (0)