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);
ApiScenarioMapper mapper = sqlSession.getMapper(ApiScenarioMapper.class);
SubListUtils.dealForSubList(insertApiScenarioList, 100, subList -> {
subList.forEach(mapper::insertSelective);
});
response.setSuccess(insertApiScenarioList.size());
insertApiScenarioList.forEach(mapper::insertSelective);
if (CollectionUtils.isNotEmpty(insertApiScenarioBlobList)) {
ApiScenarioBlobMapper blobMapper = sqlSession.getMapper(ApiScenarioBlobMapper.class);
SubListUtils.dealForSubList(insertApiScenarioBlobList, 100, subList -> {
subList.forEach(blobMapper::insertSelective);
});
insertApiScenarioBlobList.forEach(blobMapper::insertSelective);
}
if (CollectionUtils.isNotEmpty(insertApiScenarioStepList)) {
ApiScenarioStepMapper stepMapper = sqlSession.getMapper(ApiScenarioStepMapper.class);
SubListUtils.dealForSubList(insertApiScenarioStepList, 100, subList -> {
subList.forEach(stepMapper::insertSelective);
});
insertApiScenarioStepList.forEach(stepMapper::insertSelective);
}
if (CollectionUtils.isNotEmpty(insertApiScenarioStepBlobList)) {
ApiScenarioStepBlobMapper stepBlobMapper = sqlSession.getMapper(ApiScenarioStepBlobMapper.class);
SubListUtils.dealForSubList(insertApiScenarioStepBlobList, 100, subList -> {
subList.forEach(stepBlobMapper::insertSelective);
});
insertApiScenarioStepBlobList.forEach(stepBlobMapper::insertSelective);
}
if (CollectionUtils.isNotEmpty(insertApiFileResourceList)) {
SubListUtils.dealForSubList(insertApiFileResourceList, 100, subList -> {
apiFileResourceService.batchInsert(subList);
});
apiFileResourceService.batchInsert(insertApiFileResourceList);
}
sqlSession.flushStatements();
if (sqlSessionFactory != null) {
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
}
response.setSuccess(insertApiScenarioList.size());
return response;
}

View File

@ -8,12 +8,12 @@ import java.util.function.Function;
//场景批量操作工具类
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) {
ApiScenarioBatchOperationResponse response = new ApiScenarioBatchOperationResponse();
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);
response.merge(
subFunc.apply(subList));

View File

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