refactor(接口测试): 停止所有任务增加详细日志

This commit is contained in:
fit2-zhao 2022-05-06 17:36:02 +08:00 committed by fit2-zhao
parent 64836a6af9
commit eaa94d74b3
4 changed files with 45 additions and 22 deletions

View File

@ -7,5 +7,6 @@ public class TaskRequest {
private String type;
private String reportId;
private String projectId;
private String userId;
}

View File

@ -460,6 +460,9 @@ public class ApiExecutionQueueService {
}
public void stop(List<String> reportIds) {
if (CollectionUtils.isEmpty(reportIds)) {
return;
}
ApiExecutionQueueDetailExample example = new ApiExecutionQueueDetailExample();
example.createCriteria().andReportIdIn(reportIds);
List<ApiExecutionQueueDetail> details = executionQueueDetailMapper.selectByExample(example);

View File

@ -17,13 +17,13 @@ import io.metersphere.base.mapper.ext.ExtApiScenarioReportMapper;
import io.metersphere.base.mapper.ext.ExtLoadTestReportMapper;
import io.metersphere.base.mapper.ext.ExtTaskMapper;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.SessionUtils;
import io.metersphere.dto.NodeDTO;
import io.metersphere.jmeter.LocalRunner;
import io.metersphere.performance.service.PerformanceTestService;
import io.metersphere.service.CheckPermissionService;
import io.metersphere.task.dto.TaskCenterDTO;
import io.metersphere.task.dto.TaskCenterRequest;
import io.metersphere.utils.LoggerUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
@ -103,25 +103,33 @@ public class TaskService {
public void send(Map<String, List<String>> poolMap) {
try {
for (String poolId : poolMap.keySet()) {
TestResourcePoolExample example = new TestResourcePoolExample();
example.createCriteria().andStatusEqualTo("VALID").andTypeEqualTo("NODE").andIdEqualTo(poolId);
List<TestResourcePool> pools = testResourcePoolMapper.selectByExample(example);
if (CollectionUtils.isNotEmpty(pools)) {
List<String> poolIds = pools.stream().map(pool -> pool.getId()).collect(Collectors.toList());
TestResourceExample resourceExample = new TestResourceExample();
resourceExample.createCriteria().andTestResourcePoolIdIn(poolIds);
List<TestResource> testResources = testResourceMapper.selectByExampleWithBLOBs(resourceExample);
for (TestResource testResource : testResources) {
String configuration = testResource.getConfiguration();
NodeDTO node = JSON.parseObject(configuration, NodeDTO.class);
String nodeIp = node.getIp();
Integer port = node.getPort();
String uri = String.format(JMeterService.BASE_URL + "/jmeter/stop", nodeIp, port);
restTemplate.postForEntity(uri, poolMap.get(poolId), void.class);
LoggerUtil.info("结束所有NODE中执行的资源");
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
Thread.currentThread().setName("STOP-NODE");
for (String poolId : poolMap.keySet()) {
TestResourcePoolExample example = new TestResourcePoolExample();
example.createCriteria().andStatusEqualTo("VALID").andTypeEqualTo("NODE").andIdEqualTo(poolId);
List<TestResourcePool> pools = testResourcePoolMapper.selectByExample(example);
if (CollectionUtils.isNotEmpty(pools)) {
List<String> poolIds = pools.stream().map(pool -> pool.getId()).collect(Collectors.toList());
TestResourceExample resourceExample = new TestResourceExample();
resourceExample.createCriteria().andTestResourcePoolIdIn(poolIds);
List<TestResource> testResources = testResourceMapper.selectByExampleWithBLOBs(resourceExample);
for (TestResource testResource : testResources) {
String configuration = testResource.getConfiguration();
NodeDTO node = JSON.parseObject(configuration, NodeDTO.class);
String nodeIp = node.getIp();
Integer port = node.getPort();
String uri = String.format(JMeterService.BASE_URL + "/jmeter/stop", nodeIp, port);
restTemplate.postForEntity(uri, poolMap.get(poolId), void.class);
}
}
}
}
}
});
thread.start();
} catch (Exception e) {
LogUtil.error(e.getMessage());
}
@ -161,19 +169,23 @@ public class TaskService {
} else {
try {
LoggerUtil.info("进入批量停止方法");
// 全部停止
Map<String, TaskRequest> taskRequestMap = taskRequests.stream().collect(Collectors.toMap(TaskRequest::getType, taskRequest -> taskRequest));
// 获取工作空间项目
LoggerUtil.info("获取工作空间对应的项目");
TaskCenterRequest taskCenterRequest = new TaskCenterRequest();
taskCenterRequest.setProjects(this.getOwnerProjectIds(SessionUtils.getUserId()));
taskCenterRequest.setProjects(this.getOwnerProjectIds(taskRequestMap.get("SCENARIO").getUserId()));
// 结束掉未分发完成的任务
LoggerUtil.info("结束正在进行中的计划任务队列");
JmeterThreadUtils.stop("PLAN-CASE");
JmeterThreadUtils.stop("API-CASE-RUN");
JmeterThreadUtils.stop("SCENARIO-PARALLEL-THREAD");
if (taskRequestMap.containsKey("API")) {
List<ApiDefinitionExecResult> results = extApiDefinitionExecResultMapper.findByProjectIds(taskCenterRequest);
LoggerUtil.info("查询API进行中的报告" + results.size());
if (CollectionUtils.isNotEmpty(results)) {
for (ApiDefinitionExecResult item : results) {
extracted(poolMap, item.getId(), item.getActuator());
@ -182,13 +194,16 @@ public class TaskService {
PoolExecBlockingQueueUtil.offer(item.getId());
}
}
LoggerUtil.info("结束API进行中的报告");
extTaskMapper.stopApi(taskCenterRequest);
// 清理队列并停止测试计划报告
LoggerUtil.info("清理API执行链");
List<String> ids = results.stream().map(ApiDefinitionExecResult::getId).collect(Collectors.toList());
apiExecutionQueueService.stop(ids);
}
if (taskRequestMap.containsKey("SCENARIO")) {
List<ApiScenarioReport> reports = extApiScenarioReportMapper.findByProjectIds(taskCenterRequest);
LoggerUtil.info("查询到执行中的场景报告:" + reports.size());
if (CollectionUtils.isNotEmpty(reports)) {
for (ApiScenarioReport report : reports) {
@ -199,13 +214,16 @@ public class TaskService {
}
// 清理队列并停止测试计划报告
LoggerUtil.info("结束所有进行中的场景报告 ");
List<String> ids = reports.stream().map(ApiScenarioReport::getId).collect(Collectors.toList());
extTaskMapper.stopScenario(taskCenterRequest);
// 清理队列并停止测试计划报告
LoggerUtil.info("清理队列并停止测试计划报告 ");
apiExecutionQueueService.stop(ids);
}
}
if (taskRequestMap.containsKey("PERFORMANCE")) {
LoggerUtil.info("开始结束性能测试报告 ");
List<LoadTestReport> loadTestReports = extLoadTestReportMapper.selectReportByProjectId(taskRequestMap.get("PERFORMANCE").getProjectId());
if (CollectionUtils.isNotEmpty(loadTestReports)) {
for (LoadTestReport loadTestReport : loadTestReports) {
@ -216,6 +234,7 @@ public class TaskService {
PoolExecBlockingQueueUtil.offer(loadTestReport.getId());
}
}
LoggerUtil.info("结束性能测试报告完成");
}
} catch (Exception e) {
LogUtil.error(e);

View File

@ -244,9 +244,9 @@ export default {
let request = {type: row.executionModule, reportId: row.id};
array = [request];
} else {
array.push({type: 'API', projectId: getCurrentProjectID()});
array.push({type: 'SCENARIO', projectId: getCurrentProjectID()});
array.push({type: 'PERFORMANCE', projectId: getCurrentProjectID()});
array.push({type: 'API', projectId: getCurrentProjectID(),userId:getCurrentUser().id});
array.push({type: 'SCENARIO', projectId: getCurrentProjectID(),userId:getCurrentUser().id});
array.push({type: 'PERFORMANCE', projectId: getCurrentProjectID(),userId:getCurrentUser().id});
}
this.$post('/api/automation/stop/batch', array, response => {
this.$success(this.$t('report.test_stop_success'));