fix(接口定义): 修复执行问题

This commit is contained in:
fit2-zhao 2021-11-09 17:13:56 +08:00 committed by fit2-zhao
parent 051ba4a800
commit 05a6ef5569
8 changed files with 96 additions and 96 deletions

View File

@ -398,7 +398,9 @@ public class ElementUtil {
List<String> delIds = new ArrayList<>();
Map<String, MsTestElement> updateMap = new HashMap<>();
if (CollectionUtils.isEmpty(sourceHashTree)) {
sourceHashTree.addAll(targetHashTree);
if (CollectionUtils.isNotEmpty(targetHashTree)) {
sourceHashTree.addAll(targetHashTree);
}
return;
}
if (CollectionUtils.isNotEmpty(targetHashTree)) {

View File

@ -4,6 +4,7 @@ package io.metersphere.api.jmeter;
import io.metersphere.api.dto.RunningParamKeys;
import io.metersphere.api.service.MsResultService;
import io.metersphere.api.service.TestResultService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.samplers.SampleResult;
import org.springframework.stereotype.Service;
@ -26,36 +27,44 @@ public class APIBackendListenerHandler {
private MsResultService resultService;
public void handleTeardownTest(List<SampleResult> queue, String runMode, String testId, String debugReportId, String amassReport) throws Exception {
TestResult testResult = new TestResult();
testResult.setTestId(testId);
testResult.setSetReportId(amassReport);
MessageCache.runningEngine.remove(testId);
testResult.setTotal(0);
List<String> enviromentList = new ArrayList<>();
// 一个脚本里可能包含多个场景(ThreadGroup)所以要区分开key: 场景Id
final Map<String, ScenarioResult> scenarios = new LinkedHashMap<>();
queue.forEach(result -> {
// 线程名称: <场景名> <场景Index>-<请求Index>, 例如Scenario 2-1
if (StringUtils.equals(result.getSampleLabel(), RunningParamKeys.RUNNING_DEBUG_SAMPLER_NAME)) {
String evnStr = result.getResponseDataAsString();
enviromentList.add(evnStr);
} else {
resultService.formatTestResult(testResult, scenarios, result);
try {
TestResult testResult = new TestResult();
testResult.setTestId(testId);
testResult.setSetReportId(amassReport);
MessageCache.runningEngine.remove(testId);
testResult.setTotal(0);
List<String> environmentList = new ArrayList<>();
// 一个脚本里可能包含多个场景(ThreadGroup)所以要区分开key: 场景Id
final Map<String, ScenarioResult> scenarios = new LinkedHashMap<>();
queue.forEach(result -> {
// 线程名称: <场景名> <场景Index>-<请求Index>, 例如Scenario 2-1
if (StringUtils.equals(result.getSampleLabel(), RunningParamKeys.RUNNING_DEBUG_SAMPLER_NAME)) {
String evnStr = result.getResponseDataAsString();
environmentList.add(evnStr);
} else {
resultService.formatTestResult(testResult, scenarios, result);
}
});
queue.clear();
testResult.getScenarios().addAll(scenarios.values());
testResult.getScenarios().sort(Comparator.comparing(ScenarioResult::getId));
testResult.setConsole(resultService.getJmeterLogger(testId, true));
//处理环境参数
ExecutedHandleSingleton.parseEnvironment(environmentList);
testResultService.saveResult(testResult, runMode, debugReportId, testId);
// 清除已经中断的过程数据
if (!MessageCache.reportCache.containsKey(testId) && resultService.getProcessCache().containsKey(testId)) {
resultService.getProcessCache().remove(testId);
}
if (StringUtils.isNotEmpty(testId)) {
MessageCache.executionQueue.remove(testId);
}
} catch (Exception e) {
if (StringUtils.isNotEmpty(amassReport) && MessageCache.cache.get(amassReport) != null
&& StringUtils.isNotEmpty(testId)
&& CollectionUtils.isNotEmpty(MessageCache.cache.get(amassReport).getReportIds())) {
MessageCache.cache.get(amassReport).getReportIds().remove(testId);
}
});
queue.clear();
testResult.getScenarios().addAll(scenarios.values());
testResult.getScenarios().sort(Comparator.comparing(ScenarioResult::getId));
testResult.setConsole(resultService.getJmeterLogger(testId, true));
//处理环境参数
ExecutedHandleSingleton.parseEnvironment(enviromentList);
testResultService.saveResult(testResult, runMode, debugReportId, testId);
// 清除已经中断的过程数据
if (!MessageCache.reportCache.containsKey(testId) && resultService.getProcessCache().containsKey(testId)) {
resultService.getProcessCache().remove(testId);
}
if (StringUtils.isNotEmpty(testId)) {
MessageCache.executionQueue.remove(testId);
}
}
}

View File

@ -1,5 +1,6 @@
package io.metersphere.api.jmeter;
import com.alibaba.fastjson.JSON;
import io.metersphere.api.service.ApiScenarioReportService;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.LogUtil;
@ -8,7 +9,9 @@ import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Component
public class FixedTask {
@ -30,9 +33,12 @@ public class FixedTask {
if (MessageCache.cache != null && MessageCache.cache.size() > 0) {
for (String key : MessageCache.cache.keySet()) {
ReportCounter counter = MessageCache.cache.get(key);
LogUtil.info("集成报告:【" + key + "】总执行场景:【" + counter.getReportIds().size() + "】已经执行完成场景:【" + counter.getNumber() + "");
LogUtil.info("集成报告:【" + key + "】总执行场景:【" + counter.getReportIds().size() + "】已经执行完成场景:【" + counter.getCompletedIds().size() + "");
List<String> filterList = counter.getReportIds().stream().filter(t -> !counter.getCompletedIds().contains(t)).collect(Collectors.toList());
LogUtil.info("剩余要执行的报告" + JSON.toJSONString(filterList));
// 合并
if (counter.getNumber() >= counter.getReportIds().size()) {
if (counter.getCompletedIds().size() >= counter.getReportIds().size()) {
scenarioReportService.margeReport(key, counter.getReportIds());
guardTask.remove(key);
MessageCache.cache.remove(key);
@ -45,7 +51,7 @@ public class FixedTask {
} else {
guardTask.put(key, 0);
}
if (CollectionUtils.isNotEmpty(counter.getPoolUrls()) && counter.getNumber() > 0 && guardTask.get(key) > 200) {
if (CollectionUtils.isNotEmpty(counter.getPoolUrls()) && counter.getCompletedIds().size() > 0 && guardTask.get(key) > 200) {
// 资源池中已经没有执行的请求了
int runningCount = scenarioReportService.get(key, counter);
if (runningCount == 0) {

View File

@ -7,7 +7,7 @@ import java.util.List;
@Data
public class ReportCounter {
private int number;
private List<String> completedIds;
private List<JvmInfoDTO> poolUrls;
private List<String> reportIds;
}

View File

@ -1229,7 +1229,7 @@ public class ApiAutomationService {
List<String> reportIds = executeQueue.entrySet().stream()
.map(reports -> reports.getKey()).collect(Collectors.toList());
ReportCounter counter = new ReportCounter();
counter.setNumber(0);
counter.setCompletedIds(new LinkedList<>());
if (CollectionUtils.isNotEmpty(request.getConfig().getTestResources())) {
counter.setPoolUrls(request.getConfig().getTestResources());
}
@ -1369,7 +1369,7 @@ public class ApiAutomationService {
APIScenarioReportResult report = executeQueue.get(reportId).getReport();
batchMapper.insert(report);
// 增加一个本地锁防止并发找不到资源
MessageCache.scenarioExecResourceLock.put(reportId,report);
MessageCache.scenarioExecResourceLock.put(reportId, report);
}
sqlSession.flushStatements();
sqlSession.commit();

View File

@ -93,7 +93,6 @@ public class ApiScenarioReportService {
} else if (StringUtils.equalsAny(runMode, ApiRunMode.SCHEDULE_SCENARIO_PLAN.name(), ApiRunMode.JENKINS_SCENARIO_PLAN.name())) {
return updateSchedulePlanCase(result, runMode);
} else {
updateScenarioStatus(result.getTestId());
return updateScenario(result);
}
}
@ -415,37 +414,14 @@ public class ApiScenarioReportService {
*
* @param reportId
*/
public void updateScenarioStatus(String reportId) {
public void updatePrevResult(String scenarioId, String reportId) {
if (StringUtils.isNotEmpty(reportId)) {
List<String> list = new LinkedList<>();
try {
list = JSON.parseObject(reportId, List.class);
} catch (Exception e) {
list.add(reportId);
}
if (CollectionUtils.isNotEmpty(list)) {
ApiScenarioReportExample scenarioReportExample = new ApiScenarioReportExample();
scenarioReportExample.createCriteria().andIdIn(list);
List<ApiScenarioReport> reportList = apiScenarioReportMapper.selectByExample(scenarioReportExample);
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
ApiScenarioReportMapper scenarioReportMapper = sqlSession.getMapper(ApiScenarioReportMapper.class);
if (CollectionUtils.isNotEmpty(reportList)) {
reportList.forEach(report -> {
report.setUpdateTime(System.currentTimeMillis());
report.setEndTime(System.currentTimeMillis());
String status = "Error";
report.setStatus(status);
scenarioReportMapper.updateByPrimaryKeySelective(report);
// 把上一条调试的数据内容清空
ApiScenarioReport prevResult = extApiScenarioReportMapper.selectPreviousReportByScenarioId(report.getScenarioId(), reportId);
if (prevResult != null) {
ApiScenarioReportDetailExample example = new ApiScenarioReportDetailExample();
example.createCriteria().andReportIdEqualTo(prevResult.getId());
apiScenarioReportDetailMapper.deleteByExample(example);
}
});
sqlSession.flushStatements();
}
// 把上一条调试的数据内容清空
ApiScenarioReport prevResult = extApiScenarioReportMapper.selectPreviousReportByScenarioId(scenarioId, reportId);
if (prevResult != null) {
ApiScenarioReportDetailExample example = new ApiScenarioReportDetailExample();
example.createCriteria().andReportIdEqualTo(prevResult.getId());
apiScenarioReportDetailMapper.deleteByExample(example);
}
}
}
@ -540,12 +516,14 @@ public class ApiScenarioReportService {
} catch (Exception e) {
list.add(result.getTestId());
}
if (StringUtils.isNotEmpty(result.getSetReportId())) {
list.add(result.getSetReportId());
}
ApiScenarioReportExample scenarioReportExample = new ApiScenarioReportExample();
scenarioReportExample.createCriteria().andScenarioIdIn(list);
List<ApiScenarioReport> reportList = apiScenarioReportMapper.selectByExample(scenarioReportExample);
list.forEach(item ->{
if(MessageCache.scenarioExecResourceLock.containsKey(item)) {
reportList.add(MessageCache.scenarioExecResourceLock.get(item));
}
});
for (ApiScenarioReport report : reportList) {
report.setStatus("Error");
apiScenarioReportMapper.updateByPrimaryKey(report);
@ -555,7 +533,7 @@ public class ApiScenarioReportService {
Object obj = MessageCache.cache.get(result.getSetReportId());
if (obj != null) {
ReportCounter counter = (ReportCounter) obj;
counter.setNumber(counter.getNumber() + 1);
counter.getCompletedIds().add(report.getId());
MessageCache.cache.put(result.getSetReportId(), counter);
}
}
@ -612,10 +590,11 @@ public class ApiScenarioReportService {
Object obj = MessageCache.cache.get(result.getSetReportId());
if (obj != null) {
ReportCounter counter = (ReportCounter) obj;
counter.setNumber(counter.getNumber() + 1);
counter.getCompletedIds().add(report.getId());
MessageCache.cache.put(result.getSetReportId(), counter);
}
}
updatePrevResult(report.getScenarioId(), report.getId());
}
}
// 针对未正常返回结果的报告计数

View File

@ -138,31 +138,35 @@ public class MsResultService {
}
public String getJmeterLogger(String testId, boolean removed) {
Long startTime = FixedTask.tasks.get(testId);
if (startTime == null) {
startTime = FixedTask.tasks.get("[" + testId + "]");
}
if (startTime == null) {
startTime = System.currentTimeMillis();
}
Long endTime = System.currentTimeMillis();
Long finalStartTime = startTime;
String logMessage = JmeterLoggerAppender.logger.entrySet().stream()
.filter(map -> map.getKey() > finalStartTime && map.getKey() < endTime)
.map(map -> map.getValue()).collect(Collectors.joining());
if (removed) {
if (processCache.get(testId) != null) {
try {
Thread.sleep(2000);
} catch (Exception e) {
}
try {
Long startTime = FixedTask.tasks.get(testId);
if (startTime == null) {
startTime = FixedTask.tasks.get("[" + testId + "]");
}
FixedTask.tasks.remove(testId);
if (startTime == null) {
startTime = System.currentTimeMillis();
}
Long endTime = System.currentTimeMillis();
Long finalStartTime = startTime;
String logMessage = JmeterLoggerAppender.logger.entrySet().stream()
.filter(map -> map.getKey() > finalStartTime && map.getKey() < endTime)
.map(map -> map.getValue()).collect(Collectors.joining());
if (removed) {
if (processCache.get(testId) != null) {
try {
Thread.sleep(2000);
} catch (Exception e) {
}
}
FixedTask.tasks.remove(testId);
}
if (FixedTask.tasks.isEmpty()) {
JmeterLoggerAppender.logger.clear();
}
return logMessage;
}catch (Exception e){
return "";
}
if (FixedTask.tasks.isEmpty()) {
JmeterLoggerAppender.logger.clear();
}
return logMessage;
}
public RequestResult getRequestResult(SampleResult result) {

View File

@ -31,7 +31,7 @@ public class RemakeReportService {
private TestPlanApiScenarioMapper testPlanApiScenarioMapper;
public void remake(RunRequest runRequest, RunModeConfig config, String reportId) {
if (MessageCache.cache.get(config.getAmassReport()) != null
if (config != null && config.getAmassReport() != null && MessageCache.cache.get(config.getAmassReport()) != null
&& MessageCache.cache.get(config.getAmassReport()).getReportIds() != null) {
MessageCache.cache.get(config.getAmassReport()).getReportIds().remove(reportId);
}