jmeter性能测试及报告功能全量提交

This commit is contained in:
liangdaliang
2025-04-16 18:26:17 +08:00
parent f200aeec4e
commit adbceebec5
8 changed files with 535 additions and 176 deletions

View File

@@ -3,8 +3,6 @@ package com.test.test.domain;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.test.common.annotation.Excel;
import com.test.common.core.domain.BaseEntity;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import java.util.Date;
@@ -35,7 +33,7 @@ public class PerformanceTestCaseReport extends BaseEntity
/** 并发线程数 */
@Excel(name = "并发线程数")
private Long concurrentThreads;
private Integer concurrentThreads;
/** 平均响应时间(毫秒) */
@Excel(name = "平均响应时间", readConverterExp = "毫=秒")
@@ -61,12 +59,16 @@ public class PerformanceTestCaseReport extends BaseEntity
/** 触发方式1-定时任务2-手动 */
@Excel(name = "触发方式1-定时任务2-手动")
private Long triggerType;
private Integer triggerType;
/** 性能测试汇总报告json数组 */
@Excel(name = "性能测试汇总报告json数组")
private String summaryReport;
/** 性能测试错误报告json对象 */
@Excel(name = "性能测试错误报告json对象")
private String errorReport;
/** 申请人姓名 */
@Excel(name = "申请人姓名")
private String applyUser;
@@ -78,164 +80,131 @@ public class PerformanceTestCaseReport extends BaseEntity
/** 删除标记:0正常1删除 */
private String delFlag;
public void setId(Long id)
{
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getId()
{
return id;
public Long getPerformanceId() {
return performanceId;
}
public void setPerformanceId(Long performanceId)
{
public void setPerformanceId(Long performanceId) {
this.performanceId = performanceId;
}
public Long getPerformanceId()
{
return performanceId;
public Long getTestCaseId() {
return testCaseId;
}
public void setTestCaseId(Long testCaseId)
{
public void setTestCaseId(Long testCaseId) {
this.testCaseId = testCaseId;
}
public Long getTestCaseId()
{
return testCaseId;
public Long getSid() {
return sid;
}
public void setSid(Long sid)
{
public void setSid(Long sid) {
this.sid = sid;
}
public Long getSid()
{
return sid;
public Integer getConcurrentThreads() {
return concurrentThreads;
}
public void setConcurrentThreads(Long concurrentThreads)
{
public void setConcurrentThreads(Integer concurrentThreads) {
this.concurrentThreads = concurrentThreads;
}
public Long getConcurrentThreads()
{
return concurrentThreads;
public Long getAverage() {
return average;
}
public void setAverage(Long average)
{
public void setAverage(Long average) {
this.average = average;
}
public Long getAverage()
{
return average;
public String getTps() {
return tps;
}
public void setTps(String tps)
{
public void setTps(String tps) {
this.tps = tps;
}
public String getTps()
{
return tps;
public Date getStartTime() {
return startTime;
}
public void setStartTime(Date startTime)
{
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
public Date getStartTime()
{
return startTime;
public Date getEndTime() {
return endTime;
}
public void setEndTime(Date endTime)
{
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
public Date getEndTime()
{
return endTime;
public Long getCostTime() {
return costTime;
}
public void setCostTime(Long costTime)
{
public void setCostTime(Long costTime) {
this.costTime = costTime;
}
public Long getCostTime()
{
return costTime;
public Integer getTriggerType() {
return triggerType;
}
public void setTriggerType(Long triggerType)
{
public void setTriggerType(Integer triggerType) {
this.triggerType = triggerType;
}
public Long getTriggerType()
{
return triggerType;
public String getSummaryReport() {
return summaryReport;
}
public void setSummaryReport(String summaryReport)
{
public void setSummaryReport(String summaryReport) {
this.summaryReport = summaryReport;
}
public String getSummaryReport()
{
return summaryReport;
public String getErrorReport() {
return errorReport;
}
public void setApplyUser(String applyUser)
{
public void setErrorReport(String errorReport) {
this.errorReport = errorReport;
}
public String getApplyUser() {
return applyUser;
}
public void setApplyUser(String applyUser) {
this.applyUser = applyUser;
}
public String getApplyUser()
{
return applyUser;
public String getStatus() {
return status;
}
public void setStatus(String status)
{
public void setStatus(String status) {
this.status = status;
}
public String getStatus()
{
return status;
}
public void setDelFlag(String delFlag)
{
this.delFlag = delFlag;
}
public String getDelFlag()
{
public String getDelFlag() {
return delFlag;
}
@Override
public String toString() {
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
.append("id", getId())
.append("performanceId", getPerformanceId())
.append("testCaseId", getTestCaseId())
.append("sid", getSid())
.append("concurrentThreads", getConcurrentThreads())
.append("average", getAverage())
.append("tps", getTps())
.append("startTime", getStartTime())
.append("endTime", getEndTime())
.append("costTime", getCostTime())
.append("triggerType", getTriggerType())
.append("summaryReport", getSummaryReport())
.append("applyUser", getApplyUser())
.append("status", getStatus())
.append("delFlag", getDelFlag())
.append("createBy", getCreateBy())
.append("updateBy", getUpdateBy())
.append("createTime", getCreateTime())
.append("updateTime", getUpdateTime())
.toString();
public void setDelFlag(String delFlag) {
this.delFlag = delFlag;
}
}

View File

@@ -12,6 +12,14 @@ import java.util.List;
*/
public interface IPerformanceTestCaseReportService
{
/**
* 执行性能测试并生成报告
* @param id
* @param jmeterHomePath
* @param triggerType 触发方式1-定时任务2-手动
*/
public void executePerformanceTestAndReport(Long id, String jmeterHomePath, Integer triggerType);
/**
* 查询性能测试用例报告
*

View File

@@ -1,13 +1,32 @@
package com.test.test.service.impl;
import com.google.gson.Gson;
import com.test.common.core.domain.model.JmeterGroupRequest;
import com.test.common.core.domain.model.JmeterRequest;
import com.test.common.core.domain.model.LabelStatsEntity;
import com.test.common.core.domain.model.LoginUser;
import com.test.common.utils.DateUtils;
import com.test.common.utils.JMeterGroupUtil;
import com.test.common.utils.SecurityUtils;
import com.test.test.domain.PerformanceTest;
import com.test.test.domain.PerformanceTestCase;
import com.test.test.domain.PerformanceTestCaseReport;
import com.test.test.domain.TestCaseStep;
import com.test.test.mapper.PerformanceTestCaseMapper;
import com.test.test.mapper.PerformanceTestCaseReportMapper;
import com.test.test.mapper.PerformanceTestMapper;
import com.test.test.service.IPerformanceTestCaseReportService;
import com.test.test.service.ITestCaseStepService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
/**
* 性能测试用例报告Service业务层处理
@@ -15,11 +34,136 @@ import java.util.List;
* @author test
* @date 2025-04-14
*/
@Slf4j
@Service
public class PerformanceTestCaseReportServiceImpl implements IPerformanceTestCaseReportService
{
@Resource
private PerformanceTestCaseReportMapper performanceTestCaseReportMapper;
@Resource
private PerformanceTestCaseMapper performanceTestCaseMapper;
@Resource
private PerformanceTestMapper performanceTestMapper;
@Autowired
private ITestCaseStepService testCaseStepService;
@Override
public void executePerformanceTestAndReport(Long id, String jmeterHomePath, Integer triggerType) {
Long sid = System.currentTimeMillis();
PerformanceTest performanceTest = performanceTestMapper.selectPerformanceTestById(id);
PerformanceTestCase performanceTestCase = new PerformanceTestCase();
performanceTestCase.setPerformanceId(id);
performanceTestCase.setStatus(1);
List<PerformanceTestCase> relateTestCaseList = performanceTestCaseMapper.selectPerformanceTestCaseList(performanceTestCase);
for (PerformanceTestCase relateTestCase : relateTestCaseList) {
JmeterGroupRequest jmeterGroupRequest = new JmeterGroupRequest();
jmeterGroupRequest.setTestCaseId(relateTestCase.getTestCaseId());
jmeterGroupRequest.setSid(sid);
jmeterGroupRequest.setConcurrentThreads(performanceTest.getConcurrentThreads());
jmeterGroupRequest.setErrorOperType(performanceTest.getErrorOperType());
jmeterGroupRequest.setExecuteType(performanceTest.getExecuteType());
jmeterGroupRequest.setRampUpSeconds(performanceTest.getRampUpSeconds());
Long seconds = performanceTest.getPressureHour() * 3600L + performanceTest.getPressureMinute() * 60L + performanceTest.getPressureSecond() * 1L;
jmeterGroupRequest.setPressureSecond(seconds);
jmeterGroupRequest.setLoopCount(performanceTest.getLoopCount());
jmeterGroupRequest.setRpsStatus(performanceTest.getRpsStatus());
jmeterGroupRequest.setRpsLimit(performanceTest.getRpsLimit());
jmeterGroupRequest.setJmeterHomePath(jmeterHomePath);
List<TestCaseStep> testCaseStepList = testCaseStepService.selectTestCaseStepListByCaseId(relateTestCase.getTestCaseId());
List<JmeterRequest> jmeterRequestList = dealAddTestCaseHttpStep(testCaseStepList);
jmeterGroupRequest.setJmeterRequestList(jmeterRequestList);
Date startTime = new Date();
List<LabelStatsEntity> jmeterResultList = JMeterGroupUtil.getJmeterResult(jmeterGroupRequest);
Date endTime = new Date();
Gson gson = new Gson();
if (!CollectionUtils.isEmpty(jmeterResultList)) {
LabelStatsEntity lastElement = jmeterResultList.get(jmeterResultList.size() - 1);
PerformanceTestCaseReport performanceTestCaseReport = new PerformanceTestCaseReport();
performanceTestCaseReport.setPerformanceId(id);
performanceTestCaseReport.setTestCaseId(relateTestCase.getTestCaseId());
performanceTestCaseReport.setSid(sid);
performanceTestCaseReport.setConcurrentThreads(performanceTest.getConcurrentThreads());
performanceTestCaseReport.setAverage(lastElement.getAverage());
performanceTestCaseReport.setTps(String.format(Locale.US, "%.1f", lastElement.getThroughput()));
performanceTestCaseReport.setStartTime(startTime);
performanceTestCaseReport.setEndTime(endTime);
performanceTestCaseReport.setCostTime(endTime.getTime() - startTime.getTime());
performanceTestCaseReport.setTriggerType(triggerType);
performanceTestCaseReport.setSummaryReport(gson.toJson(jmeterResultList));
performanceTestCaseReport.setErrorReport("");
performanceTestCaseReport.setStatus("1");
LoginUser user = null;
try {
user = SecurityUtils.getLoginUser();
} catch (Exception e) {}
if (user != null) {
performanceTestCaseReport.setCreateBy(user.getUsername());
}
performanceTestCaseReport.setCreateTime(endTime);
performanceTestCaseReportMapper.insertPerformanceTestCaseReport(performanceTestCaseReport);
} else {
log.error(performanceTest.getPerformanceName() + "Jmeter执行性能测试无法获取到汇总报告");
}
}
performanceTest.setStatus("1");
performanceTest.setUpdateTime(new Date());
performanceTestMapper.updatePerformanceTest(performanceTest);
}
/**
* 新增http步骤测试接口到待性能测试列表
* @param testCaseStepList
* @return
*/
private List<JmeterRequest> dealAddTestCaseHttpStep(List<TestCaseStep> testCaseStepList) {
List<JmeterRequest> jmeterRequestList = new ArrayList<>();
for (TestCaseStep testCaseStep : testCaseStepList) {
if (testCaseStep.getType() == 1L) {
// http接口处理
JmeterRequest jmeterRequest = doHttpRequestTestAdd(testCaseStep);
if (jmeterRequest != null) {
jmeterRequestList.add(jmeterRequest);
log.info("用例步骤Http接口:{}新增待性能测试列表成功!", testCaseStep.getName());
}
}
}
return jmeterRequestList;
}
/**
* 处理http接口测试新增
* @param testCaseStep
* @return JmeterRequest
*/
private JmeterRequest doHttpRequestTestAdd(TestCaseStep testCaseStep) {
String url = testCaseStep.getRequestUrl();
String method = testCaseStep.getRequestMethod().toUpperCase();
if (!method.equals("GET") && !method.equals("POST")) {
log.error("不支持的请求方式:{}", method);
return null;
}
if (!url.startsWith("http")) {
String apiProtocol = testCaseStep.getApiProtocol();
String appendUrl = apiProtocol + "://" + testCaseStep.getApiHost();
if (testCaseStep.getApiPort() != null) {
url = appendUrl + ":" + testCaseStep.getApiPort() + url;
} else {
url = appendUrl + url;
}
}
JmeterRequest jmeterRequest = new JmeterRequest();
jmeterRequest.setTestCaseName(testCaseStep.getName());
jmeterRequest.setUrl(url);
jmeterRequest.setPort(testCaseStep.getApiPort());
jmeterRequest.setMethod(testCaseStep.getRequestMethod());
jmeterRequest.setRequestBody(testCaseStep.getRequestBody());
jmeterRequest.setRequestParams(testCaseStep.getRequestParams());
jmeterRequest.setRequestHeader(testCaseStep.getRequestHeader());
log.info("getRequestHeader:{}", jmeterRequest.getRequestHeader());
return jmeterRequest;
}
/**
* 查询性能测试用例报告

View File

@@ -17,6 +17,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<result property="costTime" column="cost_time" />
<result property="triggerType" column="trigger_type" />
<result property="summaryReport" column="summary_report" />
<result property="errorReport" column="error_report" />
<result property="applyUser" column="apply_user" />
<result property="status" column="status" />
<result property="delFlag" column="del_flag" />
@@ -27,7 +28,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</resultMap>
<sql id="selectPerformanceTestCaseReportVo">
select id, performance_id, test_case_id, sid, concurrent_threads, average, tps, start_time, end_time, cost_time, trigger_type, summary_report, apply_user, status, del_flag, create_by, update_by, create_time, update_time from performance_test_case_report
select id, performance_id, test_case_id, sid, concurrent_threads, average, tps, start_time, end_time, cost_time, trigger_type, summary_report, error_report, apply_user, status, del_flag, create_by, update_by, create_time, update_time from performance_test_case_report
</sql>
<select id="selectPerformanceTestCaseReportList" parameterType="PerformanceTestCaseReport" resultMap="PerformanceTestCaseReportResult">
@@ -44,6 +45,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="costTime != null "> and cost_time = #{costTime}</if>
<if test="triggerType != null "> and trigger_type = #{triggerType}</if>
<if test="summaryReport != null and summaryReport != ''"> and summary_report = #{summaryReport}</if>
<if test="errorReport != null and errorReport != ''"> and error_report = #{errorReport}</if>
<if test="applyUser != null and applyUser != ''"> and apply_user = #{applyUser}</if>
<if test="status != null and status != ''"> and status = #{status}</if>
</where>
@@ -68,6 +70,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="costTime != null">cost_time,</if>
<if test="triggerType != null">trigger_type,</if>
<if test="summaryReport != null">summary_report,</if>
<if test="errorReport != null">error_report,</if>
<if test="applyUser != null">apply_user,</if>
<if test="status != null">status,</if>
<if test="delFlag != null and delFlag != ''">del_flag,</if>
@@ -88,6 +91,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="costTime != null">#{costTime},</if>
<if test="triggerType != null">#{triggerType},</if>
<if test="summaryReport != null">#{summaryReport},</if>
<if test="errorReport != null">#{errorReport},</if>
<if test="applyUser != null">#{applyUser},</if>
<if test="status != null">#{status},</if>
<if test="delFlag != null and delFlag != ''">#{delFlag},</if>
@@ -112,6 +116,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="costTime != null">cost_time = #{costTime},</if>
<if test="triggerType != null">trigger_type = #{triggerType},</if>
<if test="summaryReport != null">summary_report = #{summaryReport},</if>
<if test="errorReport != null">error_report = #{errorReport},</if>
<if test="applyUser != null">apply_user = #{applyUser},</if>
<if test="status != null">status = #{status},</if>
<if test="delFlag != null and delFlag != ''">del_flag = #{delFlag},</if>