perf(接口测试): 场景编辑依赖关系优化

This commit is contained in:
fit2-zhao 2024-12-23 18:19:51 +08:00 committed by Craftsman
parent 989c59aaf2
commit d7ada8904f
4 changed files with 50 additions and 37 deletions

View File

@ -40,6 +40,15 @@ public class ApiAutomationRelationshipEdgeService {
beforeReferenceRelationships = this.contentAnalysis(preBlobs); beforeReferenceRelationships = this.contentAnalysis(preBlobs);
} }
// 如果有新增的关系进行批量保存
if (CollectionUtils.isNotEmpty(referenceRelationships)) {
RelationshipEdgeRequest request = new RelationshipEdgeRequest();
request.setId(scenarioWithBLOBs.getId());
request.setTargetIds(referenceRelationships);
request.setType("API_SCENARIO");
relationshipEdgeService.saveBatch(request);
}
// 比较并处理关系 // 比较并处理关系
List<String> removedRelationships = new ArrayList<>(beforeReferenceRelationships); List<String> removedRelationships = new ArrayList<>(beforeReferenceRelationships);
removedRelationships.removeAll(referenceRelationships); removedRelationships.removeAll(referenceRelationships);
@ -48,16 +57,6 @@ public class ApiAutomationRelationshipEdgeService {
if (CollectionUtils.isNotEmpty(removedRelationships)) { if (CollectionUtils.isNotEmpty(removedRelationships)) {
new Thread(() -> relationshipEdgeService.delete(scenarioWithBLOBs.getId(), removedRelationships)).start(); new Thread(() -> relationshipEdgeService.delete(scenarioWithBLOBs.getId(), removedRelationships)).start();
} }
// 如果有新增的关系进行批量保存
if (CollectionUtils.isNotEmpty(referenceRelationships)) {
RelationshipEdgeRequest request = new RelationshipEdgeRequest();
request.setId(scenarioWithBLOBs.getId());
request.setTargetIds(referenceRelationships);
request.setType("API_SCENARIO");
relationshipEdgeService.saveBatch(request);
}
} }
private List<String> contentAnalysis(ApiScenarioWithBLOBs scenarioWithBLOBs) { private List<String> contentAnalysis(ApiScenarioWithBLOBs scenarioWithBLOBs) {

View File

@ -300,10 +300,19 @@ public class ApiScenarioService {
scenario.setRefId(request.getId()); scenario.setRefId(request.getId());
scenario.setLatest(true); scenario.setLatest(true);
apiScenarioMapper.insert(scenario);
apiScenarioReferenceIdService.saveApiAndScenarioRelation(scenario);
// 存储依赖关系 // 存储依赖关系
LogUtil.info("initRelationshipEdge start");
apiAutomationRelationshipEdgeService.initRelationshipEdge(null, scenario); apiAutomationRelationshipEdgeService.initRelationshipEdge(null, scenario);
LogUtil.info("initRelationshipEdge end");
apiScenarioMapper.insert(scenario);
// 依赖关系处理
new Thread(() -> {
apiScenarioReferenceIdService.saveApiAndScenarioRelation(scenario);
uploadFiles(request, bodyFiles, scenarioFiles);
}).start();
apiTestCaseService.checkAndSendReviewMessage( apiTestCaseService.checkAndSendReviewMessage(
scenario.getId(), scenario.getId(),
scenario.getName(), scenario.getName(),
@ -315,7 +324,6 @@ public class ApiScenarioService {
scenario.getPrincipal() scenario.getPrincipal()
); );
uploadFiles(request, bodyFiles, scenarioFiles);
return scenario; return scenario;
} }
@ -373,6 +381,7 @@ public class ApiScenarioService {
} }
public ApiScenarioWithBLOBs update(SaveApiScenarioRequest request, List<MultipartFile> bodyFiles, List<MultipartFile> scenarioFiles) { public ApiScenarioWithBLOBs update(SaveApiScenarioRequest request, List<MultipartFile> bodyFiles, List<MultipartFile> scenarioFiles) {
LogUtil.info("update scenario start");
checkNameExist(request, false); checkNameExist(request, false);
checkScenarioNum(request); checkScenarioNum(request);
//如果场景有TCP步骤的话也要做参数计算处理 //如果场景有TCP步骤的话也要做参数计算处理
@ -412,12 +421,26 @@ public class ApiScenarioService {
apiScenarioMapper.updateByExampleSelective(apiScenarioWithBLOBs, example); apiScenarioMapper.updateByExampleSelective(apiScenarioWithBLOBs, example);
} }
apiScenarioReferenceIdService.saveApiAndScenarioRelation(scenario);
extScheduleMapper.updateNameByResourceID(request.getId(), request.getName());// 修改场景name同步到修改首页定时任务
uploadFiles(request, bodyFiles, scenarioFiles);
// 存储依赖关系 // 存储依赖关系
LogUtil.info("initRelationshipEdge start");
apiAutomationRelationshipEdgeService.initRelationshipEdge(beforeScenario, scenario); apiAutomationRelationshipEdgeService.initRelationshipEdge(beforeScenario, scenario);
LogUtil.info("initRelationshipEdge end");
new Thread(() -> {
apiScenarioReferenceIdService.saveApiAndScenarioRelation(scenario);
extScheduleMapper.updateNameByResourceID(request.getId(), request.getName());// 修改场景name同步到修改首页定时任务
uploadFiles(request, bodyFiles, scenarioFiles);
String defaultVersion = baseProjectVersionMapper.getDefaultVersion(request.getProjectId());
if (StringUtils.equalsIgnoreCase(request.getVersionId(), defaultVersion)) {
checkAndSetLatestVersion(beforeScenario.getRefId());
}
//同步修改所有版本的模块路径
updateOtherVersionModule(beforeScenario.getRefId(), scenario);
// 存储附件关系
extFileAssociationService.saveScenario(scenario.getId(), request.getScenarioDefinition());
}).start();
apiTestCaseService.checkAndSendReviewMessage( apiTestCaseService.checkAndSendReviewMessage(
scenario.getId(), scenario.getId(),
@ -430,14 +453,7 @@ public class ApiScenarioService {
scenario.getPrincipal() scenario.getPrincipal()
); );
String defaultVersion = baseProjectVersionMapper.getDefaultVersion(request.getProjectId()); LogUtil.info("update scenario end");
if (StringUtils.equalsIgnoreCase(request.getVersionId(), defaultVersion)) {
checkAndSetLatestVersion(beforeScenario.getRefId());
}
//同步修改所有版本的模块路径
updateOtherVersionModule(beforeScenario.getRefId(), scenario);
// 存储附件关系
extFileAssociationService.saveScenario(scenario.getId(), request.getScenarioDefinition());
return scenario; return scenario;
} }

View File

@ -3,19 +3,15 @@
<mapper namespace="io.metersphere.base.mapper.ext.BaseRelationshipEdgeMapper"> <mapper namespace="io.metersphere.base.mapper.ext.BaseRelationshipEdgeMapper">
<select id="getGraphIdsByNodeIds" resultType="java.lang.String"> <select id="getGraphIdsByNodeIds" resultType="java.lang.String">
select graph_id select distinct graph_id
from relationship_edge from relationship_edge
where source_id in where source_id in
<foreach collection="ids" item="id" open="(" close=")" separator=","> <foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id} #{id}
</foreach> </foreach>
union or target_id in
select graph_id
from relationship_edge
where target_id in
<foreach collection="ids" item="id" open="(" close=")" separator=","> <foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id} #{id}
</foreach> </foreach>
</select> </select>
</mapper> </mapper>

