refactor(接口测试): 场景执行相关代码优化

This commit is contained in:
fit2-zhao 2022-07-26 15:36:25 +08:00 committed by fit2-zhao
parent 887ae72be2
commit 0e090ba9e0
7 changed files with 134 additions and 135 deletions

View File

@ -1,6 +1,6 @@
package io.metersphere.api.controller;
import io.metersphere.api.jmeter.JmeterThreadUtils;
import io.metersphere.api.jmeter.JMeterThreadUtils;
import io.metersphere.api.service.ApiJmeterFileService;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
@ -19,7 +19,7 @@ public class ApiJmeterFileController {
@GetMapping("stop/{name}")
public String stop(@PathVariable String name) {
return JmeterThreadUtils.stop(name);
return JMeterThreadUtils.stop(name);
}
@GetMapping("download/jar")

View File

@ -1,7 +1,7 @@
package io.metersphere.api.exec.queue;
import io.metersphere.api.jmeter.JMeterService;
import io.metersphere.api.jmeter.JmeterThreadUtils;
import io.metersphere.api.jmeter.JMeterThreadUtils;
import io.metersphere.cache.JMeterEngineCache;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.dto.JmeterRunRequestDTO;
@ -22,7 +22,7 @@ public class ExecTask implements Runnable {
public void run() {
CommonBeanFactory.getBean(JMeterService.class).addQueue(request);
Object res = PoolExecBlockingQueueUtil.take(request.getReportId());
if (res == null && !JmeterThreadUtils.isRunning(request.getReportId(), request.getTestId())) {
if (res == null && !JMeterThreadUtils.isRunning(request.getReportId(), request.getTestId())) {
LoggerUtil.info("任务执行超时", request.getReportId());
if (JMeterEngineCache.runningEngine.containsKey(request.getReportId())) {
JMeterEngineCache.runningEngine.remove(request.getReportId());

View File

@ -18,10 +18,7 @@ import io.metersphere.api.service.ApiExecutionQueueService;
import io.metersphere.api.service.ApiScenarioReportService;
import io.metersphere.api.service.ApiScenarioReportStructureService;
import io.metersphere.api.service.TcpApiParamService;
import io.metersphere.base.domain.ApiScenarioExample;
import io.metersphere.base.domain.ApiScenarioReportWithBLOBs;
import io.metersphere.base.domain.ApiScenarioWithBLOBs;
import io.metersphere.base.domain.TestPlanApiScenario;
import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.ApiScenarioMapper;
import io.metersphere.base.mapper.ApiScenarioReportMapper;
import io.metersphere.base.mapper.TestPlanApiScenarioMapper;
@ -29,7 +26,6 @@ import io.metersphere.base.mapper.ext.ExtApiScenarioMapper;
import io.metersphere.commons.constants.APITestStatus;
import io.metersphere.commons.constants.ApiRunMode;
import io.metersphere.commons.constants.ReportTriggerMode;
import io.metersphere.commons.constants.ReportTypeConstants;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.*;
import io.metersphere.constants.RunModeConstants;
@ -45,6 +41,7 @@ import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.collections4.comparators.FixedOrderComparator;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jorphan.collections.HashTree;
import org.springframework.context.annotation.Lazy;
@ -107,7 +104,7 @@ public class ApiScenarioExecuteService {
request.getConfig().setEnvMap(environmentGroupProjectService.getEnvMap(request.getConfig().getEnvironmentGroupId()));
}
// 生成集成报告
// 生成集成报告id
String serialReportId = request.isRerun() && GenerateHashTreeUtil.isSetReport(request.getConfig()) ? request.getSerialReportId() : null;
LoggerUtil.info("Scenario run-执行脚本装载-根据条件查询所有场景 ");
List<ApiScenarioWithBLOBs> apiScenarios = this.get(request);
@ -129,17 +126,14 @@ public class ApiScenarioExecuteService {
}
Map<String, RunModeDataDTO> executeQueue = new LinkedHashMap<>();
List<String> scenarioIds = new ArrayList<>();
StringBuilder scenarioNames = new StringBuilder();
LoggerUtil.info("Scenario run-执行脚本装载-初始化执行队列");
if (StringUtils.equalsAny(request.getRunMode(), ApiRunMode.SCENARIO_PLAN.name(),
ApiRunMode.SCHEDULE_SCENARIO_PLAN.name(), ApiRunMode.JENKINS_SCENARIO_PLAN.name())) {
//测试计划执行
assemblyPlanScenario(apiScenarios, request, executeQueue, scenarioIds, scenarioNames);
assemblyPlanScenario(apiScenarios, request, executeQueue);
} else {
// 按照场景执行
assemblyScenario(apiScenarios, request, executeQueue, scenarioIds, scenarioNames, serialReportId);
assemblyScenario(apiScenarios, request, executeQueue);
}
LoggerUtil.info("Scenario run-执行脚本装载-初始化执行队列完成:" + executeQueue.size());
@ -147,6 +141,7 @@ public class ApiScenarioExecuteService {
if (executeQueue.isEmpty()) {
return responseDTOS;
}
// 集合报告处理
if (GenerateHashTreeUtil.isSetReport(request.getConfig())) {
// 失败重跑更新报告状态
if (request.isRerun()) {
@ -157,6 +152,7 @@ public class ApiScenarioExecuteService {
} else {
LoggerUtil.info("Scenario run-执行脚本装载-初始化集成报告:" + serialReportId);
request.getConfig().setReportId(UUID.randomUUID().toString());
List<String> scenarioIds = new ArrayList<>();
String reportScenarioIds = generateScenarioIds(scenarioIds);
if (request.getConfig() == null) {
request.setConfig(new RunModeConfigWithEnvironmentDTO());
@ -168,7 +164,9 @@ public class ApiScenarioExecuteService {
apiCaseExecuteService.setExecutionEnvironment(runModeConfig, projectEnvMap);
request.setConfig(runModeConfig);
}
APIScenarioReportResult report = getApiScenarioReportResult(request, serialReportId, scenarioNames, reportScenarioIds);
// 生成集合报告
String names = apiScenarios.stream().map(ApiScenario::getName).collect(Collectors.joining(","));
APIScenarioReportResult report = apiScenarioReportService.getApiScenarioReportResult(request, serialReportId, names, reportScenarioIds);
report.setVersionId(apiScenarios.get(0).getVersionId());
apiScenarioReportMapper.insert(report);
@ -189,14 +187,12 @@ public class ApiScenarioExecuteService {
}
// 开始执行
execute(request, serialReportId, executeQueue, executionQueue);
return responseDTOS;
}
protected void execute(RunScenarioRequest request, String serialReportId, Map<String, RunModeDataDTO> executeQueue, DBTestQueue executionQueue) {
String finalSerialReportId = serialReportId;
Thread thread = new Thread(() ->
{
Thread thread = new Thread(() -> {
Thread.currentThread().setName("SCENARIO-THREAD");
if (isSerial(request)) {
apiScenarioSerialService.serial(executionQueue);
@ -217,22 +213,6 @@ public class ApiScenarioExecuteService {
return JSON.toJSONString(CollectionUtils.isNotEmpty(scenarioIds) && scenarioIds.size() > 50 ? scenarioIds.subList(0, 50) : scenarioIds);
}
protected APIScenarioReportResult getApiScenarioReportResult(RunScenarioRequest request, String serialReportId,
StringBuilder scenarioNames, String reportScenarioIds) {
APIScenarioReportResult report = apiScenarioReportService.init(request.getConfig().getReportId(), reportScenarioIds,
scenarioNames.toString(), request.getTriggerMode(), ExecuteType.Saved.name(), request.getProjectId(),
request.getReportUserID(), request.getConfig());
report.setName(request.getConfig().getReportName());
report.setId(serialReportId);
report.setReportType(ReportTypeConstants.SCENARIO_INTEGRATED.name());
request.getConfig().setAmassReport(serialReportId);
if (request.getConfig() != null) {
report.setEnvConfig(JSON.toJSONString(request.getConfig()));
}
report.setStatus(APITestStatus.Running.name());
return report;
}
public List<ApiScenarioWithBLOBs> get(RunScenarioRequest request) {
ServiceUtils.getSelectAllIds(request, request.getCondition(),
(query) -> extApiScenarioMapper.selectIdsByQuery(query));
@ -260,61 +240,57 @@ public class ApiScenarioExecuteService {
/**
* 测试计划接口场景的预执行生成场景报告
*/
private void assemblyPlanScenario(List<ApiScenarioWithBLOBs> apiScenarios, RunScenarioRequest request, Map<String, RunModeDataDTO> executeQueue, List<String> scenarioIds, StringBuilder scenarioNames) {
private void assemblyPlanScenario(List<ApiScenarioWithBLOBs> apiScenarios, RunScenarioRequest request, Map<String, RunModeDataDTO> executeQueue) {
String reportId = request.getId();
Map<String, String> planScenarioIdMap = request.getScenarioTestPlanIdMap();
if (MapUtils.isEmpty(planScenarioIdMap)) {
return;
}
String projectId = request.getProjectId();
Map<String, ApiScenarioWithBLOBs> scenarioMap = apiScenarios.stream().collect(Collectors.toMap(ApiScenarioWithBLOBs::getId, Function.identity(), (t1, t2) -> t1));
for (Map.Entry<String, String> entry : planScenarioIdMap.entrySet()) {
String testPlanScenarioId = entry.getKey();
String scenarioId = entry.getValue();
for (String testPlanScenarioId : planScenarioIdMap.keySet()) {
String scenarioId = planScenarioIdMap.get(testPlanScenarioId);
ApiScenarioWithBLOBs scenario = scenarioMap.get(scenarioId);
if (scenario.getStepTotal() == null || scenario.getStepTotal() == 0) {
continue;
}
if (StringUtils.isEmpty(request.getProjectId())) {
request.setProjectId(scenario.getProjectId());
}
// 获取场景用例单独的执行环境
Map<String, String> planEnvMap = new HashMap<>();
TestPlanApiScenario planApiScenario = testPlanApiScenarioMapper.selectByPrimaryKey(testPlanScenarioId);
String envJson = planApiScenario.getEnvironment();
String envType = planApiScenario.getEnvironmentType();
String envGroupId = planApiScenario.getEnvironmentGroupId();
if (StringUtils.equals(envType, EnvironmentType.JSON.toString()) && StringUtils.isNotBlank(envJson)) {
planEnvMap = JSON.parseObject(envJson, Map.class);
} else if (StringUtils.equals(envType, EnvironmentType.GROUP.toString()) && StringUtils.isNotBlank(envGroupId)) {
planEnvMap = environmentGroupProjectService.getEnvMap(envGroupId);
if (planApiScenario == null) {
continue;
}
if (StringUtils.isEmpty(projectId)) {
projectId = testPlanScenarioCaseService.getProjectIdById(testPlanScenarioId);
// 环境处理
if (StringUtils.equals(planApiScenario.getEnvironmentType(), EnvironmentType.JSON.toString())
&& StringUtils.isNotBlank(planApiScenario.getEnvironment())) {
planEnvMap = JSON.parseObject(planApiScenario.getEnvironment(), Map.class);
} else if (StringUtils.equals(planApiScenario.getEnvironmentType(), EnvironmentType.GROUP.toString())
&& StringUtils.isNotBlank(planApiScenario.getEnvironmentGroupId())) {
planEnvMap = environmentGroupProjectService.getEnvMap(planApiScenario.getEnvironmentGroupId());
}
if (StringUtils.isEmpty(projectId)) {
projectId = scenario.getProjectId();
if (StringUtils.isEmpty(request.getProjectId())) {
request.setProjectId(testPlanScenarioCaseService.getProjectIdById(testPlanScenarioId));
}
APIScenarioReportResult report = request.isRerun() ? request.getReportMap().get(scenarioId) :
apiScenarioReportService.init(reportId, testPlanScenarioId, scenario.getName(), request.getTriggerMode(),
request.getExecuteType(), projectId, request.getReportUserID(), request.getConfig());
if (report == null) {
report = request.getReportMap().get(testPlanScenarioId);
APIScenarioReportResult report = apiScenarioReportService.initResult(reportId, testPlanScenarioId, scenario.getName(), request);
if (request.isRerun()) {
if (request.getReportMap().containsKey(scenarioId)) {
report = request.getReportMap().get(scenarioId);
} else if (request.getReportMap().containsKey(testPlanScenarioId)) {
report = request.getReportMap().get(testPlanScenarioId);
}
}
if (report == null) {
return;
}
report.setVersionId(scenario.getVersionId());
scenarioIds.add(scenario.getId());
RunModeDataDTO runModeDataDTO = new RunModeDataDTO();
runModeDataDTO.setTestId(testPlanScenarioId);
RunModeDataDTO runModeDataDTO = getRunModeDataDTO(testPlanScenarioId, report);
runModeDataDTO.setPlanEnvMap(planEnvMap);
runModeDataDTO.setReport(report);
runModeDataDTO.setReportId(report.getId());
runModeDataDTO.setScenario(scenario);
executeQueue.put(report.getId(), runModeDataDTO);
scenarioNames.append(scenario.getName()).append(",");
if (request.getConfig() != null) {
if (ObjectUtils.isNotEmpty(request.getConfig())) {
RunModeConfigWithEnvironmentDTO runModeConfig = new RunModeConfigWithEnvironmentDTO();
BeanUtils.copyBean(runModeConfig, request.getConfig());
if (MapUtils.isEmpty(runModeConfig.getEnvMap())) {
@ -334,7 +310,7 @@ public class ApiScenarioExecuteService {
/**
* 接口场景的预执行生成场景报告
*/
private void assemblyScenario(List<ApiScenarioWithBLOBs> apiScenarios, RunScenarioRequest request, Map<String, RunModeDataDTO> executeQueue, List<String> scenarioIds, StringBuilder scenarioNames, String serialReportId) {
private void assemblyScenario(List<ApiScenarioWithBLOBs> apiScenarios, RunScenarioRequest request, Map<String, RunModeDataDTO> executeQueue) {
String reportId = request.getId();
for (int i = 0; i < apiScenarios.size(); i++) {
ApiScenarioWithBLOBs item = apiScenarios.get(i);
@ -342,26 +318,17 @@ public class ApiScenarioExecuteService {
continue;
}
if (MapUtils.isEmpty(request.getConfig().getEnvMap())) {
RunModeConfigWithEnvironmentDTO runModeConfig = new RunModeConfigWithEnvironmentDTO();
BeanUtils.copyBean(runModeConfig, request.getConfig());
Map<String, List<String>> projectEnvMap = apiScenarioEnvService.selectApiScenarioEnv(apiScenarios);
apiCaseExecuteService.setExecutionEnvironment(runModeConfig, projectEnvMap);
request.setConfig(runModeConfig);
}
RunModeConfigWithEnvironmentDTO runModeConfig = new RunModeConfigWithEnvironmentDTO();
BeanUtils.copyBean(runModeConfig, request.getConfig());
if(StringUtils.equals(runModeConfig.getEnvironmentType(), EnvironmentType.JSON.name()) && MapUtils.isEmpty(runModeConfig.getEnvMap())){
Map<String, List<String>> projectEnvMap = apiScenarioEnvService.selectApiScenarioEnv(new ArrayList<>(){{this.add(item);}});
if (StringUtils.equals(runModeConfig.getEnvironmentType(), EnvironmentType.JSON.name()) && MapUtils.isEmpty(runModeConfig.getEnvMap())) {
Map<String, List<String>> projectEnvMap = apiScenarioEnvService.selectApiScenarioEnv(new ArrayList<>() {{
this.add(item);
}});
runModeConfig.setExecutionEnvironmentMap(projectEnvMap);
request.setConfig(runModeConfig);
}
APIScenarioReportResult report = apiScenarioReportService.init(reportId, item.getId(), item.getName(), request.getTriggerMode(),
request.getExecuteType(), item.getProjectId(), request.getReportUserID(), runModeConfig);
scenarioIds.add(item.getId());
APIScenarioReportResult report = apiScenarioReportService.initResult(reportId, item.getId(), item.getName(), request);
report.setVersionId(item.getVersionId());
scenarioNames.append(item.getName()).append(",");
RunModeDataDTO runModeDataDTO = getRunModeDataDTO(item.getId(), report);
runModeDataDTO.setScenario(item);
@ -379,9 +346,9 @@ public class ApiScenarioExecuteService {
}
}
protected RunModeDataDTO getRunModeDataDTO(String id, APIScenarioReportResult report) {
protected RunModeDataDTO getRunModeDataDTO(String testId, APIScenarioReportResult report) {
RunModeDataDTO runModeDataDTO = new RunModeDataDTO();
runModeDataDTO.setTestId(id);
runModeDataDTO.setTestId(testId);
runModeDataDTO.setPlanEnvMap(new HashMap<>());
runModeDataDTO.setReport(report);
runModeDataDTO.setReportId(report.getId());
@ -395,50 +362,29 @@ public class ApiScenarioExecuteService {
}
public String debug(RunDefinitionRequest request, List<MultipartFile> bodyFiles, List<MultipartFile> scenarioFiles) {
Map<String, String> map = request.getEnvironmentMap();
String envType = request.getEnvironmentType();
if (StringUtils.equals(envType, EnvironmentType.GROUP.toString())) {
String environmentGroupId = request.getEnvironmentGroupId();
map = environmentGroupProjectService.getEnvMap(environmentGroupId);
if (StringUtils.equals(request.getEnvironmentType(), EnvironmentType.GROUP.toString())) {
request.setEnvironmentMap(environmentGroupProjectService.getEnvMap(request.getEnvironmentGroupId()));
}
ParameterConfig config = new ParameterConfig();
config.setScenarioId(request.getScenarioId());
if (map != null) {
apiScenarioEnvService.setEnvConfig(map, config);
}
HashTree hashTree = null;
try {
uploadBodyFiles(request.getBodyFileRequestIds(), bodyFiles);
FileUtils.createBodyFiles(request.getScenarioFileIds(), scenarioFiles);
this.testElement(request);
hashTree = request.getTestElement().generateHashTree(config);
LogUtil.info(request.getTestElement().getJmx(hashTree));
} catch (Exception e) {
LoggerUtil.error("调试失败", request.getReportId(), e);
MSException.throwException(e.getMessage());
if (MapUtils.isNotEmpty(request.getEnvironmentMap())) {
apiScenarioEnvService.setEnvConfig(request.getEnvironmentMap(), config);
}
if (request.isSaved()) {
//记录环境
RunModeConfigDTO runModeCconfig = request.getConfig();
if (runModeCconfig == null) {
runModeCconfig = new RunModeConfigDTO();
runModeCconfig.setEnvMap(map);
if (request.getConfig() == null) {
RunModeConfigDTO configDTO = new RunModeConfigDTO();
configDTO.setEnvMap(request.getEnvironmentMap());
request.setConfig(configDTO);
}
APIScenarioReportResult report = apiScenarioReportService.init(request.getId(),
request.getScenarioId(),
request.getScenarioName(),
ReportTriggerMode.MANUAL.name(),
request.getExecuteType(),
request.getProjectId(),
SessionUtils.getUserId(),
runModeCconfig);
// 生成调试结果
APIScenarioReportResult report = apiScenarioReportService.initDebugResult(request);
ApiScenarioWithBLOBs scenario = apiScenarioMapper.selectByPrimaryKey(request.getScenarioId());
String reportType = request.getConfig() != null ? request.getConfig().getReportType() : null;
if (scenario != null) {
report.setVersionId(scenario.getVersionId());
String scenarioDefinition = JSON.toJSONString(request.getTestElement().getHashTree().get(0).getHashTree().get(0));
scenario.setScenarioDefinition(scenarioDefinition);
apiScenarioReportStructureService.save(scenario, report.getId(), reportType);
apiScenarioReportStructureService.save(scenario, report.getId(), request.getConfig().getReportType());
} else {
if (request.getTestElement() != null && CollectionUtils.isNotEmpty(request.getTestElement().getHashTree())) {
ApiScenarioWithBLOBs apiScenario = new ApiScenarioWithBLOBs();
@ -447,14 +393,19 @@ public class ApiScenarioExecuteService {
if (testElement != null) {
apiScenario.setName(testElement.getName());
apiScenario.setScenarioDefinition(JSON.toJSONString(testElement));
apiScenarioReportStructureService.save(apiScenario, report.getId(), reportType);
apiScenarioReportStructureService.save(apiScenario, report.getId(), request.getConfig().getReportType());
}
}
}
apiScenarioReportMapper.insert(report);
}
String runMode = StringUtils.isEmpty(request.getRunMode()) ? ApiRunMode.SCENARIO.name() : request.getRunMode();
// 调用执行方法
LoggerUtil.info("调用调试方法,开始执行", request.getId());
uploadBodyFiles(request.getBodyFileRequestIds(), bodyFiles);
FileUtils.createBodyFiles(request.getScenarioFileIds(), scenarioFiles);
this.testElement(request);
HashTree hashTree = request.getTestElement().generateHashTree(config);
String runMode = StringUtils.isEmpty(request.getRunMode()) ? ApiRunMode.SCENARIO.name() : request.getRunMode();
JmeterRunRequestDTO runRequest = new JmeterRunRequestDTO(request.getId(), request.getId(), runMode, hashTree);
LoggerUtil.info(new MsTestPlan().getJmx(hashTree));
runRequest.setDebug(true);
@ -469,5 +420,20 @@ public class ApiScenarioExecuteService {
});
}
}
/**
* 暂时保留给UI使用
*
* @param request
* @param reportId
* @param name
* @param ids
* @return
*/
@Deprecated
protected APIScenarioReportResult getApiScenarioReportResult(RunScenarioRequest request, String reportId,
StringBuilder name, String ids) {
return apiScenarioReportService.getApiScenarioReportResult(request, reportId, name.toString(), ids);
}
}

View File

@ -6,7 +6,7 @@ import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.LogUtil;
import org.apache.commons.lang3.StringUtils;
public class JmeterThreadUtils {
public class JMeterThreadUtils {
public static String stop(String name) {
ThreadGroup currentGroup = Thread.currentThread().getThreadGroup();
@ -16,7 +16,6 @@ public class JmeterThreadUtils {
StringBuilder threadNames = new StringBuilder();
for (int i = 0; i < noThreads; i++) {
if (lstThreads[i]!=null && StringUtils.isNotEmpty(lstThreads[i].getName()) && lstThreads[i].getName().startsWith(name)) {
System.out.println("异常强制处理线程编号:" + i + " = " + lstThreads[i].getName());
LogUtil.error("异常强制处理线程编号:" + i + " = " + lstThreads[i].getName());
threadNames.append(lstThreads[i].getName()).append("");
lstThreads[i].interrupt();

View File

@ -9,7 +9,7 @@ import io.metersphere.api.exec.api.ApiCaseSerialService;
import io.metersphere.api.exec.queue.DBTestQueue;
import io.metersphere.api.exec.scenario.ApiScenarioSerialService;
import io.metersphere.api.jmeter.JMeterService;
import io.metersphere.api.jmeter.JmeterThreadUtils;
import io.metersphere.api.jmeter.JMeterThreadUtils;
import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.*;
import io.metersphere.base.mapper.ext.ExtApiDefinitionExecResultMapper;
@ -400,7 +400,7 @@ public class ApiExecutionQueueService {
continue;
}
// 检查执行报告是否还在等待队列中或执行线程中
if (JmeterThreadUtils.isRunning(item.getReportId(), item.getTestId())) {
if (JMeterThreadUtils.isRunning(item.getReportId(), item.getTestId())) {
continue;
}
// 检查是否已经超时

View File

@ -7,8 +7,10 @@ import com.alibaba.fastjson.parser.Feature;
import io.metersphere.api.dto.*;
import io.metersphere.api.dto.automation.APIScenarioReportResult;
import io.metersphere.api.dto.automation.ExecuteType;
import io.metersphere.api.dto.automation.RunScenarioRequest;
import io.metersphere.api.dto.automation.ScenarioStatus;
import io.metersphere.api.dto.datacount.ApiDataCountResult;
import io.metersphere.api.dto.definition.RunDefinitionRequest;
import io.metersphere.api.jmeter.FixedCapacityUtils;
import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.*;
@ -866,7 +868,7 @@ public class ApiScenarioReportService {
return map;
}
public APIScenarioReportResult init(String id, String scenarioId, String scenarioName, String triggerMode, String execType, String projectId, String userID, RunModeConfigDTO config) {
public APIScenarioReportResult init(String id, String scenarioId, String scenarioName, String triggerMode, String execType, String projectId, String userId, RunModeConfigDTO config) {
APIScenarioReportResult report = new APIScenarioReportResult();
if (triggerMode.equals(ApiRunMode.SCENARIO.name()) || triggerMode.equals(ApiRunMode.DEFINITION.name())) {
triggerMode = ReportTriggerMode.MANUAL.name();
@ -885,9 +887,9 @@ public class ApiScenarioReportService {
String status = config != null && StringUtils.equals(config.getMode(), RunModeConstants.SERIAL.toString())
? APITestStatus.Waiting.name() : APITestStatus.Running.name();
report.setStatus(status);
if (StringUtils.isNotEmpty(userID)) {
report.setUserId(userID);
report.setCreateUser(userID);
if (StringUtils.isNotEmpty(userId)) {
report.setUserId(userId);
report.setCreateUser(userId);
} else {
report.setUserId(SessionUtils.getUserId());
report.setCreateUser(SessionUtils.getUserId());
@ -914,6 +916,22 @@ public class ApiScenarioReportService {
return report;
}
public APIScenarioReportResult getApiScenarioReportResult(RunScenarioRequest request, String serialReportId,
String scenarioNames, String reportScenarioIds) {
APIScenarioReportResult report = this.init(request.getConfig().getReportId(), reportScenarioIds,
scenarioNames, request.getTriggerMode(), ExecuteType.Saved.name(), request.getProjectId(),
request.getReportUserID(), request.getConfig());
report.setName(request.getConfig().getReportName());
report.setId(serialReportId);
report.setReportType(ReportTypeConstants.SCENARIO_INTEGRATED.name());
request.getConfig().setAmassReport(serialReportId);
if (request.getConfig() != null) {
report.setEnvConfig(JSON.toJSONString(request.getConfig()));
}
report.setStatus(APITestStatus.Running.name());
return report;
}
/**
* 返回正确的报告状态
*
@ -934,10 +952,10 @@ public class ApiScenarioReportService {
if (StringUtils.isNotEmpty(dto.getRunMode()) && dto.getRunMode().startsWith("UI")) {
try {
errorSize = dto.getRequestResults().stream().filter(requestResult ->
StringUtils.isNotEmpty(requestResult.getResponseResult().getHeaders())
&& JSONArray.parseArray(requestResult.getResponseResult().getHeaders()).stream().filter(
r -> ((JSONObject) r).containsKey("success") && !((JSONObject) r).getBoolean("success")
).count() > 0)
StringUtils.isNotEmpty(requestResult.getResponseResult().getHeaders())
&& JSONArray.parseArray(requestResult.getResponseResult().getHeaders()).stream().filter(
r -> ((JSONObject) r).containsKey("success") && !((JSONObject) r).getBoolean("success")
).count() > 0)
.count();
} catch (Exception e) {
// UI 返回的结果在 headers 里面格式不符合规范的直接认定结果为失败
@ -1023,4 +1041,20 @@ public class ApiScenarioReportService {
MSException.throwException(e.getMessage());
}
}
public APIScenarioReportResult initResult(String reportId, String testPlanScenarioId, String name, RunScenarioRequest request) {
return this.init(reportId, testPlanScenarioId, name, request.getTriggerMode(),
request.getExecuteType(), request.getProjectId(), request.getReportUserID(), request.getConfig());
}
public APIScenarioReportResult initDebugResult(RunDefinitionRequest request) {
return this.init(request.getId(),
request.getScenarioId(),
request.getScenarioName(),
ReportTriggerMode.MANUAL.name(),
request.getExecuteType(),
request.getProjectId(),
SessionUtils.getUserId(),
request.getConfig());
}
}

View File

@ -5,7 +5,7 @@ import io.metersphere.api.dto.automation.TaskRequest;
import io.metersphere.api.exec.queue.ExecThreadPoolExecutor;
import io.metersphere.api.exec.queue.PoolExecBlockingQueueUtil;
import io.metersphere.api.jmeter.JMeterService;
import io.metersphere.api.jmeter.JmeterThreadUtils;
import io.metersphere.api.jmeter.JMeterThreadUtils;
import io.metersphere.api.service.ApiExecutionQueueService;
import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.ApiDefinitionExecResultMapper;
@ -179,9 +179,9 @@ public class TaskService {
// 结束掉未分发完成的任务
LoggerUtil.info("结束正在进行中的计划任务队列");
JmeterThreadUtils.stop("PLAN-CASE");
JmeterThreadUtils.stop("API-CASE-RUN");
JmeterThreadUtils.stop("SCENARIO-PARALLEL-THREAD");
JMeterThreadUtils.stop("PLAN-CASE");
JMeterThreadUtils.stop("API-CASE-RUN");
JMeterThreadUtils.stop("SCENARIO-PARALLEL-THREAD");
if (taskRequestMap.containsKey("API")) {
List<ApiDefinitionExecResult> results = extApiDefinitionExecResultMapper.findByProjectIds(taskCenterRequest);
@ -261,7 +261,7 @@ public class TaskService {
}
} else {
new LocalRunner().stop(reportId);
JmeterThreadUtils.stop(reportId);
JMeterThreadUtils.stop(reportId);
}
}
}