perf: 优化接口定义、接口场景、功能案例的模块树查找速度

优化接口定义、接口场景、功能案例的模块树查找速度
This commit is contained in:
song-tianyang 2021-05-31 18:01:37 +08:00 committed by 刘瑞斌
parent d719fefec4
commit aca31b166c
9 changed files with 171 additions and 13 deletions

View File

@ -88,16 +88,61 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
Map<String, List<String>> filters = new LinkedHashMap<>();
filters.put("status", list);
request.setFilters(filters);
// apiModules.forEach(node -> {
// List<String> moduleIds = new ArrayList<>();
// moduleIds = this.nodeList(apiModules, node.getId(), moduleIds);
// moduleIds.add(node.getId());
// request.setModuleIds(moduleIds);
// node.setCaseNum(extApiDefinitionMapper.moduleCount(request));
// });
//优化 所有统计SQL一次查询出来
List<String> allModuleIdList = new ArrayList<>();
for (ApiModuleDTO node : apiModules) {
List<String> moduleIds = new ArrayList<>();
moduleIds = this.nodeList(apiModules, node.getId(), moduleIds);
moduleIds.add(node.getId());
for (String moduleId : moduleIds) {
if(!allModuleIdList.contains(moduleId)){
allModuleIdList.add(moduleId);
}
}
}
request.setModuleIds(allModuleIdList);
List<Map<String,Object>> moduleCountList = extApiDefinitionMapper.moduleCountByCollection(request);
Map<String,Integer> moduleCountMap = this.parseModuleCountList(moduleCountList);
apiModules.forEach(node -> {
List<String> moduleIds = new ArrayList<>();
moduleIds = this.nodeList(apiModules, node.getId(), moduleIds);
moduleIds.add(node.getId());
request.setModuleIds(moduleIds);
node.setCaseNum(extApiDefinitionMapper.moduleCount(request));
int countNum = 0;
for (String moduleId : moduleIds) {
if(moduleCountMap.containsKey(moduleId)){
countNum += moduleCountMap.get(moduleId).intValue();
}
}
node.setCaseNum(countNum);
});
return getNodeTrees(apiModules);
}
private Map<String, Integer> parseModuleCountList(List<Map<String, Object>> moduleCountList) {
Map<String,Integer> returnMap = new HashMap<>();
for (Map<String, Object> map: moduleCountList){
Object moduleIdObj = map.get("moduleId");
Object countNumObj = map.get("countNum");
if(moduleIdObj!= null && countNumObj != null){
String moduleId = String.valueOf(moduleIdObj);
try {
Integer countNumInteger = new Integer(String.valueOf(countNumObj));
returnMap.put(moduleId,countNumInteger);
}catch (Exception e){
}
}
}
return returnMap;
}
public static List<String> nodeList(List<ApiModuleDTO> apiNodes, String pid, List<String> list) {
for (ApiModuleDTO node : apiNodes) {
//遍历出父id等于参数的idadd进子节点集合

View File

@ -87,16 +87,58 @@ public class ApiScenarioModuleService extends NodeTreeService<ApiScenarioModuleD
Map<String, List<String>> filters = new LinkedHashMap<>();
filters.put("status", list);
request.setFilters(filters);
//优化所有SQL统一查出来
// nodes.forEach(node -> {
// List<String> scenarioNodes = new ArrayList<>();
// scenarioNodes = this.nodeList(nodes, node.getId(), scenarioNodes);
// scenarioNodes.add(node.getId());
// request.setModuleIds(scenarioNodes);
// node.setCaseNum(extApiScenarioMapper.listModule(request));
// });
List<String> allModuleIdList = new ArrayList<>();
for (ApiScenarioModuleDTO node : nodes) {
List<String> moduleIds = new ArrayList<>();
moduleIds = this.nodeList(nodes, node.getId(), moduleIds);
moduleIds.add(node.getId());
for (String moduleId : moduleIds) {
if(!allModuleIdList.contains(moduleId)){
allModuleIdList.add(moduleId);
}
}
}
request.setModuleIds(allModuleIdList);
List<Map<String,Object>> moduleCountList = extApiScenarioMapper.listModuleByCollection(request);
Map<String,Integer> moduleCountMap = this.parseModuleCountList(moduleCountList);
nodes.forEach(node -> {
List<String> scenarioNodes = new ArrayList<>();
scenarioNodes = this.nodeList(nodes, node.getId(), scenarioNodes);
scenarioNodes.add(node.getId());
request.setModuleIds(scenarioNodes);
node.setCaseNum(extApiScenarioMapper.listModule(request));
List<String> moduleIds = new ArrayList<>();
moduleIds = this.nodeList(nodes, node.getId(), moduleIds);
moduleIds.add(node.getId());
int countNum = 0;
for (String moduleId : moduleIds) {
if(moduleCountMap.containsKey(moduleId)){
countNum += moduleCountMap.get(moduleId).intValue();
}
}
node.setCaseNum(countNum);
});
return getNodeTrees(nodes);
}
private Map<String, Integer> parseModuleCountList(List<Map<String, Object>> moduleCountList) {
Map<String,Integer> returnMap = new HashMap<>();
for (Map<String, Object> map: moduleCountList){
Object moduleIdObj = map.get("moduleId");
Object countNumObj = map.get("countNum");
if(moduleIdObj!= null && countNumObj != null){
String moduleId = String.valueOf(moduleIdObj);
try {
Integer countNumInteger = new Integer(String.valueOf(countNumObj));
returnMap.put(moduleId,countNumInteger);
}catch (Exception e){
}
}
}
return returnMap;
}
public static List<String> nodeList(List<ApiScenarioModuleDTO> nodes, String pid, List<String> list) {
for (ApiScenarioModuleDTO node : nodes) {

View File

@ -11,6 +11,7 @@ import io.metersphere.controller.request.BaseQueryRequest;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface ExtApiDefinitionMapper {
List<ApiSwaggerUrlDTO> selectScheduleList(@Param("projectId") String projectId);
@ -48,4 +49,6 @@ public interface ExtApiDefinitionMapper {
List<ApiDefinition> selectEffectiveIdByProjectId(String projectId);
List<ApiDefinitionResult> listByIds(@Param("ids") List<String> ids);
List<Map<String, Object>> moduleCountByCollection(@Param("request") ApiDefinitionRequest request);
}

View File

@ -492,6 +492,12 @@
<include refid="queryWhereCondition"/>
</select>
<select id="moduleCountByCollection" resultType="java.util.Map">
select module_id AS moduleId,count(id) AS countNum from api_definition
<include refid="queryWhereCondition"/>
GROUP BY module_id
</select>
<sql id="filter">
<if test="request.filters != null and request.filters.size() > 0">
<foreach collection="request.filters.entrySet()" index="key" item="values">

View File

@ -9,6 +9,7 @@ import io.metersphere.base.domain.ApiScenarioWithBLOBs;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface ExtApiScenarioMapper {
List<ApiScenarioDTO> list(@Param("request") ApiScenarioRequest request);
@ -48,4 +49,6 @@ public interface ExtApiScenarioMapper {
void updateCustomNumByProjectId(@Param("projectId") String projectId);
List<ApiScenarioWithBLOBs> listWithIds(@Param("ids") List<String> ids);
List<Map<String, Object>> listModuleByCollection(@Param("request") ApiScenarioRequest request);
}

View File

@ -136,6 +136,12 @@
select count(id) from api_scenario
<include refid="queryWhereCondition"/>
</select>
<select id="listModuleByCollection" resultType="java.util.Map">
select api_scenario_module_id AS moduleId,count(id) AS countNum from api_scenario
<include refid="queryWhereCondition"/>
GROUP BY api_scenario_module_id
</select>
<select id="list" resultMap="BaseResultMap">
select api_scenario.id, api_scenario.project_id, api_scenario.tags, api_scenario.user_id, api_scenario.num,
api_scenario.custom_num,

View File

@ -10,6 +10,7 @@ import io.metersphere.track.response.TrackCountResult;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
import java.util.Set;
public interface ExtTestCaseMapper {
@ -96,4 +97,6 @@ public interface ExtTestCaseMapper {
void updateTestCaseCustomNumByProjectId(@Param("projectId") String projectId);
List<String> selectRelateIdsByQuery(@Param("request") BaseQueryRequest query);
List<Map<String, Object>> moduleCountByCollection(@Param("request")QueryTestCaseRequest request);
}

View File

@ -176,6 +176,12 @@
select count(id) from test_case
<include refid="queryWhereCondition"/>
</select>
<select id="moduleCountByCollection" resultType="java.util.Map">
select node_id AS moduleId,count(id) AS countNum from test_case
<include refid="queryWhereCondition"/>
GROUP BY node_id
</select>
<select id="listIds" resultType="io.metersphere.track.dto.TestCaseDTO">
select
<if test="request.selectFields != null and request.selectFields.size() > 0">

View File

@ -142,16 +142,60 @@ public class TestCaseNodeService extends NodeTreeService<TestCaseNodeDTO> {
QueryTestCaseRequest request = new QueryTestCaseRequest();
request.setUserId(SessionUtils.getUserId());
request.setProjectId(projectId);
// for (TestCaseNodeDTO node : testCaseNodes) {
// List<String> nodeIds = new ArrayList<>();
// nodeIds = this.nodeList(testCaseNodes, node.getId(), nodeIds);
// nodeIds.add(node.getId());
// request.setNodeIds(nodeIds);
// node.setCaseNum(extTestCaseMapper.moduleCount(request));
// }
//优化将for循环内的SQL抽出来只差一次
List<String> allModuleIdList = new ArrayList<>();
for (TestCaseNodeDTO node : testCaseNodes) {
List<String> nodeIds = new ArrayList<>();
nodeIds = this.nodeList(testCaseNodes, node.getId(), nodeIds);
nodeIds.add(node.getId());
request.setNodeIds(nodeIds);
node.setCaseNum(extTestCaseMapper.moduleCount(request));
List<String> moduleIds = new ArrayList<>();
moduleIds = this.nodeList(testCaseNodes, node.getId(), moduleIds);
moduleIds.add(node.getId());
for (String moduleId : moduleIds) {
if(!allModuleIdList.contains(moduleId)){
allModuleIdList.add(moduleId);
}
}
}
request.setModuleIds(allModuleIdList);
List<Map<String,Object>> moduleCountList = extTestCaseMapper.moduleCountByCollection(request);
Map<String,Integer> moduleCountMap = this.parseModuleCountList(moduleCountList);
testCaseNodes.forEach(node -> {
List<String> moduleIds = new ArrayList<>();
moduleIds = this.nodeList(testCaseNodes, node.getId(), moduleIds);
moduleIds.add(node.getId());
int countNum = 0;
for (String moduleId : moduleIds) {
if(moduleCountMap.containsKey(moduleId)){
countNum += moduleCountMap.get(moduleId).intValue();
}
}
node.setCaseNum(countNum);
});
return getNodeTrees(testCaseNodes);
}
private Map<String, Integer> parseModuleCountList(List<Map<String, Object>> moduleCountList) {
Map<String,Integer> returnMap = new HashMap<>();
for (Map<String, Object> map: moduleCountList){
Object moduleIdObj = map.get("moduleId");
Object countNumObj = map.get("countNum");
if(moduleIdObj!= null && countNumObj != null){
String moduleId = String.valueOf(moduleIdObj);
try {
Integer countNumInteger = new Integer(String.valueOf(countNumObj));
returnMap.put(moduleId,countNumInteger);
}catch (Exception e){
}
}
}
return returnMap;
}
public static List<String> nodeList(List<TestCaseNodeDTO> testCaseNodes, String pid, List<String> list) {
for (TestCaseNodeDTO node : testCaseNodes) {
//遍历出父id等于参数的idadd进子节点集合