Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #544]add jmh benchmark test #545

Merged
merged 6 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@
</module>
<!-- 检查未被注释的main方法,排除以Application结尾命名的类 -->
<module name="UncommentedMain">
<property name="excludedClasses" value=".*Application$"/>
<property name="excludedClasses" value=".*Application$|.*BenchmarkRunner"/>
</module>
<!-- 禁止使用System.out.println -->
<module name="Regexp">
Expand Down
34 changes: 34 additions & 0 deletions benchmark/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.dromara.dynamictp</groupId>
<artifactId>dynamic-tp-all</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>dynamic-tp-benchmark</artifactId>

<dependencies>
<dependency>
<groupId>org.dromara.dynamictp</groupId>
<artifactId>dynamic-tp-core</artifactId>
</dependency>

<dependency>
<groupId>org.dromara.dynamictp</groupId>
<artifactId>dynamic-tp-extension-agent</artifactId>
</dependency>

<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.dromara.dynamictp.benchmark;

import org.openjdk.jmh.results.format.ResultFormatType;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

/**
* BenchmarkRunner related
*
* @author yanhom
* @since 1.2.1
**/
public class BenchmarkRunner {

public static void main(String[] args) throws RunnerException {
Options options = new OptionsBuilder()
.include(ExecutorBenchmark.class.getSimpleName())
.resultFormat(ResultFormatType.JSON)
.result("executor-benchmark-results.json")
.build();

new Runner(options).run();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.dromara.dynamictp.benchmark;

import com.google.common.collect.Sets;
import org.dromara.dynamictp.core.executor.DtpExecutor;
import org.dromara.dynamictp.core.support.ThreadPoolBuilder;
import org.dromara.dynamictp.core.support.task.wrapper.TaskWrappers;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;
import org.openjdk.jmh.annotations.Threads;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
* ExecutorBenchmark related
*
* @author yanhom
* @since 1.2.1
**/
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
@State(Scope.Thread)
@Warmup(iterations = 5)
public class ExecutorBenchmark {

@Param({"100", "2000", "4000", "6000", "8000"})
private int max;

@Param({"MEDIUM"})
private TaskType taskType;

private ThreadPoolExecutor standardExecutor;
private DtpExecutor dtpExecutor;

public enum TaskType {
// 轻量级任务,执行时间很短
LIGHT,
// 中等任务,有一定计算量
MEDIUM,
// 重量级任务,执行时间较长或有IO操作
HEAVY
}

@Setup
public void setup() {
standardExecutor = new ThreadPoolExecutor(
4,
8,
60L,
TimeUnit.SECONDS,
new LinkedBlockingQueue<>(1024),
new ThreadFactory() {
private final AtomicInteger counter = new AtomicInteger(1);
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "standard-pool-" + counter.getAndIncrement());
}
},
new ThreadPoolExecutor.CallerRunsPolicy()
);

dtpExecutor = ThreadPoolBuilder.newBuilder()
.corePoolSize(4)
.maximumPoolSize(8)
.keepAliveTime(60)
.threadFactory("dtp-test-pool")
.runTimeout(100)
.queueCapacity(100)
.queueCapacity(1024)
.taskWrappers(TaskWrappers.getInstance().getByNames(Sets.newHashSet("ttl", "mdc")))
.rejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy())
.buildDynamic();
}

@TearDown
public void tearDown() {
standardExecutor.shutdown();
dtpExecutor.shutdown();
try {
if (!standardExecutor.awaitTermination(5, TimeUnit.SECONDS)) {
standardExecutor.shutdownNow();
}
if (!dtpExecutor.awaitTermination(5, TimeUnit.SECONDS)) {
dtpExecutor.shutdownNow();
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}

@Benchmark
@Threads(1)
public void testStandardSubmit(Blackhole bh) {
executeTasksAndWait(standardExecutor, bh);
}

@Benchmark
@Threads(1)
public void testDtpSubmit(Blackhole bh) {
executeTasksAndWait(dtpExecutor, bh);
}

@Benchmark
@Threads(4)
public void test8ThreadsStandardSubmit(Blackhole bh) {
executeTasksAndWait(standardExecutor, bh);
}

@Benchmark
@Threads(4)
public void test8ThreadsDtpSubmit(Blackhole bh) {
executeTasksAndWait(dtpExecutor, bh);
}

private void executeTasksAndWait(ThreadPoolExecutor executor, Blackhole bh) {
executor.submit(() -> {
try {
int res = 0;
switch (taskType) {
case LIGHT:
res = max * max;
break;
case MEDIUM:
res = calculatePrimes(max);
break;
case HEAVY:
res = calculatePrimes(max);
Thread.sleep(2);
break;
default:
break;
}
bh.consume(res);
return res;
} catch (Exception e) {
return -1;
}
});
}

private int calculatePrimes(int max) {
int count = 0;
for (int i = 2; i <= max; i++) {
boolean isPrime = true;
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
count++;
}
}
return count;
}
}
Loading
Loading