refactor(接口测试): 定时检查测试计划报告状态

This commit is contained in:
fit2-zhao 2021-12-16 15:26:36 +08:00 committed by fit2-zhao
parent 2fd6dc1316
commit b4f2854b0e
3 changed files with 48 additions and 30 deletions

View File

@ -0,0 +1,36 @@
package io.metersphere.api.exec.schedule;
import io.metersphere.api.cache.TestPlanReportExecuteCatch;
import io.metersphere.api.jmeter.MessageCache;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.track.service.TestPlanReportService;
import io.metersphere.utils.LoggerUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class TestPlanReportListenerScheduled {
/**
* 定时调用监听检查报告状态
*/
@Scheduled(cron = "*/9 * * * * ?")
public void testPlanScheduled() {
//判断缓冲队列是否存在记录
if (CollectionUtils.isNotEmpty(MessageCache.jobReportCache)) {
for (int i = 0; i < MessageCache.jobReportCache.size(); i++) {
this.listener(MessageCache.jobReportCache.get(i));
}
}
}
private void listener(String planReportId) {
LoggerUtil.info("检查测试计划执行报告:【" + planReportId + "");
if (TestPlanReportExecuteCatch.getTestPlanExecuteInfo(planReportId) != null) {
CommonBeanFactory.getBean(TestPlanReportService.class).countReport(planReportId);
} else {
MessageCache.jobReportCache.remove(planReportId);
LoggerUtil.info("测试计划执行报告:【" + planReportId + "】执行完成,剩余队列:" + MessageCache.jobReportCache.size());
}
}
}

View File

@ -4,16 +4,20 @@ import io.metersphere.base.domain.ApiDefinitionExecResult;
import javax.websocket.Session;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class MessageCache {
public static Map<String, ReportCounter> concurrencyCounter = new HashMap<>();
public static ConcurrentHashMap<String, Session> reportCache = new ConcurrentHashMap<>();
public final static Map<String, ReportCounter> concurrencyCounter = new HashMap<>();
public final static ConcurrentHashMap<String, Session> reportCache = new ConcurrentHashMap<>();
// 用例并发锁
public static ConcurrentHashMap<String, ApiDefinitionExecResult> caseExecResourceLock = new ConcurrentHashMap<>();
public final static ConcurrentHashMap<String, ApiDefinitionExecResult> caseExecResourceLock = new ConcurrentHashMap<>();
public static Map<String, Long> jmeterLogTask = new HashMap<>();
public final static Map<String, Long> jmeterLogTask = new HashMap<>();
// 定时任务报告
public final static List<String> jobReportCache = new LinkedList<>();
}

View File

@ -22,6 +22,7 @@ import io.metersphere.api.dto.definition.request.MsTestPlan;
import io.metersphere.api.dto.definition.request.MsThreadGroup;
import io.metersphere.api.dto.definition.request.variable.ScenarioVariable;
import io.metersphere.api.jmeter.JMeterService;
import io.metersphere.api.jmeter.MessageCache;
import io.metersphere.api.service.ApiAutomationService;
import io.metersphere.api.service.ApiDefinitionService;
import io.metersphere.api.service.ApiScenarioReportService;
@ -59,6 +60,7 @@ import io.metersphere.track.request.testplan.LoadCaseRequest;
import io.metersphere.track.request.testplan.TestplanRunRequest;
import io.metersphere.track.request.testplancase.QueryTestPlanCaseRequest;
import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType;
@ -71,7 +73,6 @@ import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
@ -1071,8 +1072,7 @@ public class TestPlanService {
String planReportId = testPlanReport.getId();
testPlanLog.info("ReportId[" + planReportId + "] created. TestPlanID:[" + testPlanID + "]. " + "API Run Config:【" + apiRunConfig + "");
//开启测试计划执行状态的监听
this.listenTaskExecuteStatus(planReportId);
MessageCache.jobReportCache.add(planReportId);
//不同任务的执行ID
Map<String, String> executePerformanceIdMap = new HashMap<>();
Map<String, String> executeApiCaseIdMap = new HashMap<>();
@ -1146,28 +1146,6 @@ public class TestPlanService {
return testPlanReport.getId();
}
private void listenTaskExecuteStatus(String planReportId) {
// 开始串行执行
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
Thread.currentThread().setName("TestPlanListener");
try {
//10s 查询一次状态
Thread.sleep(10000);
while (TestPlanReportExecuteCatch.getTestPlanExecuteInfo(planReportId) != null) {
testPlanReportService.countReport(planReportId);
Thread.sleep(10000);
}
} catch (InterruptedException e) {
TestPlanReportExecuteCatch.remove(planReportId);
LogUtil.error(e);
}
}
});
thread.start();
}
private void executeApiTestCase(String triggerMode, String planReportId, String userId, List<String> planCaseIds, RunModeConfigDTO runModeConfig) {
BatchRunDefinitionRequest request = new BatchRunDefinitionRequest();
if (StringUtils.equals(triggerMode, ReportTriggerMode.API.name())) {