Skip to content

Commit

Permalink
test: add interface-v2 benchmark test result and update README
Browse files Browse the repository at this point in the history
  • Loading branch information
adorabled4 committed Jan 5, 2024
1 parent bde1225 commit 3b135b1
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 5 deletions.
35 changes: 33 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -496,9 +496,8 @@ knife4j:

测试信息

Windows 10 @ 5600H

```
# Windows 10 @ 5600H
# JMH version: 1.22
# VM version: JDK 17.0.6, Java HotSpot(TM) 64-Bit Server VM, 17.0.6+9-LTS-190
# VM invoker: D:\jdk17_\bin\java.exe
Expand All @@ -520,6 +519,38 @@ V1InterfaceBenchmarkTest.getSuffixInfoTest avgt 20 0.909 ± 0.01
### v2-interface
具体代码参考 `com.dhx.apiinterface.jmh.V2InterfaceBenchmarkTest`
测试信息
```
# Windows 10 @ 5600H
# JMH version: 1.22
# VM version: JDK 17.0.6, Java HotSpot(TM) 64-Bit Server VM, 17.0.6+9-LTS-190
# VM invoker: D:\jdk17_\bin\java.exe
# VM options: -server
# Warmup: 1 iterations, 100 ms each, 10 calls per op
# Measurement: 100 iterations, 1 s each, 10 calls per op
# Timeout: 120 s per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Average time, time/op
# Benchmark: com.dhx.apiinterface.jmh.V3InterfaceBenchmarkTest.anaIdiomTest

Benchmark Mode Cnt Score Error Units
V3InterfaceBenchmarkTest.anaIdiomTest avgt 100 5.846 ± 0.123 ms/op
V3InterfaceBenchmarkTest.constellationParamSearchTest avgt 100 5.958 ± 0.121 ms/op
V3InterfaceBenchmarkTest.jqrcodeTest avgt 100 19.609 ± 0.433 ms/op
V3InterfaceBenchmarkTest.locationSearchTest avgt 100 5.837 ± 0.133 ms/op
```
![](http://dhx-blog.oss-cn-beijing.aliyuncs.com/dhx/image-20240105205527341.png)
## 快速部署
本项目前后端均使用docker进行镜像构建以及部署。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.openjdk.jmh.results.format.ResultFormatType;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import org.openjdk.jmh.runner.options.TimeValue;

import java.util.concurrent.TimeUnit;

Expand All @@ -20,9 +21,13 @@ protected Options getOptions() {
.shouldDoGC(true)
.shouldFailOnError(true)
.forks(1)
.threads(4) // 测试线程数
.warmupIterations(5) // 预热迭代次数
.measurementIterations(20) // 测试迭代次数
.threads(1) // 测试线程数
.warmupIterations(1) // 预热迭代次数
.warmupTime(TimeValue.seconds(1))// 预热迭代的时间
.warmupBatchSize(10)// 预热迭代次数
.measurementIterations(100) // 测试迭代次数
.measurementTime(TimeValue.seconds(1))// 正式测量迭代的时间
.measurementBatchSize(10) // 每批次测量的大小
.timeUnit(TimeUnit.MILLISECONDS)
.resultFormat(resultsFileOutputType)
.result(buildResultsFileName(resultFilePrefix, resultsFileOutputType))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package com.dhx.apiinterface.jmh;

import cn.hutool.core.util.RandomUtil;
import com.dhx.apicommon.model.v1.param.FileSuffixParam;
import com.dhx.apicommon.model.v1.param.IpAnaParam;
import com.dhx.apicommon.model.v3.param.ConstellationParam;
import com.dhx.apicommon.model.v3.param.IdiomParm;
import com.dhx.apicommon.model.v3.param.LocationParm;
import com.dhx.apicommon.model.v3.param.QRCodeParam;
import com.dhx.apiinterface.ApiInterfaceApplication;
import com.dhx.apiinterface.controller.Version3Controller;
import org.junit.jupiter.api.Test;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
* @author adorabled4
* @className V2InterfaceBenchmarkTest
* @date : 2024-1-5 20:17:20
**/
@State(Scope.Benchmark)
@BenchmarkMode(Mode.AverageTime)
@Timeout(time = 120)
public class V3InterfaceBenchmarkTest extends BaseBenchMarkTest {
private Version3Controller controller;
ConfigurableApplicationContext context;

@Setup
public void setup() {
if (context == null) {
context = SpringApplication.run(ApiInterfaceApplication.class);
}
if (controller == null) {
this.controller = this.context.getBean(Version3Controller.class);
}
}

@TearDown
public void tearDown() {
// 在测试结束后进行清理工作,比如关闭Spring上下文
context.close();
}

@Benchmark
public void anaIdiomTest() {
IdiomParm param = new IdiomParm();
param.setWd(getWd());
this.controller.anaIdiom(param);
}

@Benchmark
public void jqrcodeTest() {
this.controller.jqrcode(generateRandomQRCodeParam());
}


@Benchmark
public void constellationParamSearchTest() {
this.controller.constellationParamSearch(ConstellationParam.builder()
.keyword(LocalDateTime.now().format(DateTimeFormatter.ISO_DATE))
.build());
}


@Benchmark
public void locationSearchTest() {
this.controller.locationSearch(genLocationParam());
}



@Test
public void executeJmhRunner() throws RunnerException, IOException {
new Runner(getOptions()).run();
}

private String getWd() {
String[] fileExtensions = {
"平步青云",
"春风得意",
"乐不思蜀",
"百花齐放",
"画龙点睛",
"千军万马",
"如鱼得水",
"四面楚歌",
"笑逐颜开",
"金榜题名",
"心驰神往",
"独具匠心",
"心旷神怡",
"通宵达旦",
"心灵手巧",
"左右逢源",
"心惊肉跳",
"笑逐颜开",
"心直口快"
};
return fileExtensions[RandomUtil.randomInt(0, fileExtensions.length)];
}

public QRCodeParam generateRandomQRCodeParam() {
String[] possibleErrorLevels = {"h", "q", "m", "l"};
String randomText = "YourRandomTextHere"; // Replace with actual random text
String randomEL = possibleErrorLevels[RandomUtil.randomInt(0, possibleErrorLevels.length)];
String randomBgColor = "ffffff"; // Replace with actual random color
String randomFgColor = "000000"; // Replace with actual random color
String randomLogo = "https://example.com/logo.png"; // Replace with actual random logo URL
Integer randomW = RandomUtil.randomInt(200, 500);
Integer randomM = 10; // Replace with actual random margin
Integer randomLW = 60; // Replace with actual random logo width
Integer randomType = 1;

return QRCodeParam.builder()
.text(randomText)
.el(randomEL)
.bgcolor(randomBgColor)
.fgcolor(randomFgColor)
.logo(randomLogo)
.w(randomW)
.m(randomM)
.lw(randomLW)
.type(randomType)
.build();
}

private LocationParm genLocationParam() {
LocationParm locationParm = new LocationParm();
String[] fids = {"320000", "320000", "320000", "320000", "320000", "320000", "320000", "320000", "320000", "320000", "320000", "320000", "320000", "360000", "360000", "360000", "360000", "360000", "360000", "360000", "360000", "360000", "360000", "360000"};
locationParm.setFid(fids[RandomUtil.randomInt(0, fids.length)]);
return locationParm;
}

}

0 comments on commit 3b135b1

Please sign in to comment.