fix(接口自动化): 并发数量控制

This commit is contained in:
fit2-zhao 2021-04-23 18:59:27 +08:00 committed by fit2-zhao
parent 060be4f6c7
commit 2c8502f75c
4 changed files with 47 additions and 16 deletions

View File

@ -48,6 +48,7 @@ import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.collections.ListedHashTree; import org.apache.jorphan.collections.ListedHashTree;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -594,7 +595,6 @@ public class ApiAutomationService {
report.setProjectId(projectId); report.setProjectId(projectId);
report.setScenarioName(scenarioName); report.setScenarioName(scenarioName);
report.setScenarioId(scenarioId); report.setScenarioId(scenarioId);
apiScenarioReportMapper.insert(report);
return report; return report;
} }
@ -696,7 +696,7 @@ public class ApiAutomationService {
* @param request * @param request
* @return * @return
*/ */
public String abandonedRun(RunScenarioRequest request) { public String parallelRun(RunScenarioRequest request) {
ServiceUtils.getSelectAllIds(request, request.getCondition(), ServiceUtils.getSelectAllIds(request, request.getCondition(),
(query) -> extApiScenarioMapper.selectIdsByQuery((ApiScenarioRequest) query)); (query) -> extApiScenarioMapper.selectIdsByQuery((ApiScenarioRequest) query));
@ -823,6 +823,7 @@ public class ApiAutomationService {
// 创建场景报告 // 创建场景报告
if (reportIds != null) { if (reportIds != null) {
//如果是测试计划页面触发的执行方式生成报告时createScenarioReport第二个参数需要特殊处理 //如果是测试计划页面触发的执行方式生成报告时createScenarioReport第二个参数需要特殊处理
APIScenarioReportResult report = null;
if (StringUtils.equals(request.getRunMode(), ApiRunMode.SCENARIO_PLAN.name())) { if (StringUtils.equals(request.getRunMode(), ApiRunMode.SCENARIO_PLAN.name())) {
String testPlanScenarioId = item.getId(); String testPlanScenarioId = item.getId();
if (request.getScenarioTestPlanIdMap() != null && request.getScenarioTestPlanIdMap().containsKey(item.getId())) { if (request.getScenarioTestPlanIdMap() != null && request.getScenarioTestPlanIdMap().containsKey(item.getId())) {
@ -834,12 +835,13 @@ public class ApiAutomationService {
scenario.setEnvironmentMap(JSON.parseObject(environment, Map.class)); scenario.setEnvironmentMap(JSON.parseObject(environment, Map.class));
} }
} }
createScenarioReport(group.getName(), testPlanScenarioId, item.getName(), request.getTriggerMode() == null ? ReportTriggerMode.MANUAL.name() : request.getTriggerMode(), report = createScenarioReport(group.getName(), testPlanScenarioId, item.getName(), request.getTriggerMode() == null ? ReportTriggerMode.MANUAL.name() : request.getTriggerMode(),
request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig()); request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig());
} else { } else {
createScenarioReport(group.getName(), item.getId(), item.getName(), request.getTriggerMode() == null ? ReportTriggerMode.MANUAL.name() : request.getTriggerMode(), report = createScenarioReport(group.getName(), item.getId(), item.getName(), request.getTriggerMode() == null ? ReportTriggerMode.MANUAL.name() : request.getTriggerMode(),
request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig()); request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig());
} }
apiScenarioReportMapper.insert(report);
reportIds.add(group.getName()); reportIds.add(group.getName());
} }
group.setHashTree(scenarios); group.setHashTree(scenarios);
@ -909,7 +911,7 @@ public class ApiAutomationService {
* @param request * @param request
* @return * @return
*/ */
public String run(RunScenarioRequest request) { public String serialRun(RunScenarioRequest request) {
ServiceUtils.getSelectAllIds(request, request.getCondition(), ServiceUtils.getSelectAllIds(request, request.getCondition(),
(query) -> extApiScenarioMapper.selectIdsByQuery((ApiScenarioRequest) query)); (query) -> extApiScenarioMapper.selectIdsByQuery((ApiScenarioRequest) query));
List<String> ids = request.getIds(); List<String> ids = request.getIds();
@ -953,6 +955,25 @@ public class ApiAutomationService {
return request.getId(); return request.getId();
} }
@Value("${run.concurrency}")
private String concurrency;
public String run(RunScenarioRequest request) {
if (request.getConfig() != null && request.getConfig().getMode().equals("serial")) {
return this.serialRun(request);
} else {
// 校验并发数量
int count = 50;
if (StringUtils.isNotEmpty(concurrency)) {
count = Integer.parseInt(concurrency);
}
if (request.getIds().size() > count) {
MSException.throwException("并发数量过大,请重新选择!");
}
return this.parallelRun(request);
}
}
public void checkScenarioIsRunning(List<String> ids) { public void checkScenarioIsRunning(List<String> ids) {
List<ApiScenarioReport> lastReportStatusByIds = apiReportService.selectLastReportByIds(ids); List<ApiScenarioReport> lastReportStatusByIds = apiReportService.selectLastReportByIds(ids);
for (ApiScenarioReport report : lastReportStatusByIds) { for (ApiScenarioReport report : lastReportStatusByIds) {
@ -1013,8 +1034,9 @@ public class ApiAutomationService {
MSException.throwException(e.getMessage()); MSException.throwException(e.getMessage());
} }
// 调用执行方法 // 调用执行方法
createScenarioReport(request.getId(), request.getScenarioId(), request.getScenarioName(), ReportTriggerMode.MANUAL.name(), request.getExecuteType(), request.getProjectId(), APIScenarioReportResult report = createScenarioReport(request.getId(), request.getScenarioId(), request.getScenarioName(), ReportTriggerMode.MANUAL.name(), request.getExecuteType(), request.getProjectId(),
SessionUtils.getUserId(), null); SessionUtils.getUserId(), null);
apiScenarioReportMapper.insert(report);
// 调用执行方法 // 调用执行方法
// jMeterService.runTest(request.getId(), hashTree, ApiRunMode.SCENARIO.name(), true, null); // jMeterService.runTest(request.getId(), hashTree, ApiRunMode.SCENARIO.name(), true, null);
// 调用执行方法 // 调用执行方法
@ -1037,11 +1059,6 @@ public class ApiAutomationService {
if (CollectionUtils.isEmpty(request.getPlanIds())) { if (CollectionUtils.isEmpty(request.getPlanIds())) {
MSException.throwException(Translator.get("plan id is null ")); MSException.throwException(Translator.get("plan id is null "));
} }
// List<String> scenarioIds = request.getScenarioIds();
// if (request.isSelectAllDate()) {
// scenarioIds = this.getAllScenarioIdsByFontedSelect(
// request.getModuleIds(), request.getName(), request.getProjectId(), request.getFilters(), request.getUnSelectIds());
// }
Map<String, List<String>> mapping = request.getMapping(); Map<String, List<String>> mapping = request.getMapping();
Map<String, String> envMap = request.getEnvMap(); Map<String, String> envMap = request.getEnvMap();
Set<String> set = mapping.keySet(); Set<String> set = mapping.keySet();
@ -1158,7 +1175,6 @@ public class ApiAutomationService {
testCaseReviewScenario.setUpdateTime(System.currentTimeMillis()); testCaseReviewScenario.setUpdateTime(System.currentTimeMillis());
testCaseReviewScenario.setEnvironment(JSON.toJSONString(newEnvMap)); testCaseReviewScenario.setEnvironment(JSON.toJSONString(newEnvMap));
testCaseReviewScenarioMapper.insert(testCaseReviewScenario); testCaseReviewScenarioMapper.insert(testCaseReviewScenario);
}); });
} }

View File

@ -43,6 +43,7 @@ import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.HashTree;
import org.aspectj.util.FileUtil; import org.aspectj.util.FileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@ -293,7 +294,8 @@ public class ApiDefinitionService {
test.setTags(request.getTags()); test.setTags(request.getTags());
} else { } else {
test.setTags(null); test.setTags(null);
} this.setModule(test); }
this.setModule(test);
apiDefinitionMapper.updateByPrimaryKeySelective(test); apiDefinitionMapper.updateByPrimaryKeySelective(test);
return test; return test;
} }
@ -526,6 +528,9 @@ public class ApiDefinitionService {
} }
} }
@Value("${run.concurrency}")
private String concurrency;
/** /**
* 测试执行 * 测试执行
* *
@ -534,6 +539,13 @@ public class ApiDefinitionService {
* @return * @return
*/ */
public String run(RunDefinitionRequest request, List<MultipartFile> bodyFiles) { public String run(RunDefinitionRequest request, List<MultipartFile> bodyFiles) {
int count = 100;
if (StringUtils.isNotEmpty(concurrency)) {
count = Integer.parseInt(concurrency);
}
if (request.getTestElement() != null && request.getTestElement().getHashTree().size() == 1 && request.getTestElement().getHashTree().get(0).getHashTree().size() > count) {
MSException.throwException("并发数量过大,请重新选择!");
}
List<String> bodyUploadIds = new ArrayList<>(request.getBodyUploadIds()); List<String> bodyUploadIds = new ArrayList<>(request.getBodyUploadIds());
FileUtils.createBodyFiles(bodyUploadIds, bodyFiles); FileUtils.createBodyFiles(bodyUploadIds, bodyFiles);

View File

@ -605,6 +605,7 @@ export default {
let reqObj = {id: getUUID().substring(0, 8), testElement: testPlan, type: 'API_PLAN', reportId: "run", projectId: projectId}; let reqObj = {id: getUUID().substring(0, 8), testElement: testPlan, type: 'API_PLAN', reportId: "run", projectId: projectId};
let bodyFiles = getBodyUploadFiles(reqObj, this.runData); let bodyFiles = getBodyUploadFiles(reqObj, this.runData);
this.$fileUpload("/api/definition/run", null, bodyFiles, reqObj, response => { this.$fileUpload("/api/definition/run", null, bodyFiles, reqObj, response => {
this.$message('任务执行中,请稍后刷新查看结果');
}); });
} else { } else {
testPlan.serializeThreadgroups = false; testPlan.serializeThreadgroups = false;
@ -617,10 +618,10 @@ export default {
let reqObj = {id: getUUID().substring(0, 8), testElement: testPlan, type: 'API_PLAN', reportId: "run", projectId: projectId}; let reqObj = {id: getUUID().substring(0, 8), testElement: testPlan, type: 'API_PLAN', reportId: "run", projectId: projectId};
let bodyFiles = getBodyUploadFiles(reqObj, this.runData); let bodyFiles = getBodyUploadFiles(reqObj, this.runData);
this.$fileUpload("/api/definition/run", null, bodyFiles, reqObj, response => { this.$fileUpload("/api/definition/run", null, bodyFiles, reqObj, response => {
this.$message('任务执行中,请稍后刷新查看结果');
}); });
} }
this.search(); this.search();
this.$message('任务执行中,请稍后刷新查看结果');
}, },
autoCheckStatus() { // autoCheckStatus() { //
if (!this.planId) { if (!this.planId) {

View File

@ -324,7 +324,9 @@ export default {
this.selectRows.forEach(row => { this.selectRows.forEach(row => {
this.buildExecuteParam(param,row); this.buildExecuteParam(param,row);
}); });
this.$post("/test/case/review/scenario/case/run", param, response => {}); this.$post("/test/case/review/scenario/case/run", param, response => {
this.$message('任务执行中,请稍后刷新查看结果');
});
} }
if (this.planId) { if (this.planId) {
let selectParam = buildBatchParam(this); let selectParam = buildBatchParam(this);
@ -334,9 +336,9 @@ export default {
}); });
param.condition = selectParam.condition; param.condition = selectParam.condition;
this.$post("/test/plan/scenario/case/run", param, response => { this.$post("/test/plan/scenario/case/run", param, response => {
this.$message('任务执行中,请稍后刷新查看结果');
}); });
} }
this.$message('任务执行中,请稍后刷新查看结果');
this.search(); this.search();
}, },
execute(row) { execute(row) {