refactor(测试计划): 本地执行数据入库优化

This commit is contained in:
fit2-zhao 2022-04-26 14:35:32 +08:00 committed by fit2-zhao
parent 294ce2ce59
commit 2bd59cbcb2
6 changed files with 49 additions and 27 deletions

View File

@ -74,10 +74,10 @@ public class ApiDefinitionExecResultService {
RedisTemplate<String, Object> redisTemplate; RedisTemplate<String, Object> redisTemplate;
public void saveApiResult(List<RequestResult> requestResults, ResultDTO dto) { public void saveApiResult(ResultDTO dto) {
LoggerUtil.info("接收到API/CASE执行结果【 " + requestResults.size() + ""); LoggerUtil.info("接收到API/CASE执行结果【 " + dto.getRequestResults().size() + "");
for (RequestResult item : requestResults) { for (RequestResult item : dto.getRequestResults()) {
if (item.getResponseResult() != null && item.getResponseResult().getResponseTime() <= 0) { if (item.getResponseResult() != null && item.getResponseResult().getResponseTime() <= 0) {
item.getResponseResult().setResponseTime((item.getEndTime() - item.getStartTime())); item.getResponseResult().setResponseTime((item.getEndTime() - item.getStartTime()));
} }
@ -302,10 +302,10 @@ public class ApiDefinitionExecResultService {
* 定时任务触发的保存逻辑 * 定时任务触发的保存逻辑
* 定时任务时userID要改为定时任务中的用户 * 定时任务时userID要改为定时任务中的用户
*/ */
public void saveApiResultByScheduleTask(List<RequestResult> requestResults, ResultDTO dto) { public void saveApiResultByScheduleTask(ResultDTO dto) {
if (CollectionUtils.isNotEmpty(requestResults)) { if (CollectionUtils.isNotEmpty(dto.getRequestResults())) {
LoggerUtil.info("接收到定时任务执行结果【 " + requestResults.size() + ""); LoggerUtil.info("接收到定时任务执行结果【 " + dto.getRequestResults().size() + "");
for (RequestResult item : requestResults) { for (RequestResult item : dto.getRequestResults()) {
if (!StringUtils.startsWithAny(item.getName(), "PRE_PROCESSOR_ENV_", "POST_PROCESSOR_ENV_")) { if (!StringUtils.startsWithAny(item.getName(), "PRE_PROCESSOR_ENV_", "POST_PROCESSOR_ENV_")) {
//对响应内容进行进一步解析如果有附加信息比如误报库信息则根据附加信息内的数据进行其他判读 //对响应内容进行进一步解析如果有附加信息比如误报库信息则根据附加信息内的数据进行其他判读
RequestResultExpandDTO expandDTO = ResponseUtil.parseByRequestResult(item); RequestResultExpandDTO expandDTO = ResponseUtil.parseByRequestResult(item);
@ -336,8 +336,8 @@ public class ApiDefinitionExecResultService {
} }
updateTestCaseStates(dto.getTestId()); updateTestCaseStates(dto.getTestId());
Map<String, String> apiIdResultMap = new HashMap<>(); Map<String, String> apiIdResultMap = new HashMap<>();
long errorSize = requestResults.stream().filter(requestResult -> requestResult.getError() > 0).count(); long errorSize = dto.getRequestResults().stream().filter(requestResult -> requestResult.getError() > 0).count();
String status = errorSize > 0 || requestResults.isEmpty() ? TestPlanApiExecuteStatus.FAILD.name() : TestPlanApiExecuteStatus.SUCCESS.name(); String status = errorSize > 0 || dto.getRequestResults().isEmpty() ? TestPlanApiExecuteStatus.FAILD.name() : TestPlanApiExecuteStatus.SUCCESS.name();
if (StringUtils.isNotEmpty(dto.getReportId())) { if (StringUtils.isNotEmpty(dto.getReportId())) {
apiIdResultMap.put(dto.getReportId(), status); apiIdResultMap.put(dto.getReportId(), status);
} }

View File

@ -326,6 +326,7 @@ public class ApiExecutionQueueService {
for (ApiExecutionQueueDetail item : queueDetails) { for (ApiExecutionQueueDetail item : queueDetails) {
ApiExecutionQueue queue = queueMapper.selectByPrimaryKey(item.getQueueId()); ApiExecutionQueue queue = queueMapper.selectByPrimaryKey(item.getQueueId());
if (queue == null) { if (queue == null) {
executionQueueDetailMapper.deleteByPrimaryKey(item.getId());
continue; continue;
} }
// 在资源池中执行 // 在资源池中执行
@ -345,6 +346,10 @@ public class ApiExecutionQueueService {
ApiRunMode.SCHEDULE_SCENARIO.name(), ApiRunMode.SCHEDULE_SCENARIO.name(),
ApiRunMode.JENKINS_SCENARIO_PLAN.name())) { ApiRunMode.JENKINS_SCENARIO_PLAN.name())) {
ApiScenarioReport report = apiScenarioReportMapper.selectByPrimaryKey(item.getReportId()); ApiScenarioReport report = apiScenarioReportMapper.selectByPrimaryKey(item.getReportId());
// 报告已经被删除则队列也删除
if (report == null) {
executionQueueDetailMapper.deleteByPrimaryKey(item.getId());
}
if (report != null && StringUtils.equalsAnyIgnoreCase(report.getStatus(), TestPlanReportStatus.RUNNING.name(), APITestStatus.Waiting.name()) if (report != null && StringUtils.equalsAnyIgnoreCase(report.getStatus(), TestPlanReportStatus.RUNNING.name(), APITestStatus.Waiting.name())
&& report.getUpdateTime() < timeout) { && report.getUpdateTime() < timeout) {
report.setStatus(ScenarioStatus.Timeout.name()); report.setStatus(ScenarioStatus.Timeout.name());
@ -433,7 +438,11 @@ public class ApiExecutionQueueService {
}); });
} }
public void checkExecutionQueneByLoadTest(LoadTestReport loadTestReport) { /**
* 性能测试监听检查
* @param loadTestReport
*/
public void checkExecutionQueueByLoadTest(LoadTestReport loadTestReport) {
ApiExecutionQueueDetailExample detailExample = new ApiExecutionQueueDetailExample(); ApiExecutionQueueDetailExample detailExample = new ApiExecutionQueueDetailExample();
detailExample.createCriteria().andReportIdEqualTo(loadTestReport.getId()); detailExample.createCriteria().andReportIdEqualTo(loadTestReport.getId());
executionQueueDetailMapper.deleteByExample(detailExample); executionQueueDetailMapper.deleteByExample(detailExample);

View File

@ -35,14 +35,28 @@ public class ApiScenarioReportResultService {
public void save(String reportId, List<RequestResult> queue) { public void save(String reportId, List<RequestResult> queue) {
if (CollectionUtils.isNotEmpty(queue)) { if (CollectionUtils.isNotEmpty(queue)) {
queue.forEach(item -> { if (queue.size() == 1) {
// 事物控制器出来的结果特殊处理 queue.forEach(item -> {
if (StringUtils.isNotEmpty(item.getName()) && item.getName().startsWith("Transaction=") && CollectionUtils.isEmpty(item.getSubRequestResults())) { // 事物控制器出来的结果特殊处理
LoggerUtil.debug("合并事物请求暂不入库"); if (StringUtils.isNotEmpty(item.getName()) && item.getName().startsWith("Transaction=") && CollectionUtils.isEmpty(item.getSubRequestResults())) {
} else { LoggerUtil.debug("合并事物请求暂不入库");
apiScenarioReportResultMapper.insert(this.newApiScenarioReportResult(reportId, item)); } else {
apiScenarioReportResultMapper.insert(this.newApiScenarioReportResult(reportId, item));
}
});
} else {
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
ApiScenarioReportResultMapper batchMapper = sqlSession.getMapper(ApiScenarioReportResultMapper.class);
queue.forEach(item -> {
if (StringUtils.isEmpty(item.getName()) || !item.getName().startsWith("Transaction=") || !CollectionUtils.isEmpty(item.getSubRequestResults())) {
batchMapper.insert(this.newApiScenarioReportResult(reportId, item));
}
});
sqlSession.flushStatements();
if (sqlSession != null && sqlSessionFactory != null) {
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
} }
}); }
} }
} }
@ -112,7 +126,7 @@ public class ApiScenarioReportResultService {
if (CollectionUtils.isNotEmpty(errorCodeDTO.getErrorCodeList())) { if (CollectionUtils.isNotEmpty(errorCodeDTO.getErrorCodeList())) {
report.setErrorCode(errorCodeDTO.getErrorCodeStr()); report.setErrorCode(errorCodeDTO.getErrorCodeStr());
} }
if(StringUtils.equalsIgnoreCase(errorCodeDTO.getRequestStatus(),ExecuteResult.errorReportResult.name())){ if (StringUtils.equalsIgnoreCase(errorCodeDTO.getRequestStatus(), ExecuteResult.errorReportResult.name())) {
status = errorCodeDTO.getRequestStatus(); status = errorCodeDTO.getRequestStatus();
} }
report.setStatus(status); report.setStatus(status);

View File

@ -87,9 +87,9 @@ public class ApiScenarioReportService {
@Resource @Resource
RedisTemplate<String, Object> redisTemplate; RedisTemplate<String, Object> redisTemplate;
public void saveResult(List<RequestResult> requestResults, ResultDTO dto) { public void saveResult(ResultDTO dto) {
// 报告详情内容 // 报告详情内容
apiScenarioReportResultService.save(dto.getReportId(), requestResults); apiScenarioReportResultService.save(dto.getReportId(), dto.getRequestResults());
} }

View File

@ -65,7 +65,6 @@ public class TestResultService {
if (dto.getArbitraryData() != null && dto.getArbitraryData().containsKey("ENV")) { if (dto.getArbitraryData() != null && dto.getArbitraryData().containsKey("ENV")) {
environmentList = (List<String>) dto.getArbitraryData().get("ENV"); environmentList = (List<String>) dto.getArbitraryData().get("ENV");
} }
List<RequestResult> requestResults = dto.getRequestResults();
//处理环境参数 //处理环境参数
if (CollectionUtils.isNotEmpty(environmentList)) { if (CollectionUtils.isNotEmpty(environmentList)) {
apiEnvironmentRunningParamService.parseEnvironment(environmentList); apiEnvironmentRunningParamService.parseEnvironment(environmentList);
@ -73,15 +72,15 @@ public class TestResultService {
} }
//测试计划定时任务-接口执行逻辑的话需要同步测试计划的报告数据 //测试计划定时任务-接口执行逻辑的话需要同步测试计划的报告数据
if (StringUtils.equalsAny(dto.getRunMode(), ApiRunMode.SCHEDULE_API_PLAN.name(), ApiRunMode.JENKINS_API_PLAN.name(), ApiRunMode.MANUAL_PLAN.name())) { if (StringUtils.equalsAny(dto.getRunMode(), ApiRunMode.SCHEDULE_API_PLAN.name(), ApiRunMode.JENKINS_API_PLAN.name(), ApiRunMode.MANUAL_PLAN.name())) {
apiDefinitionExecResultService.saveApiResultByScheduleTask(requestResults, dto); apiDefinitionExecResultService.saveApiResultByScheduleTask(dto);
} else if (StringUtils.equalsAny(dto.getRunMode(), ApiRunMode.DEFINITION.name(), ApiRunMode.JENKINS.name(), ApiRunMode.API_PLAN.name(), ApiRunMode.SCHEDULE_API_PLAN.name(), ApiRunMode.JENKINS_API_PLAN.name(), ApiRunMode.MANUAL_PLAN.name())) { } else if (StringUtils.equalsAny(dto.getRunMode(), ApiRunMode.DEFINITION.name(), ApiRunMode.JENKINS.name(), ApiRunMode.API_PLAN.name(), ApiRunMode.SCHEDULE_API_PLAN.name(), ApiRunMode.JENKINS_API_PLAN.name(), ApiRunMode.MANUAL_PLAN.name())) {
apiDefinitionExecResultService.saveApiResult(requestResults, dto); apiDefinitionExecResultService.saveApiResult(dto);
} else if (StringUtils.equalsAny(dto.getRunMode(), ApiRunMode.SCENARIO.name(), ApiRunMode.SCENARIO_PLAN.name(), ApiRunMode.SCHEDULE_SCENARIO_PLAN.name(), ApiRunMode.SCHEDULE_SCENARIO.name(), ApiRunMode.JENKINS_SCENARIO_PLAN.name())) { } else if (StringUtils.equalsAny(dto.getRunMode(), ApiRunMode.SCENARIO.name(), ApiRunMode.SCENARIO_PLAN.name(), ApiRunMode.SCHEDULE_SCENARIO_PLAN.name(), ApiRunMode.SCHEDULE_SCENARIO.name(), ApiRunMode.JENKINS_SCENARIO_PLAN.name())) {
apiScenarioReportService.saveResult(requestResults, dto); apiScenarioReportService.saveResult(dto);
} else if (StringUtils.equalsAny(dto.getRunMode(), ApiRunMode.UI_SCENARIO.name(), ApiRunMode.UI_SCENARIO_PLAN.name(), ApiRunMode.UI_JENKINS_SCENARIO_PLAN.name(), ApiRunMode.UI_SCHEDULE_SCENARIO.name(), ApiRunMode.UI_SCHEDULE_SCENARIO_PLAN.name())) { } else if (StringUtils.equalsAny(dto.getRunMode(), ApiRunMode.UI_SCENARIO.name(), ApiRunMode.UI_SCENARIO_PLAN.name(), ApiRunMode.UI_JENKINS_SCENARIO_PLAN.name(), ApiRunMode.UI_SCHEDULE_SCENARIO.name(), ApiRunMode.UI_SCHEDULE_SCENARIO_PLAN.name())) {
apiScenarioReportService.saveUiResult(requestResults, dto); apiScenarioReportService.saveUiResult(dto.getRequestResults(), dto);
} }
updateTestCaseStates(requestResults, dto.getRunMode()); updateTestCaseStates(dto.getRequestResults(), dto.getRunMode());
} }
public void batchSaveResults(Map<String, List<ResultDTO>> resultDtoMap) { public void batchSaveResults(Map<String, List<ResultDTO>> resultDtoMap) {

View File

@ -18,7 +18,7 @@ public class PerformanceQueueEvent implements LoadTestFinishEvent {
public void sendNotice(LoadTestReport loadTestReport) { public void sendNotice(LoadTestReport loadTestReport) {
//删除性能测试在执行队列中的数据 在测试计划执行中会将性能测试执行添加到执行队列用于判断整个测试计划到执行进度 //删除性能测试在执行队列中的数据 在测试计划执行中会将性能测试执行添加到执行队列用于判断整个测试计划到执行进度
try { try {
apiExecutionQueueService.checkExecutionQueneByLoadTest(loadTestReport); apiExecutionQueueService.checkExecutionQueueByLoadTest(loadTestReport);
} catch (Exception e) { } catch (Exception e) {
LogUtil.error("PerformanceQueueEvent error. id:" + loadTestReport.getId()); LogUtil.error("PerformanceQueueEvent error. id:" + loadTestReport.getId());
} }