perf(接口测试): 保存场景优化

This commit is contained in:
fit2-zhao 2022-04-07 14:42:01 +08:00 committed by fit2-zhao
parent c2e7a1a0f6
commit d2ac8ae5c6
4 changed files with 132 additions and 166 deletions

View File

@ -255,7 +255,7 @@ public class ApiAutomationService {
esbApiParamService.checkScenarioRequests(request);
apiScenarioMapper.insert(scenario);
apiScenarioReferenceIdService.saveByApiScenario(scenario);
apiScenarioReferenceIdService.saveApiAndScenarioRelation(scenario);
// 存储依赖关系
ApiAutomationRelationshipEdgeService relationshipEdgeService = CommonBeanFactory.getBean(ApiAutomationRelationshipEdgeService.class);
if (relationshipEdgeService != null) {
@ -384,12 +384,11 @@ public class ApiAutomationService {
apiScenarioMapper.updateByExampleSelective(apiScenarioWithBLOBs, example);
}
apiScenarioReferenceIdService.saveByApiScenario(scenario);
apiScenarioReferenceIdService.saveApiAndScenarioRelation(scenario);
extScheduleMapper.updateNameByResourceID(request.getId(), request.getName());// 修改场景name同步到修改首页定时任务
uploadFiles(request, bodyFiles, scenarioFiles);
// 存储依赖关系
ApiAutomationRelationshipEdgeService relationshipEdgeService = CommonBeanFactory.getBean(ApiAutomationRelationshipEdgeService.class);
if (relationshipEdgeService != null) {
@ -1101,7 +1100,7 @@ public class ApiAutomationService {
item.setScenarioDefinition(JSONObject.toJSONString(object));
}
apiScenarioMapper.updateByPrimaryKeySelective(item);
apiScenarioReferenceIdService.saveByApiScenario(item);
apiScenarioReferenceIdService.saveApiAndScenarioRelation(item);
});
}
}
@ -1140,7 +1139,7 @@ public class ApiAutomationService {
scenarioWithBLOBs.setLatest(true);
checkReferenceCase(scenarioWithBLOBs, apiTestCaseMapper, apiDefinitionMapper);
batchMapper.insert(scenarioWithBLOBs);
apiScenarioReferenceIdService.saveByApiScenario(scenarioWithBLOBs);
apiScenarioReferenceIdService.saveApiAndScenarioRelation(scenarioWithBLOBs);
} else {
//如果存在则修改
if (StringUtils.isEmpty(apiTestImportRequest.getUpdateVersionId())) {
@ -1170,7 +1169,7 @@ public class ApiAutomationService {
batchMapper.updateByPrimaryKeyWithBLOBs(scenarioWithBLOBs);
}
checkReferenceCase(scenarioWithBLOBs, apiTestCaseMapper, apiDefinitionMapper);
apiScenarioReferenceIdService.saveByApiScenario(scenarioWithBLOBs);
apiScenarioReferenceIdService.saveApiAndScenarioRelation(scenarioWithBLOBs);
extApiScenarioMapper.clearLatestVersion(scenarioWithBLOBs.getRefId());
extApiScenarioMapper.addLatestVersion(scenarioWithBLOBs.getRefId());
}
@ -1247,7 +1246,7 @@ public class ApiAutomationService {
if (relationshipEdgeService != null) {
relationshipEdgeService.initRelationshipEdge(null, scenarioWithBLOBs);
}
apiScenarioReferenceIdService.saveByApiScenario(scenarioWithBLOBs);
apiScenarioReferenceIdService.saveApiAndScenarioRelation(scenarioWithBLOBs);
}
} else {
@ -1396,6 +1395,7 @@ public class ApiAutomationService {
ids.add(scenario.getId());
}
}
public void setHashTree(JSONArray hashTree) {
try {
if (CollectionUtils.isNotEmpty(hashTree)) {
@ -1630,13 +1630,7 @@ public class ApiAutomationService {
public List<ApiMethodUrlDTO> parseUrl(ApiScenarioWithBLOBs scenario) {
List<ApiMethodUrlDTO> urlList = new ArrayList<>();
String scenarioDefinition = scenario.getScenarioDefinition();
JSONObject scenarioObj = JSONObject.parseObject(scenarioDefinition);
List<ApiMethodUrlDTO> stepUrlList = hashTreeService.getMethodUrlDTOByHashTreeJsonObj(scenarioObj);
if (CollectionUtils.isNotEmpty(stepUrlList)) {
Collection unionList = CollectionUtils.union(urlList, stepUrlList);
urlList = new ArrayList<>(unionList);
}
// 去除未生效且影响性能的方法
return urlList;
}
@ -1750,18 +1744,16 @@ public class ApiAutomationService {
updateModel.setId(scenario.getId());
updateModel.setUseUrl(JSONArray.toJSONString(useUrl));
apiScenarioMapper.updateByPrimaryKeySelective(updateModel);
apiScenarioReferenceIdService.saveByApiScenario(updateModel);
updateModel = null;
apiScenarioReferenceIdService.saveApiAndScenarioRelation(updateModel);
}
}
scenario = null;
}
}
public void checkApiScenarioReferenceId() {
List<ApiScenarioWithBLOBs> scenarioNoRefs = extApiScenarioMapper.selectByNoReferenceId();
for (ApiScenarioWithBLOBs model : scenarioNoRefs) {
apiScenarioReferenceIdService.saveByApiScenario(model);
apiScenarioReferenceIdService.saveApiAndScenarioRelation(model);
}
}

View File

@ -6,11 +6,14 @@ import io.metersphere.base.domain.ApiScenarioReferenceId;
import io.metersphere.base.domain.ApiScenarioReferenceIdExample;
import io.metersphere.base.domain.ApiScenarioWithBLOBs;
import io.metersphere.base.mapper.ApiScenarioReferenceIdMapper;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.SessionUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -26,6 +29,8 @@ import java.util.*;
public class ApiScenarioReferenceIdService {
@Resource
private ApiScenarioReferenceIdMapper apiScenarioReferenceIdMapper;
@Resource
private SqlSessionFactory sqlSessionFactory;
public List<ApiScenarioReferenceId> findByReferenceIds(List<String> deleteIds) {
if (CollectionUtils.isEmpty(deleteIds)) {
@ -43,112 +48,80 @@ public class ApiScenarioReferenceIdService {
apiScenarioReferenceIdMapper.deleteByExample(example);
}
public void saveByApiScenario(ApiScenarioWithBLOBs scenario) {
public void saveApiAndScenarioRelation(ApiScenarioWithBLOBs scenario) {
if (scenario.getId() == null) {
return;
}
this.deleteByScenarioId(scenario.getId());
long createTime = System.currentTimeMillis();
String createUser = SessionUtils.getUserId();
Map<String, ApiScenarioReferenceId> refreceIdDic = new HashMap<>();
try {
if (scenario.getScenarioDefinition() != null) {
Map<String, ApiScenarioReferenceId> referenceIdMap = new HashMap<>();
if (StringUtils.isNotEmpty(scenario.getScenarioDefinition())) {
JSONObject jsonObject = JSONObject.parseObject(scenario.getScenarioDefinition());
if (jsonObject.containsKey("hashTree")) {
JSONArray testElementList = jsonObject.getJSONArray("hashTree");
for (int index = 0; index < testElementList.size(); index++) {
JSONObject item = testElementList.getJSONObject(index);
String refId = "";
String refrenced = "";
String dataType = "";
if(item.containsKey("id")){
refId = item.getString("id");
if (!jsonObject.containsKey(MsHashTreeService.HASH_TREE)) {
return;
}
if(item.containsKey("referenced")){
refrenced = item.getString("referenced");
JSONArray hashTree = jsonObject.getJSONArray(MsHashTreeService.HASH_TREE);
for (int index = 0; index < hashTree.size(); index++) {
JSONObject item = hashTree.getJSONObject(index);
if (item == null) {
continue;
}
if(item.containsKey("refType")){
dataType = item.getString("refType");
}
if (StringUtils.isNotEmpty(refId) && StringUtils.isNotEmpty(refrenced)) {
if (item.containsKey(MsHashTreeService.ID) && item.containsKey(MsHashTreeService.REFERENCED)) {
ApiScenarioReferenceId saveItem = new ApiScenarioReferenceId();
saveItem.setId(UUID.randomUUID().toString());
saveItem.setApiScenarioId(scenario.getId());
saveItem.setCreateTime(createTime);
saveItem.setCreateUserId(createUser);
saveItem.setReferenceId(refId);
saveItem.setReferenceType(refrenced);
saveItem.setDataType(dataType);
refreceIdDic.put(refId,saveItem);
saveItem.setReferenceId(item.getString(MsHashTreeService.ID));
saveItem.setReferenceType(item.getString(MsHashTreeService.REFERENCED));
saveItem.setDataType(item.getString(MsHashTreeService.REF_TYPE));
referenceIdMap.put(item.getString(MsHashTreeService.ID), saveItem);
}
if(item.containsKey("hashTree")){
refreceIdDic.putAll(this.deepParseTestElement(createTime,createUser,scenario.getId(),item.getJSONArray("hashTree")));
if (item.containsKey(MsHashTreeService.HASH_TREE)) {
referenceIdMap.putAll(this.deepElementRelation(scenario.getId(), item.getJSONArray(MsHashTreeService.HASH_TREE)));
}
}
}
if (MapUtils.isNotEmpty(referenceIdMap)) {
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
ApiScenarioReferenceIdMapper referenceIdMapper = sqlSession.getMapper(ApiScenarioReferenceIdMapper.class);
for (ApiScenarioReferenceId apiScenarioReferenceId : referenceIdMap.values()) {
apiScenarioReferenceId.setCreateTime(System.currentTimeMillis());
apiScenarioReferenceId.setCreateUserId(SessionUtils.getUserId());
referenceIdMapper.insert(apiScenarioReferenceId);
}
} catch (Exception e) {
LogUtil.error(e);
}
if(MapUtils.isNotEmpty(refreceIdDic)){
for (ApiScenarioReferenceId model:refreceIdDic.values()) {
apiScenarioReferenceIdMapper.insert(model);
sqlSession.flushStatements();
if (sqlSession != null && sqlSessionFactory != null) {
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
}
} else {
ApiScenarioReferenceId saveItem = new ApiScenarioReferenceId();
saveItem.setId(UUID.randomUUID().toString());
saveItem.setApiScenarioId(scenario.getId());
saveItem.setCreateTime(createTime);
saveItem.setCreateUserId(createUser);
try{
saveItem.setCreateTime(System.currentTimeMillis());
saveItem.setCreateUserId(SessionUtils.getUserId());
apiScenarioReferenceIdMapper.insert(saveItem);
}catch (Exception e){
LogUtil.error(e);
}
}
}
public Map<String,ApiScenarioReferenceId> deepParseTestElement(long createTime,String createUser,String scenarioId,JSONArray testElementList){
Map<String,ApiScenarioReferenceId> returnMap = new HashMap<>();
if(CollectionUtils.isNotEmpty(testElementList)){
for (int index = 0; index < testElementList.size(); index++) {
JSONObject item = testElementList.getJSONObject(index);
String refId = "";
String refrenced = "";
String dataType = "";
if(item.containsKey("id")){
refId = item.getString("id");
}
if(item.containsKey("referenced")){
refrenced = item.getString("referenced");
}
if(item.containsKey("refType")){
dataType = item.getString("refType");
}
if (StringUtils.isNotEmpty(refId) && StringUtils.isNotEmpty(refrenced)) {
public Map<String, ApiScenarioReferenceId> deepElementRelation(String scenarioId, JSONArray hashTree) {
Map<String, ApiScenarioReferenceId> deepRelations = new HashMap<>();
if (CollectionUtils.isNotEmpty(hashTree)) {
for (int index = 0; index < hashTree.size(); index++) {
JSONObject item = hashTree.getJSONObject(index);
if (item.containsKey(MsHashTreeService.ID) && item.containsKey(MsHashTreeService.REFERENCED)) {
ApiScenarioReferenceId saveItem = new ApiScenarioReferenceId();
saveItem.setId(UUID.randomUUID().toString());
saveItem.setApiScenarioId(scenarioId);
saveItem.setCreateTime(createTime);
saveItem.setCreateUserId(createUser);
saveItem.setReferenceId(refId);
saveItem.setReferenceType(refrenced);
saveItem.setDataType(dataType);
returnMap.put(refId,saveItem);
saveItem.setReferenceId(item.getString(MsHashTreeService.ID));
saveItem.setReferenceType(item.getString(MsHashTreeService.REFERENCED));
saveItem.setDataType(item.getString(MsHashTreeService.REF_TYPE));
deepRelations.put(item.getString(MsHashTreeService.ID), saveItem);
}
if(item.containsKey("hashTree")){
returnMap.putAll(this.deepParseTestElement(createTime,createUser,scenarioId,item.getJSONArray("hashTree")));
if (item.containsKey(MsHashTreeService.HASH_TREE)) {
deepRelations.putAll(this.deepElementRelation(scenarioId, item.getJSONArray(MsHashTreeService.HASH_TREE)));
}
}
}
return returnMap;
return deepRelations;
}
public List<ApiScenarioReferenceId> findByReferenceIdsAndRefType(List<String> deleteIds, String referenceType) {

View File

@ -371,7 +371,7 @@ public class HistoricalDataUpgradeService {
List<ApiMethodUrlDTO> useUrl = apiAutomationService.parseUrl(scenario);
scenario.setUseUrl(JSONArray.toJSONString(useUrl));
mapper.updateByPrimaryKeySelective(scenario);
apiScenarioReferenceIdService.saveByApiScenario(scenario);
apiScenarioReferenceIdService.saveApiAndScenarioRelation(scenario);
} else {
scenario = new ApiScenarioWithBLOBs();
scenario.setId(id);
@ -392,7 +392,7 @@ public class HistoricalDataUpgradeService {
List<ApiMethodUrlDTO> useUrl = apiAutomationService.parseUrl(scenario);
scenario.setUseUrl(JSONArray.toJSONString(useUrl));
mapper.insert(scenario);
apiScenarioReferenceIdService.saveByApiScenario(scenario);
apiScenarioReferenceIdService.saveApiAndScenarioRelation(scenario);
}
}

View File

@ -34,32 +34,33 @@ public class MsHashTreeService {
@Resource
private ExtApiScenarioMapper extApiScenarioMapper;
private static final String CASE = "CASE";
private static final String REFERENCED = "referenced";
private static final String REF = "REF";
private static final String REF_TYPE = "refType";
private static final String ID = "id";
private static final String NAME = "name";
private static final String SCENARIO = "scenario";
private static final String TYPE = "type";
private static final String HASH_TREE = "hashTree";
private static final String PATH = "path";
private static final String METHOD = "method";
private static final String ENABLE = "enable";
private static final String NUM = "num";
private static final String ENV_ENABLE = "environmentEnable";
private static final String VARIABLE_ENABLE = "variableEnable";
private static final String DISABLED = "disabled";
private static final String VERSION_NAME = "versionName";
private static final String VERSION_ENABLE = "versionEnable";
private static final String URL = "url";
private static final String HEADERS = "headers";
private static final String REST = "rest";
private static final String BODY = "body";
private static final String ARGUMENTS = "arguments";
private static final String AUTH_MANAGER = "authManager";
private static final String PROJECT_ID = "projectId";
private static final String ACTIVE = "active";
public static final String CASE = "CASE";
public static final String REFERENCED = "referenced";
public static final String REF = "REF";
public static final String CREATED = "Created";
public static final String REF_TYPE = "refType";
public static final String ID = "id";
public static final String NAME = "name";
public static final String SCENARIO = "scenario";
public static final String TYPE = "type";
public static final String HASH_TREE = "hashTree";
public static final String PATH = "path";
public static final String METHOD = "method";
public static final String ENABLE = "enable";
public static final String NUM = "num";
public static final String ENV_ENABLE = "environmentEnable";
public static final String VARIABLE_ENABLE = "variableEnable";
public static final String DISABLED = "disabled";
public static final String VERSION_NAME = "versionName";
public static final String VERSION_ENABLE = "versionEnable";
public static final String URL = "url";
public static final String HEADERS = "headers";
public static final String REST = "rest";
public static final String BODY = "body";
public static final String ARGUMENTS = "arguments";
public static final String AUTH_MANAGER = "authManager";
public static final String PROJECT_ID = "projectId";
public static final String ACTIVE = "active";
public void setHashTree(JSONArray hashTree) {
// 将引用转成复制