View File

@ -7,6 +7,7 @@ import io.metersphere.base.domain.RelationshipEdgeKey;
import io.metersphere.base.mapper.RelationshipEdgeMapper; import io.metersphere.base.mapper.RelationshipEdgeMapper;
import io.metersphere.base.mapper.ext.BaseRelationshipEdgeMapper; import io.metersphere.base.mapper.ext.BaseRelationshipEdgeMapper;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.SessionUtils; import io.metersphere.commons.utils.SessionUtils;
import io.metersphere.request.RelationshipEdgeRequest; import io.metersphere.request.RelationshipEdgeRequest;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
@ -206,7 +207,10 @@ public class RelationshipEdgeService {
*/ */
public void saveBatch(RelationshipEdgeRequest request) { public void saveBatch(RelationshipEdgeRequest request) {
String graphId = UUID.randomUUID().toString(); String graphId = UUID.randomUUID().toString();
LogUtil.info("saveBatch graphId: " + graphId);
List<RelationshipEdge> relationshipEdges = getEdgesBySaveRequest(request); List<RelationshipEdge> relationshipEdges = getEdgesBySaveRequest(request);
LogUtil.info("saveBatch relationshipEdges: " + relationshipEdges.size());
Set<String> addEdgesIds = new HashSet<>(); Set<String> addEdgesIds = new HashSet<>();
if (CollectionUtils.isNotEmpty(request.getTargetIds())) { if (CollectionUtils.isNotEmpty(request.getTargetIds())) {
@ -235,7 +239,8 @@ public class RelationshipEdgeService {
MSException.throwException("关联后存在循环依赖,请检查依赖关系"); MSException.throwException("关联后存在循环依赖,请检查依赖关系");
} }
}); });
// 合并图
LogUtil.info("开始合并图内容");
new Thread(() -> { new Thread(() -> {
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
RelationshipEdgeMapper batchMapper = sqlSession.getMapper(RelationshipEdgeMapper.class); RelationshipEdgeMapper batchMapper = sqlSession.getMapper(RelationshipEdgeMapper.class);
@ -258,7 +263,6 @@ public class RelationshipEdgeService {
if (sqlSessionFactory != null) { if (sqlSessionFactory != null) {
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
} }
}).start(); }).start();
} }
@ -293,11 +297,9 @@ public class RelationshipEdgeService {
if (CollectionUtils.isEmpty(graphIds)) { if (CollectionUtils.isEmpty(graphIds)) {
return new ArrayList<>(); return new ArrayList<>();
} }
RelationshipEdgeExample example = new RelationshipEdgeExample(); RelationshipEdgeExample example = new RelationshipEdgeExample();
example.createCriteria() example.createCriteria()
.andGraphIdIn(graphIds.stream() .andGraphIdIn(graphIds);
.distinct().collect(Collectors.toList()));
return relationshipEdgeMapper.selectByExample(example); return relationshipEdgeMapper.selectByExample(example);
} }
@ -321,7 +323,7 @@ public class RelationshipEdgeService {
markSet.add(id); markSet.add(id);
visitedSet.add(id); visitedSet.add(id);
ArrayList<String> nextLevelNodes = new ArrayList<>(); ArrayList<String> nextLevelNodes = new ArrayList();
for (RelationshipEdge relationshipEdge : edges) { for (RelationshipEdge relationshipEdge : edges) {
if (id.equals(relationshipEdge.getSourceId())) { if (id.equals(relationshipEdge.getSourceId())) {
nextLevelNodes.add(relationshipEdge.getTargetId()); nextLevelNodes.add(relationshipEdge.getTargetId());