refactor(接口测试): 优化执行过程资源池校验及结果处理

Signed-off-by: fit2-zhao <yong.zhao@fit2cloud.com>
This commit is contained in:
fit2-zhao 2023-04-20 16:51:20 +08:00 committed by fit2-zhao
parent e413afea11
commit 37882175e4
6 changed files with 46 additions and 40 deletions

View File

@ -191,14 +191,16 @@ public class JMeterService {
}
}
private synchronized void nodeDebug(JmeterRunRequestDTO request) {
private void nodeDebug(JmeterRunRequestDTO request) {
try {
LoggerUtil.info("获取调试资源池", request.getReportId());
List<TestResource> resources = GenerateHashTreeUtil.setPoolResource(request.getPoolId());
if (request.getHashTree() != null) {
// 过程变量处理
this.fileProcessing(request);
request.setHashTree(null);
}
LoggerUtil.info("调用资源池开始执行", request.getReportId());
apiPoolDebugService.run(request, resources);
} catch (Exception e) {
LoggerUtil.error(e);
@ -208,7 +210,7 @@ public class JMeterService {
}
}
private synchronized void send(JmeterRunRequestDTO request) {
private void send(JmeterRunRequestDTO request) {
try {
if (redisTemplate.opsForValue().get(SmoothWeighted.EXEC_INDEX + request.getPoolId()) != null) {
long index = Long.parseLong(redisTemplate.opsForValue().get(SmoothWeighted.EXEC_INDEX + request.getPoolId()).toString());

View File

@ -56,13 +56,16 @@ public class KafkaListenerTask implements Runnable {
LoggerUtil.info("KAFKA解析结果任务开始解析结果", String.valueOf(record.key()));
ResultDTO dto = this.formatResult();
if (dto == null) {
LoggerUtil.info("未获取到执行结果", String.valueOf(record.key()));
return;
}
redisTemplateService.delete(JmxFileUtil.getExecuteFileKeyInRedis(dto.getReportId()));
if (dto.getArbitraryData() != null && dto.getArbitraryData().containsKey(ExtendedParameter.TEST_END)
&& (Boolean) dto.getArbitraryData().get(ExtendedParameter.TEST_END)) {
redisTemplateService.delete(JmxFileUtil.getExecuteFileKeyInRedis(dto.getReportId()));
resultDTOS.add(dto);
// 全局并发队列
PoolExecBlockingQueueUtil.offer(dto.getReportId());
LoggerUtil.info("KAFKA消费结果处理状态" + dto.getArbitraryData().get(ExtendedParameter.TEST_END), String.valueOf(record.key()));
}
// 携带结果
@ -71,7 +74,7 @@ public class KafkaListenerTask implements Runnable {
if (assortMap.containsKey(key)) {
assortMap.get(key).add(dto);
} else {
assortMap.put(key, new LinkedList<ResultDTO>() {{
assortMap.put(key, new LinkedList<>() {{
this.add(dto);
}});
}
@ -88,8 +91,6 @@ public class KafkaListenerTask implements Runnable {
testResultService.testEnded(testResult);
LoggerUtil.info("执行队列处理:" + testResult.getQueueId(), testResult.getReportId());
apiExecutionQueueService.queueNext(testResult);
// 全局并发队列
PoolExecBlockingQueueUtil.offer(testResult.getReportId());
// 更新测试计划报告
LoggerUtil.info("Check Processing Test Plan report status" + testResult.getQueueId() + "" + testResult.getTestId(), testResult.getReportId());
apiExecutionQueueService.checkTestPlanCaseTestEnd(testResult.getTestId(), testResult.getRunMode(), testResult.getTestPlanReportId());

View File

@ -33,9 +33,9 @@ public class MsKafkaListener {
@Resource
private TestResultService testResultService;
// 线程池维护线程的最少数量
private final static int CORE_POOL_SIZE = 5;
private final static int CORE_POOL_SIZE = 10;
// 线程池维护线程的最大数量
private final static int MAX_POOL_SIZE = 5;
private final static int MAX_POOL_SIZE = 10;
// 线程池维护线程所允许的空闲时间
private final static int KEEP_ALIVE_TIME = 1;
// 线程池所使用的缓冲队列大小
@ -67,7 +67,6 @@ public class MsKafkaListener {
task.setRedisTemplateService(redisTemplateService);
threadPool.execute(task);
});
JvmUtil.memoryInfo();
} catch (Exception e) {
LoggerUtil.error("KAFKA消费失败", e);
} finally {

View File

@ -1,6 +1,6 @@
package io.metersphere.service;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.utils.LoggerUtil;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
@ -17,7 +17,7 @@ public class RedisTemplateService {
try {
return redisTemplate.opsForValue().setIfAbsent(key, value);
} catch (Exception e) {
LogUtil.error(e);
LoggerUtil.error(key, e);
return true;
}
}
@ -26,7 +26,7 @@ public class RedisTemplateService {
try {
return redisTemplate.expire(key, TIME_OUT, TimeUnit.MINUTES);
} catch (Exception e) {
LogUtil.error(e);
LoggerUtil.error(key, e);
return false;
}
}
@ -35,7 +35,7 @@ public class RedisTemplateService {
try {
return redisTemplate.opsForValue().get(key);
} catch (Exception e) {
LogUtil.error(e);
LoggerUtil.error(key, e);
}
return null;
}
@ -44,7 +44,7 @@ public class RedisTemplateService {
try {
return redisTemplate.delete(key);
} catch (Exception e) {
LogUtil.error(e);
LoggerUtil.error(key, e);
return false;
}
}

View File

@ -256,27 +256,27 @@ public class ApiScenarioReportService {
return report;
}
public ApiScenarioReportWithBLOBs editReport(String reportType, String reportId, String status, String runMode) {
ApiScenarioReportWithBLOBs report = apiScenarioReportMapper.selectByPrimaryKey(reportId);
if (report == null) {
report = new ApiScenarioReportWithBLOBs();
report.setId(reportId);
public ApiScenarioReport editReport(String reportType, String reportId, String status, String runMode) {
ApiScenarioReport report = apiScenarioReportMapper.selectByPrimaryKey(reportId);
if (report != null) {
if (StringUtils.equals(reportType, RunModeConstants.SET_REPORT.toString())
|| StringUtils.equals(report.getStatus(), ApiReportStatus.STOPPED.name())) {
return report;
}
ApiScenarioReportWithBLOBs bloBs = new ApiScenarioReportWithBLOBs();
if (StringUtils.equals(runMode, CommonConstants.CASE)) {
bloBs.setTriggerMode(TriggerMode.MANUAL.name());
}
bloBs.setId(reportId);
bloBs.setStatus(status);
bloBs.setName(report.getScenarioName() + "-" + DateUtils.getTimeStr(System.currentTimeMillis()));
bloBs.setEndTime(System.currentTimeMillis());
bloBs.setUpdateTime(System.currentTimeMillis());
if (StringUtils.isNotEmpty(report.getTriggerMode()) && report.getTriggerMode().equals(CommonConstants.CASE)) {
bloBs.setTriggerMode(TriggerMode.MANUAL.name());
}
apiScenarioReportMapper.updateByPrimaryKeySelective(bloBs);
}
if (StringUtils.equals(reportType, RunModeConstants.SET_REPORT.toString())
|| StringUtils.equals(report.getStatus(), ApiReportStatus.STOPPED.name())) {
return report;
}
if (StringUtils.equals(runMode, CommonConstants.CASE)) {
report.setTriggerMode(TriggerMode.MANUAL.name());
}
report.setStatus(status);
report.setName(report.getScenarioName() + "-" + DateUtils.getTimeStr(System.currentTimeMillis()));
report.setEndTime(System.currentTimeMillis());
report.setUpdateTime(System.currentTimeMillis());
if (StringUtils.isNotEmpty(report.getTriggerMode()) && report.getTriggerMode().equals(CommonConstants.CASE)) {
report.setTriggerMode(TriggerMode.MANUAL.name());
}
apiScenarioReportMapper.updateByPrimaryKeySelective(report);
return report;
}
@ -340,7 +340,7 @@ public class ApiScenarioReportService {
List<String> testPlanReportIdList = new ArrayList<>();
ResultVO resultVO = ReportStatusUtil.computedProcess(dto);
ApiScenarioReportWithBLOBs report = editReport(dto.getReportType(), dto.getReportId(), resultVO.getStatus(), dto.getRunMode());
ApiScenarioReport report = editReport(dto.getReportType(), dto.getReportId(), resultVO.getStatus(), dto.getRunMode());
if (report != null) {
if (StringUtils.isNotEmpty(dto.getTestPlanReportId()) && !testPlanReportIdList.contains(dto.getTestPlanReportId())) {
testPlanReportIdList.add(dto.getTestPlanReportId());

View File

@ -125,14 +125,18 @@ public class ApiScenarioReportStructureService {
public void update(String reportId, String console, boolean isActuator) {
ApiScenarioReportStructureExample example = new ApiScenarioReportStructureExample();
example.createCriteria().andReportIdEqualTo(reportId);
List<ApiScenarioReportStructureWithBLOBs> structures = mapper.selectByExampleWithBLOBs(example);
for (ApiScenarioReportStructureWithBLOBs structure : structures) {
List<ApiScenarioReportStructure> structures = mapper.selectByExample(example);
if (CollectionUtils.isNotEmpty(structures)) {
ApiScenarioReportStructureWithBLOBs bloBs = new ApiScenarioReportStructureWithBLOBs();
bloBs.setId(structures.get(0).getId());
if (isActuator) {
structure.setConsole(StringUtils.join(structure.getConsole(), StringUtils.LF, console));
// 集合报告日志合并处理
bloBs = mapper.selectByPrimaryKey(structures.get(0).getId());
bloBs.setConsole(StringUtils.join(bloBs.getConsole(), StringUtils.LF, console));
} else {
structure.setConsole(console);
bloBs.setConsole(console);
}
mapper.updateByPrimaryKeyWithBLOBs(structure);
mapper.updateByPrimaryKeySelective(bloBs);
}
}