refactor: 解决性能测试定时任务状态不改变的问题

解决性能测试定时任务状态不改变的问题
This commit is contained in:
song.tianyang 2021-03-25 14:45:22 +08:00
parent cf39bb066d
commit 51a172385a
3 changed files with 224 additions and 162 deletions

View File

@ -2,22 +2,20 @@ package io.metersphere.track.controller;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.metersphere.base.domain.TestCaseReport;
import io.metersphere.base.domain.TestPlanReport;
import io.metersphere.commons.constants.ReportTriggerMode;
import io.metersphere.commons.utils.PageUtils;
import io.metersphere.commons.utils.Pager;
import io.metersphere.commons.utils.SessionUtils;
import io.metersphere.track.dto.TestCaseReportMetricDTO;
import io.metersphere.track.dto.TestPlanDTOWithMetric;
import io.metersphere.track.dto.TestPlanReportDTO;
import io.metersphere.track.request.report.QueryTestPlanReportRequest;
import io.metersphere.track.request.testcase.QueryTestPlanRequest;
import io.metersphere.track.request.report.TestPlanReportSaveRequest;
import io.metersphere.track.service.TestPlanReportService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;
/**
* @author song.tianyang
@ -62,14 +60,18 @@ public class TestPlanReportController {
@GetMapping("/apiExecuteFinish/{planId}/{userId}")
public void apiExecuteFinish(@PathVariable String planId,@PathVariable String userId) {
TestPlanReport report = testPlanReportService.genTestPlanReport(planId,userId,ReportTriggerMode.API.name());
String reportId = UUID.randomUUID().toString();
TestPlanReportSaveRequest saveRequest = new TestPlanReportSaveRequest(reportId,planId,userId,ReportTriggerMode.API.name());
TestPlanReport report = testPlanReportService.genTestPlanReport(saveRequest);
testPlanReportService.countReportByTestPlanReportId(report.getId(),null, ReportTriggerMode.API.name());
}
@GetMapping("/saveTestPlanReport/{planId}/{triggerMode}")
public String saveTestPlanReport(@PathVariable String planId,@PathVariable String triggerMode) {
String userId = SessionUtils.getUser().getId();
TestPlanReport report = testPlanReportService.genTestPlanReport(planId,userId,triggerMode);
String reportId = UUID.randomUUID().toString();
TestPlanReportSaveRequest saveRequest = new TestPlanReportSaveRequest(reportId,planId,userId,triggerMode);
TestPlanReport report = testPlanReportService.genTestPlanReport(saveRequest);
testPlanReportService.countReportByTestPlanReportId(report.getId(),null, triggerMode);
return "success";
}

View File

@ -2,9 +2,6 @@ package io.metersphere.track.service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.metersphere.api.dto.definition.ApiDefinitionRequest;
import io.metersphere.api.dto.definition.ApiDefinitionResult;
import io.metersphere.api.jmeter.TestResult;
import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.*;
import io.metersphere.base.mapper.ext.ExtTestPlanApiCaseMapper;
@ -21,15 +18,14 @@ import io.metersphere.track.Factory.ReportComponentFactory;
import io.metersphere.track.domain.ReportComponent;
import io.metersphere.track.dto.*;
import io.metersphere.track.request.report.QueryTestPlanReportRequest;
import io.metersphere.track.request.report.TestPlanReportSaveRequest;
import io.metersphere.track.request.testcase.QueryTestPlanRequest;
import io.metersphere.track.request.testplan.LoadCaseRequest;
import org.apache.commons.lang3.StringUtils;
import org.python.bouncycastle.pqc.math.linearalgebra.IntUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@ -80,36 +76,25 @@ public class TestPlanReportService {
}
/**
* 生成测试计划
* @param planId
* @param userId
* @param triggerMode
* @param reportId 报告ID(外部传入
* @param planId 测试计划ID
* @param userId 用户ID
* @param triggerMode 执行方式
* @param countResources 是否统计资源-false的话 下面三个不同资源是否运行则由参数决定 true的话则由统计后的结果决定
* @param apiCaseIsExecuting 接口案例是否执行中
* @param scenarioIsExecuting 场景案例是否执行中
* @param performanceIsExecuting 性能案例是否执行中
* @return
*/
public TestPlanReport genTestPlanReport(String planId, String userId,String triggerMode) {
TestPlan testPlan = testPlanMapper.selectByPrimaryKey(planId);
public TestPlanReport genTestPlanReport(TestPlanReportSaveRequest saveRequest) {
TestPlan testPlan = testPlanMapper.selectByPrimaryKey(saveRequest.getPlanId());
testPlan.setExecutionTimes(1);
testPlan.setExecutionTimes(testPlan.getExecutionTimes() + 1);
testPlanMapper.updateByPrimaryKey(testPlan);
TestPlanApiCaseExample apiExample = new TestPlanApiCaseExample();
apiExample.createCriteria().andTestPlanIdEqualTo(planId);
List<String> apiCaseIdList = testPlanApiCaseMapper.selectByExample(apiExample)
.stream().map(TestPlanApiCase::getApiCaseId).collect(Collectors.toList());
TestPlanApiScenarioExample example = new TestPlanApiScenarioExample();
example.createCriteria().andTestPlanIdEqualTo(planId);
List<String> scenarioIdList = testPlanScenarioCaseMapper.selectByExample(example)
.stream().map(TestPlanApiScenario::getApiScenarioId).collect(Collectors.toList());
LoadCaseRequest loadCaseRequest = new LoadCaseRequest();
loadCaseRequest.setTestPlanId(planId);
loadCaseRequest.setProjectId(testPlan.getProjectId());
List<String> performanceIdList = testPlanLoadCaseService.list(loadCaseRequest)
.stream().map(TestPlanLoadCaseDTO::getLoadCaseId).collect(Collectors.toList());
String testPlanReportID = UUID.randomUUID().toString();
String testPlanReportID = saveRequest.getReportID();
TestPlanReport testPlanReport = new TestPlanReport();
testPlanReport.setTestPlanId(planId);
testPlanReport.setTestPlanId(saveRequest.getPlanId());
testPlanReport.setId(testPlanReportID);
testPlanReport.setCreateTime(System.currentTimeMillis());
testPlanReport.setUpdateTime(System.currentTimeMillis());
@ -117,41 +102,68 @@ public class TestPlanReportService {
testPlanReport.setName(testPlan.getName() + "-" + DateUtils.getTimeString(new Date()));
} catch (Exception e) {
}
testPlanReport.setTriggerMode(triggerMode);
testPlanReport.setCreator(userId);
testPlanReport.setTriggerMode(saveRequest.getTriggerMode());
testPlanReport.setCreator(saveRequest.getUserId());
testPlanReport.setStartTime(System.currentTimeMillis());
testPlanReport.setEndTime(System.currentTimeMillis());
if (apiCaseIdList.isEmpty()) {
testPlanReport.setIsApiCaseExecuting(false);
} else {
testPlanReport.setIsApiCaseExecuting(true);
}
if (scenarioIdList.isEmpty()) {
testPlanReport.setIsScenarioExecuting(false);
} else {
testPlanReport.setIsScenarioExecuting(true);
}
if (performanceIdList.isEmpty()) {
testPlanReport.setIsPerformanceExecuting(false);
} else {
testPlanReport.setIsPerformanceExecuting(true);
}
testPlanReport.setPrincipal(testPlan.getPrincipal());
if(testPlanReport.getIsScenarioExecuting() || testPlanReport.getIsApiCaseExecuting() || testPlanReport.getIsPerformanceExecuting()){
testPlanReport.setStatus(APITestStatus.Starting.name());
}else {
testPlanReport.setStatus(APITestStatus.Completed.name());
}
testPlanReportMapper.insert(testPlanReport);
TestPlanReportDataWithBLOBs testPlanReportData = new TestPlanReportDataWithBLOBs();
testPlanReportData.setId(UUID.randomUUID().toString());
testPlanReportData.setTestPlanReportId(testPlanReportID);
if (saveRequest.isCountResources()) {
TestPlanApiCaseExample apiExample = new TestPlanApiCaseExample();
apiExample.createCriteria().andTestPlanIdEqualTo(saveRequest.getPlanId());
List<String> apiCaseIdList = testPlanApiCaseMapper.selectByExample(apiExample)
.stream().map(TestPlanApiCase::getApiCaseId).collect(Collectors.toList());
if (apiCaseIdList.isEmpty()) {
testPlanReport.setIsApiCaseExecuting(false);
} else {
testPlanReport.setIsApiCaseExecuting(true);
}
TestPlanApiScenarioExample example = new TestPlanApiScenarioExample();
example.createCriteria().andTestPlanIdEqualTo(saveRequest.getPlanId());
List<String> scenarioIdList = testPlanScenarioCaseMapper.selectByExample(example)
.stream().map(TestPlanApiScenario::getApiScenarioId).collect(Collectors.toList());
if (scenarioIdList.isEmpty()) {
testPlanReport.setIsScenarioExecuting(false);
} else {
testPlanReport.setIsScenarioExecuting(true);
}
LoadCaseRequest loadCaseRequest = new LoadCaseRequest();
loadCaseRequest.setTestPlanId(saveRequest.getPlanId());
loadCaseRequest.setProjectId(testPlan.getProjectId());
List<String> performanceIdList = testPlanLoadCaseService.list(loadCaseRequest)
.stream().map(TestPlanLoadCaseDTO::getLoadCaseId).collect(Collectors.toList());
if (performanceIdList.isEmpty()) {
testPlanReport.setIsPerformanceExecuting(false);
} else {
testPlanReport.setIsPerformanceExecuting(true);
}
testPlanReportData.setApiCaseInfo(JSONArray.toJSONString(apiCaseIdList));
testPlanReportData.setScenarioInfo(JSONArray.toJSONString(scenarioIdList));
testPlanReportData.setPerformanceInfo(JSONArray.toJSONString(performanceIdList));
} else {
testPlanReport.setIsApiCaseExecuting(saveRequest.isApiCaseIsExecuting());
testPlanReport.setIsScenarioExecuting(saveRequest.isScenarioIsExecuting());
testPlanReport.setIsPerformanceExecuting(saveRequest.isPerformanceIsExecuting());
testPlanReportData.setApiCaseInfo(saveRequest.getApiCaseIdListJSON());
testPlanReportData.setScenarioInfo(saveRequest.getScenarioIdListJSON());
testPlanReportData.setPerformanceInfo(saveRequest.getPerformanceIdListJSON());
}
testPlanReport.setPrincipal(testPlan.getPrincipal());
if (testPlanReport.getIsScenarioExecuting() || testPlanReport.getIsApiCaseExecuting() || testPlanReport.getIsPerformanceExecuting()) {
testPlanReport.setStatus(APITestStatus.Starting.name());
} else {
testPlanReport.setStatus(APITestStatus.Completed.name());
}
testPlanReportMapper.insert(testPlanReport);
testPlanReportDataMapper.insert(testPlanReportData);
//更新TestPlan状态改为进行中
@ -219,7 +231,6 @@ public class TestPlanReportService {
}
/**
*
* @param planReportId 测试计划报告ID
* @param resourceRunMode 资源的运行模式,triggerMode非Scedule可以为null
* @param triggerMode 触发方式 ReportTriggerMode.enum
@ -324,6 +335,7 @@ public class TestPlanReportService {
/**
* 计算测试计划的状态
*
* @param testPlanReport
* @return
*/

View File

@ -33,6 +33,7 @@ import io.metersphere.service.SystemParameterService;
import io.metersphere.track.Factory.ReportComponentFactory;
import io.metersphere.track.domain.ReportComponent;
import io.metersphere.track.dto.*;
import io.metersphere.track.request.report.TestPlanReportSaveRequest;
import io.metersphere.track.request.testcase.PlanCaseRelevanceRequest;
import io.metersphere.track.request.testcase.QueryTestPlanRequest;
import io.metersphere.track.request.testplan.AddTestPlanRequest;
@ -128,6 +129,8 @@ public class TestPlanService {
private ApiScenarioMapper apiScenarioMapper;
@Resource
private TestCaseTestMapper testCaseTestMapper;
@Resource
private ApiScenarioReportMapper apiScenarioReportMapper;
public synchronized String addTestPlan(AddTestPlanRequest testPlan) {
if (getTestPlanByName(testPlan.getName()).size() > 0) {
@ -863,12 +866,13 @@ public class TestPlanService {
* @return
*/
public String runScenarioCase(SchedulePlanScenarioExecuteRequest request) {
String returnId = "";
MsTestPlan testPlan = new MsTestPlan();
testPlan.setHashTree(new LinkedList<>());
HashTree jmeterHashTree = new ListedHashTree();
Map<String, Map<String, String>> testPlanScenarioIdMap = request.getTestPlanScenarioIDMap();
for (Map.Entry<String, Map<String, String>> entry : testPlanScenarioIdMap.entrySet()) {
String testPlanID = entry.getKey();
Map<String, String> planScenarioIdMap = entry.getValue();
List<ApiScenarioWithBLOBs> apiScenarios = extApiScenarioMapper.selectIds(new ArrayList<>(planScenarioIdMap.keySet()));
try {
@ -918,23 +922,27 @@ public class TestPlanService {
scenarios.add(scenario);
// 创建场景报告
//不同的运行模式第二个参数入参不同
apiAutomationService.createScenarioReport(group.getName(),
APIScenarioReportResult report = apiAutomationService.createScenarioReport(group.getName(),
planScenarioID + ":" + request.getTestPlanReportId(),
item.getName(), request.getTriggerMode() == null ? ReportTriggerMode.MANUAL.name() : request.getTriggerMode(),
request.getExecuteType(), item.getProjectId(), request.getReportUserID());
group.setHashTree(scenarios);
testPlan.getHashTree().add(group);
apiScenarioReportMapper.insert(report);
returnId = request.getId();
}
} catch (Exception ex) {
MSException.throwException(ex.getMessage());
}
}
testPlan.toHashTree(jmeterHashTree, testPlan.getHashTree(), new ParameterConfig());
String runMode = ApiRunMode.SCHEDULE_SCENARIO_PLAN.name();
// 调用执行方法
jMeterService.runDefinition(request.getId(), jmeterHashTree, request.getReportId(), runMode);
return request.getId();
}
return returnId;
}
public void run(String testPlanID, String projectID, String userId, String triggerMode) {
@ -966,37 +974,21 @@ public class TestPlanService {
LogUtil.info("-------------- start testplan schedule ----------");
TestPlanReportService testPlanReportService = CommonBeanFactory.getBean(TestPlanReportService.class);
//首先创建testPlanReport然后返回的ID重新赋值为resourceID作为后续的参数
TestPlanReport testPlanReport = testPlanReportService.genTestPlanReport(testPlanID, userId, triggerMode);
//执行接口案例任务
for (Map.Entry<String, String> entry : apiTestCaseIdMap.entrySet()) {
String apiCaseID = entry.getKey();
String planCaseID = entry.getValue();
ApiTestCaseWithBLOBs blobs = apiTestCaseService.get(apiCaseID);
//需要更新这里来保证PlanCase的状态能正常更改
apiTestCaseService.run(blobs, UUID.randomUUID().toString(), testPlanReport.getId(), testPlanID, ApiRunMode.SCHEDULE_API_PLAN.name());
}
//执行场景执行任务
if (!planScenarioIdMap.isEmpty()) {
LogUtil.info("-------------- testplan schedule ---------- api case over -----------------");
SchedulePlanScenarioExecuteRequest scenarioRequest = new SchedulePlanScenarioExecuteRequest();
String senarionReportID = UUID.randomUUID().toString();
scenarioRequest.setId(senarionReportID);
scenarioRequest.setReportId(senarionReportID);
scenarioRequest.setProjectId(projectID);
scenarioRequest.setTriggerMode(ReportTriggerMode.SCHEDULE.name());
scenarioRequest.setExecuteType(ExecuteType.Saved.name());
Map<String, Map<String, String>> testPlanScenarioIdMap = new HashMap<>();
testPlanScenarioIdMap.put(testPlanID, planScenarioIdMap);
scenarioRequest.setTestPlanScenarioIDMap(testPlanScenarioIdMap);
scenarioRequest.setReportUserID(userId);
scenarioRequest.setTestPlanID(testPlanID);
scenarioRequest.setRunMode(ApiRunMode.SCHEDULE_SCENARIO_PLAN.name());
scenarioRequest.setTestPlanReportId(testPlanReport.getId());
this.runScenarioCase(scenarioRequest);
LogUtil.info("-------------- testplan schedule ---------- scenario case over -----------------");
}
boolean apiCaseIsExcuting = false;
boolean scenarioIsExcuting = false;
boolean performaceIsExcuting = false;
String apiCaseIdArray = "";
String scenarioCaseIdArray = "";
String performanceCaseIdArray = "";
String planReportId = UUID.randomUUID().toString();
//创建测试报告然后返回的ID重新赋值为resourceID作为后续的参数
TestPlanReportSaveRequest saveRequest = new TestPlanReportSaveRequest(planReportId,testPlanID,userId,triggerMode,
apiTestCaseIdMap.size()>0,planScenarioIdMap.size()>0,performanceIdMap.size()>0,
JSONArray.toJSONString(new ArrayList<>(apiTestCaseIdMap.keySet())),JSONArray.toJSONString(new ArrayList<>(planScenarioIdMap.keySet())),JSONArray.toJSONString(new ArrayList<>(performanceIdMap.values())));
TestPlanReport testPlanReport = testPlanReportService.genTestPlanReport(saveRequest);
//执行性能测试任务
List<String> performaneReportIDList = new ArrayList<>();
for (Map.Entry<String, String> entry : performanceIdMap.entrySet()) {
@ -1016,20 +1008,76 @@ public class TestPlanService {
testPlanLoadCase.setId(performanceRequest.getTestPlanLoadId());
testPlanLoadCase.setLoadReportId(reportId);
testPlanLoadCaseService.update(testPlanLoadCase);
}
} catch (Exception e) {
e.printStackTrace();
}
//更新关联处的报告
TestPlanLoadCase loadCase = new TestPlanLoadCaseDTO();
loadCase.setId(id);
loadCase.setLoadReportId(reportId);
testPlanLoadCaseService.update(loadCase);
}
} catch (Exception e) {
e.printStackTrace();
}
if(StringUtils.isEmpty(reportId)){
performaceIsExcuting = false;
}
}
if(performaceIsExcuting){
performanceCaseIdArray= JSONArray.toJSONString(new ArrayList<>(performanceIdMap.values()));
}
if (!performaneReportIDList.isEmpty()) {
//性能测试时保存性能测试报告ID在结果返回时用于捕捉并进行
testPlanReportService.updatePerformanceInfo(testPlanReport, performaneReportIDList, ReportTriggerMode.SCHEDULE.name());
}
//执行接口案例任务
for (Map.Entry<String, String> entry : apiTestCaseIdMap.entrySet()) {
String apiCaseID = entry.getKey();
String planCaseID = entry.getValue();
ApiTestCaseWithBLOBs blobs = apiTestCaseService.get(apiCaseID);
//需要更新这里来保证PlanCase的状态能正常更改
apiTestCaseService.run(blobs, UUID.randomUUID().toString(), planReportId, testPlanID, ApiRunMode.SCHEDULE_API_PLAN.name());
apiCaseIsExcuting = true;
}
if(apiCaseIsExcuting){
apiCaseIdArray = JSONArray.toJSONString(new ArrayList<>(apiTestCaseIdMap.keySet()));
}
//执行场景执行任务
if (!planScenarioIdMap.isEmpty()) {
LogUtil.info("-------------- testplan schedule ---------- api case over -----------------");
SchedulePlanScenarioExecuteRequest scenarioRequest = new SchedulePlanScenarioExecuteRequest();
String senarionReportID = UUID.randomUUID().toString();
scenarioRequest.setId(senarionReportID);
scenarioRequest.setReportId(senarionReportID);
scenarioRequest.setProjectId(projectID);
scenarioRequest.setTriggerMode(ReportTriggerMode.SCHEDULE.name());
scenarioRequest.setExecuteType(ExecuteType.Saved.name());
Map<String, Map<String, String>> testPlanScenarioIdMap = new HashMap<>();
testPlanScenarioIdMap.put(testPlanID, planScenarioIdMap);
scenarioRequest.setTestPlanScenarioIDMap(testPlanScenarioIdMap);
scenarioRequest.setReportUserID(userId);
scenarioRequest.setTestPlanID(testPlanID);
scenarioRequest.setRunMode(ApiRunMode.SCHEDULE_SCENARIO_PLAN.name());
scenarioRequest.setTestPlanReportId(planReportId);
String scenarioReportID = this.runScenarioCase(scenarioRequest);
if(StringUtils.isNotEmpty(scenarioReportID)){
scenarioIsExcuting = true;
scenarioCaseIdArray= JSONArray.toJSONString(new ArrayList<>(planScenarioIdMap.keySet()));
}
LogUtil.info("-------------- testplan schedule ---------- scenario case over -----------------");
}
//如果report参数和预期不对某些原因执行失败则更新report
if(saveRequest.isApiCaseIsExecuting() != apiCaseIsExcuting ||saveRequest.isScenarioIsExecuting()!=scenarioIsExcuting ||saveRequest.isPerformanceIsExecuting() != performaceIsExcuting){
testPlanReport.setIsApiCaseExecuting(apiCaseIsExcuting);
testPlanReport.setIsScenarioExecuting(scenarioIsExcuting);
testPlanReport.setIsPerformanceExecuting(performaceIsExcuting);
testPlanReportService.update(testPlanReport);
}
}
}