fix(UI 自动化) UI通过率计算出错

--bug=1016572 --user=张大海 【UI测试】github#17648,UI测试,通过率计算错误 https://www.tapd.cn/55049933/s/1239228
This commit is contained in:
zhangdahai112 2022-09-06 01:36:16 +08:00 committed by zhangdahai112
parent f01b30a811
commit 14b600c54f
2 changed files with 123 additions and 12 deletions

View File

@ -9,6 +9,7 @@ import io.metersphere.api.dto.automation.*;
import io.metersphere.api.dto.datacount.ApiDataCountResult; import io.metersphere.api.dto.datacount.ApiDataCountResult;
import io.metersphere.api.dto.definition.RunDefinitionRequest; import io.metersphere.api.dto.definition.RunDefinitionRequest;
import io.metersphere.api.jmeter.FixedCapacityUtils; import io.metersphere.api.jmeter.FixedCapacityUtils;
import io.metersphere.api.service.utils.PassRateUtil;
import io.metersphere.base.domain.*; import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.*; import io.metersphere.base.mapper.*;
import io.metersphere.base.mapper.ext.ExtApiDefinitionExecResultMapper; import io.metersphere.base.mapper.ext.ExtApiDefinitionExecResultMapper;
@ -505,17 +506,12 @@ public class ApiScenarioReportService {
if (testPlanUiScenario != null) { if (testPlanUiScenario != null) {
report.setScenarioId(testPlanUiScenario.getUiScenarioId()); report.setScenarioId(testPlanUiScenario.getUiScenarioId());
report.setEndTime(System.currentTimeMillis()); report.setEndTime(System.currentTimeMillis());
long successSize = requestResults.stream().filter(requestResult -> StringUtils.equalsIgnoreCase(requestResult.getStatus(), ScenarioStatus.Success.name())).count();
if (StringUtils.equalsAnyIgnoreCase(status, ExecuteResult.UN_EXECUTE.toString())) { if (StringUtils.equalsAnyIgnoreCase(status, ExecuteResult.UN_EXECUTE.toString())) {
testPlanUiScenario.setLastResult(ScenarioStatus.Fail.name()); testPlanUiScenario.setLastResult(ScenarioStatus.Fail.name());
} else { } else {
testPlanUiScenario.setLastResult(errorSize > 0 ? ScenarioStatus.Fail.name() : ScenarioStatus.Success.name()); testPlanUiScenario.setLastResult(errorSize > 0 ? ScenarioStatus.Fail.name() : ScenarioStatus.Success.name());
} }
if (CollectionUtils.isEmpty(requestResults)) { testPlanUiScenario.setPassRate(PassRateUtil.calculatePassRate(requestResults, report));
testPlanUiScenario.setPassRate("0");
} else {
testPlanUiScenario.setPassRate(new DecimalFormat("0%").format((float) successSize / requestResults.size()));
}
testPlanUiScenario.setReportId(report.getId()); testPlanUiScenario.setReportId(report.getId());
report.setEndTime(System.currentTimeMillis()); report.setEndTime(System.currentTimeMillis());
testPlanUiScenario.setUpdateTime(System.currentTimeMillis()); testPlanUiScenario.setUpdateTime(System.currentTimeMillis());
@ -536,12 +532,7 @@ public class ApiScenarioReportService {
scenario.setLastResult(errorSize > 0 ? ScenarioStatus.Fail.name() : ScenarioStatus.Success.name()); scenario.setLastResult(errorSize > 0 ? ScenarioStatus.Fail.name() : ScenarioStatus.Success.name());
} }
long successSize = requestResults.stream().filter(requestResult -> StringUtils.equalsIgnoreCase(requestResult.getStatus(), ScenarioStatus.Success.name())).count(); scenario.setPassRate(PassRateUtil.calculatePassRate(requestResults, report));
if (CollectionUtils.isEmpty(requestResults)) {
scenario.setPassRate("0");
} else {
scenario.setPassRate(new DecimalFormat("0%").format((float) successSize / requestResults.size()));
}
scenario.setReportId(dto.getReportId()); scenario.setReportId(dto.getReportId());
int executeTimes = 0; int executeTimes = 0;
if (scenario.getExecuteTimes() != null) { if (scenario.getExecuteTimes() != null) {

View File

@ -0,0 +1,120 @@
package io.metersphere.api.service.utils;
import com.alibaba.fastjson.JSONArray;
import io.metersphere.api.dto.StepTreeDTO;
import io.metersphere.api.dto.automation.ScenarioStatus;
import io.metersphere.base.domain.ApiScenarioReport;
import io.metersphere.base.domain.ApiScenarioReportResult;
import io.metersphere.base.domain.ApiScenarioReportStructureExample;
import io.metersphere.base.domain.ApiScenarioReportStructureWithBLOBs;
import io.metersphere.base.mapper.ApiScenarioReportStructureMapper;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.constants.SystemConstants;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;
/**
* API/UI/PERFORMANCE 通过率计算工具
*/
public class PassRateUtil {
private final static String UISCENARIO_TYPE_NAME = "scenario";
public static String calculatePassRate(List<ApiScenarioReportResult> requestResults, ApiScenarioReport report) {
if (CollectionUtils.isEmpty(requestResults)) {
return "0";
} else {
long successSize = requestResults.stream().filter(requestResult -> StringUtils.equalsIgnoreCase(requestResult.getStatus(), ScenarioStatus.Success.name())).count();
if (report.getReportType().startsWith(SystemConstants.TestTypeEnum.UI.name())) {
Map<String, String> resultIdMap = requestResults.stream().collect(Collectors.toMap(r -> r.getResourceId(), r -> r.getStatus()));
ApiScenarioReportStructureExample e = new ApiScenarioReportStructureExample();
e.createCriteria().andReportIdEqualTo(report.getId());
List<ApiScenarioReportStructureWithBLOBs> apiScenarioReportStructures = CommonBeanFactory.getBean(ApiScenarioReportStructureMapper.class).selectByExampleWithBLOBs(e);
if (CollectionUtils.isEmpty(apiScenarioReportStructures)) {
return "0";
}
List<StepTreeDTO> stepList = JSONArray.parseArray(new String(apiScenarioReportStructures.get(0).getResourceTree(), StandardCharsets.UTF_8), StepTreeDTO.class);
successSize = getUISuccessSize(resultIdMap, stepList);
if (CollectionUtils.isEmpty(stepList)) {
return "0";
}
return new DecimalFormat("0%").format((float) successSize / getTotalSteps(stepList));
} else {
return new DecimalFormat("0%").format((float) successSize / requestResults.size());
}
}
}
/**
* 计算 UI 成功的步骤数
*
* @param resultIdMap
* @param stepList
* @return
*/
private static long getUISuccessSize(Map<String, String> resultIdMap, List<StepTreeDTO> stepList) {
AtomicLong atomicLong = new AtomicLong();
stepList.forEach(stepFather -> {
stepFather.getChildren().forEach(step -> {
if (StringUtils.equalsIgnoreCase(step.getType(), UISCENARIO_TYPE_NAME)) {
AtomicInteger failCount = new AtomicInteger();
getUIFailStepCount(resultIdMap, Optional.ofNullable(step.getChildren()).orElse(new ArrayList<>()), failCount);
//复制或者嵌套场景的成功只算 1
if (failCount.get() == 0) {
atomicLong.getAndAdd(1);
}
} else {
if (resultIdMap.containsKey(step.getResourceId()) && StringUtils.equalsIgnoreCase(resultIdMap.get(step.getResourceId()), ScenarioStatus.Success.name())) {
atomicLong.incrementAndGet();
}
}
});
});
return atomicLong.get();
}
/**
* 递归叶子节点有失败的证明嵌套的场景是失败的
*
* @param stepTrees
* @param resultIdMap
* @param failCount
* @return
*/
private static void getUIFailStepCount(Map<String, String> resultIdMap, List<StepTreeDTO> stepTrees, AtomicInteger failCount) {
stepTrees.forEach(step -> {
if (StringUtils.equalsIgnoreCase(step.getType(), UISCENARIO_TYPE_NAME)) {
getUIFailStepCount(resultIdMap, Optional.ofNullable(step.getChildren()).orElse(new ArrayList<>()), failCount);
} else {
if (resultIdMap.containsKey(step.getResourceId()) && StringUtils.equalsIgnoreCase(resultIdMap.get(step.getResourceId()), ScenarioStatus.Error.name())) {
failCount.incrementAndGet();
}
}
});
}
/**
* 计算总步骤数
*
* @param stepList
* @return
*/
private static float getTotalSteps(List<StepTreeDTO> stepList) {
AtomicInteger integerAtomic = new AtomicInteger();
stepList.forEach(s -> integerAtomic.getAndAdd(Optional.ofNullable(s.getChildren()).orElse(new ArrayList<>()).size()));
return integerAtomic.get();
}
}