refactor(测试跟踪):批量插入数据过多导致OOM优化,新增批量utils

--story=1011417 --user=王旭 【Bug转需求】[测试跟踪] github#19980测试计划关联用例,当用例较多的时候崩溃(6000条) https://www.tapd.cn/55049933/s/1362368
This commit is contained in:
WangXu10 2023-04-13 15:04:31 +08:00 committed by jianxing
parent 2154fd9fb0
commit 69f886b230
1 changed files with 33 additions and 28 deletions

View File

@ -81,6 +81,7 @@ import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -649,8 +650,10 @@ public class TestPlanService {
// 尽量保持与用例顺序一致 // 尽量保持与用例顺序一致
Collections.reverse(testCaseIds); Collections.reverse(testCaseIds);
Long nextOrder = ServiceUtils.getNextOrder(request.getPlanId(), extTestPlanTestCaseMapper::getLastOrder); AtomicReference<Long> nextOrder = new AtomicReference<>(ServiceUtils.getNextOrder(request.getPlanId(), extTestPlanTestCaseMapper::getLastOrder));
for (String caseId : testCaseIds) { try {
SubListUtil.dealForSubList(testCaseIds, 1000, (subList) -> {
for (Object caseId : subList) {
TestPlanTestCaseWithBLOBs testPlanTestCase = new TestPlanTestCaseWithBLOBs(); TestPlanTestCaseWithBLOBs testPlanTestCase = new TestPlanTestCaseWithBLOBs();
testPlanTestCase.setId(UUID.randomUUID().toString()); testPlanTestCase.setId(UUID.randomUUID().toString());
testPlanTestCase.setCreateUser(SessionUtils.getUserId()); testPlanTestCase.setCreateUser(SessionUtils.getUserId());
@ -659,27 +662,29 @@ public class TestPlanService {
maintainer = SessionUtils.getUserId(); maintainer = SessionUtils.getUserId();
} }
testPlanTestCase.setExecutor(maintainer); testPlanTestCase.setExecutor(maintainer);
testPlanTestCase.setCaseId(caseId); testPlanTestCase.setCaseId(caseId.toString());
testPlanTestCase.setCreateTime(System.currentTimeMillis()); testPlanTestCase.setCreateTime(System.currentTimeMillis());
testPlanTestCase.setUpdateTime(System.currentTimeMillis()); testPlanTestCase.setUpdateTime(System.currentTimeMillis());
testPlanTestCase.setPlanId(request.getPlanId()); testPlanTestCase.setPlanId(request.getPlanId());
testPlanTestCase.setStatus(TestPlanStatus.Prepare.name()); testPlanTestCase.setStatus(TestPlanStatus.Prepare.name());
testPlanTestCase.setIsDel(false); testPlanTestCase.setIsDel(false);
testPlanTestCase.setOrder(nextOrder); testPlanTestCase.setOrder(nextOrder.get());
nextOrder += ServiceUtils.ORDER_STEP; nextOrder.updateAndGet(v -> v + ServiceUtils.ORDER_STEP);
batchMapper.insert(testPlanTestCase); batchMapper.insert(testPlanTestCase);
} }
sqlSession.flushStatements();
caseTestRelevance(request, testCaseIds);
resetStatus(testPlan.getId());
sqlSession.flushStatements(); sqlSession.flushStatements();
caseTestRelevance(request, subList);
});
} catch (Exception e) {
sqlSession.rollback();
throw new RuntimeException(e);
}finally {
if (sqlSession != null && sqlSessionFactory != null) { if (sqlSession != null && sqlSessionFactory != null) {
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
} }
} }
}
public void caseTestRelevance(PlanCaseRelevanceRequest request, List<String> testCaseIds) { public void caseTestRelevance(PlanCaseRelevanceRequest request, List<String> testCaseIds) {
//同步添加关联的接口和测试用例 //同步添加关联的接口和测试用例