refactor(接口测试): 优化接口场景批处理方法,调整批处理条数防止内存溢出

This commit is contained in:
Jianguo-Genius 2024-04-22 15:51:48 +08:00 committed by Craftsman
parent 08ca3d58bd
commit 2650f482f6
3 changed files with 18 additions and 19 deletions

View File

@ -2569,37 +2569,32 @@ public class ApiScenarioService extends MoveNodeService {
} }
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
ApiScenarioMapper mapper = sqlSession.getMapper(ApiScenarioMapper.class); ApiScenarioMapper mapper = sqlSession.getMapper(ApiScenarioMapper.class);
SubListUtils.dealForSubList(insertApiScenarioList, 100, subList -> {
subList.forEach(mapper::insertSelective); insertApiScenarioList.forEach(mapper::insertSelective);
});
response.setSuccess(insertApiScenarioList.size());
if (CollectionUtils.isNotEmpty(insertApiScenarioBlobList)) { if (CollectionUtils.isNotEmpty(insertApiScenarioBlobList)) {
ApiScenarioBlobMapper blobMapper = sqlSession.getMapper(ApiScenarioBlobMapper.class); ApiScenarioBlobMapper blobMapper = sqlSession.getMapper(ApiScenarioBlobMapper.class);
SubListUtils.dealForSubList(insertApiScenarioBlobList, 100, subList -> { insertApiScenarioBlobList.forEach(blobMapper::insertSelective);
subList.forEach(blobMapper::insertSelective);
});
} }
if (CollectionUtils.isNotEmpty(insertApiScenarioStepList)) { if (CollectionUtils.isNotEmpty(insertApiScenarioStepList)) {
ApiScenarioStepMapper stepMapper = sqlSession.getMapper(ApiScenarioStepMapper.class); ApiScenarioStepMapper stepMapper = sqlSession.getMapper(ApiScenarioStepMapper.class);
SubListUtils.dealForSubList(insertApiScenarioStepList, 100, subList -> { insertApiScenarioStepList.forEach(stepMapper::insertSelective);
subList.forEach(stepMapper::insertSelective);
});
} }
if (CollectionUtils.isNotEmpty(insertApiScenarioStepBlobList)) { if (CollectionUtils.isNotEmpty(insertApiScenarioStepBlobList)) {
ApiScenarioStepBlobMapper stepBlobMapper = sqlSession.getMapper(ApiScenarioStepBlobMapper.class); ApiScenarioStepBlobMapper stepBlobMapper = sqlSession.getMapper(ApiScenarioStepBlobMapper.class);
SubListUtils.dealForSubList(insertApiScenarioStepBlobList, 100, subList -> { insertApiScenarioStepBlobList.forEach(stepBlobMapper::insertSelective);
subList.forEach(stepBlobMapper::insertSelective);
});
} }
if (CollectionUtils.isNotEmpty(insertApiFileResourceList)) { if (CollectionUtils.isNotEmpty(insertApiFileResourceList)) {
SubListUtils.dealForSubList(insertApiFileResourceList, 100, subList -> { apiFileResourceService.batchInsert(insertApiFileResourceList);
apiFileResourceService.batchInsert(subList);
});
} }
sqlSession.flushStatements(); sqlSession.flushStatements();
if (sqlSessionFactory != null) { if (sqlSessionFactory != null) {
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
} }
response.setSuccess(insertApiScenarioList.size());
return response; return response;
} }

View File

@ -8,12 +8,12 @@ import java.util.function.Function;
//场景批量操作工具类 //场景批量操作工具类
public class ApiScenarioBatchOperationUtils { public class ApiScenarioBatchOperationUtils {
private static int MAX_OPERATION_SIZE = 100; private static int MAX_OPERATION_SIZE = 20;
public static <T> ApiScenarioBatchOperationResponse executeWithBatchOperationResponse(List<T> totalList, Function<List<T>, ApiScenarioBatchOperationResponse> subFunc) { public static <T> ApiScenarioBatchOperationResponse executeWithBatchOperationResponse(List<T> totalList, Function<List<T>, ApiScenarioBatchOperationResponse> subFunc) {
ApiScenarioBatchOperationResponse response = new ApiScenarioBatchOperationResponse(); ApiScenarioBatchOperationResponse response = new ApiScenarioBatchOperationResponse();
List<T> operationList = new ArrayList<>(totalList); List<T> operationList = new ArrayList<>(totalList);
while (operationList.size() > 100) { while (operationList.size() > MAX_OPERATION_SIZE) {
List<T> subList = operationList.subList(0, MAX_OPERATION_SIZE); List<T> subList = operationList.subList(0, MAX_OPERATION_SIZE);
response.merge( response.merge(
subFunc.apply(subList)); subFunc.apply(subList));

View File

@ -8,6 +8,7 @@ import jakarta.validation.groups.Default;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.Set; import java.util.Set;
import java.util.TreeSet;
@Component @Component
public class UserExcelValidateHelper { public class UserExcelValidateHelper {
@ -21,9 +22,12 @@ public class UserExcelValidateHelper {
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
Set<ConstraintViolation<T>> set = excelValidateHelper.validator.validate(obj, Default.class); Set<ConstraintViolation<T>> set = excelValidateHelper.validator.validate(obj, Default.class);
if (set != null && !set.isEmpty()) { if (set != null && !set.isEmpty()) {
//报错信息进行有序去重处理
Set<String> errorMsgSet = new TreeSet<>();
for (ConstraintViolation<T> cv : set) { for (ConstraintViolation<T> cv : set) {
result.append(cv.getMessage()).append("; "); errorMsgSet.add(cv.getMessage());
} }
errorMsgSet.forEach(item -> result.append(item).append("; "));
} }
return result.toString(); return result.toString();
} }