refactor(接口测试): 针对资源池调试匿名用户对附件读取方式优化
This commit is contained in:
parent
da92c1416e
commit
e1c67d1908
|
@ -1,12 +1,9 @@
|
|||
package io.metersphere.api.dto;
|
||||
|
||||
import io.metersphere.api.dto.scenario.request.BodyFile;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class BodyFileRequest {
|
||||
private String reportId;
|
||||
private List<BodyFile> bodyFiles;
|
||||
private String testId;
|
||||
}
|
||||
|
|
|
@ -2,12 +2,12 @@ package io.metersphere.api.jmeter;
|
|||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import io.metersphere.api.dto.definition.request.ElementUtil;
|
||||
import io.metersphere.api.dto.definition.request.MsTestPlan;
|
||||
import io.metersphere.api.exec.ApiPoolDebugService;
|
||||
import io.metersphere.api.exec.queue.ExecThreadPoolExecutor;
|
||||
import io.metersphere.api.exec.utils.GenerateHashTreeUtil;
|
||||
import io.metersphere.api.jmeter.utils.ServerConfig;
|
||||
import io.metersphere.api.jmeter.utils.SmoothWeighted;
|
||||
import io.metersphere.api.service.RedisTemplateService;
|
||||
import io.metersphere.api.service.RemakeReportService;
|
||||
import io.metersphere.commons.constants.ApiRunMode;
|
||||
import io.metersphere.commons.constants.ExtendedParameter;
|
||||
|
@ -54,11 +54,13 @@ public class JMeterService {
|
|||
@Resource
|
||||
private RestTemplate restTemplate;
|
||||
@Resource
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
private RedisTemplateService redisTemplateService;
|
||||
@Resource
|
||||
private ExecThreadPoolExecutor execThreadPoolExecutor;
|
||||
@Resource
|
||||
private RemakeReportService remakeReportService;
|
||||
@Resource
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
|
@ -148,14 +150,12 @@ public class JMeterService {
|
|||
if (request.getPool().isK8s()) {
|
||||
try {
|
||||
// 缓存调试脚本
|
||||
if (request.getHashTree() != null) {
|
||||
String key = StringUtils.join(request.getReportId(), "-", request.getTestId());
|
||||
redisTemplate.opsForValue().set(key, new MsTestPlan().getJmx(request.getHashTree()));
|
||||
}
|
||||
redisTemplateService.initDebug(request);
|
||||
LoggerUtil.info("开始发送请求[ " + request.getTestId() + " ] 到K8S节点执行", request.getReportId());
|
||||
final Engine engine = EngineFactory.createApiEngine(request);
|
||||
engine.start();
|
||||
} catch (Exception e) {
|
||||
redisTemplateService.deleteDebug(request);
|
||||
remakeReportService.testEnded(request, e.getMessage());
|
||||
LoggerUtil.error("调用K8S执行请求[ " + request.getTestId() + " ]失败:", request.getReportId(), e);
|
||||
}
|
||||
|
|
|
@ -47,7 +47,8 @@ public class ApiJmeterFileService {
|
|||
private ApiScenarioMapper apiScenarioMapper;
|
||||
@Resource
|
||||
private ApiExecutionQueueDetailMapper executionQueueDetailMapper;
|
||||
|
||||
@Resource
|
||||
private RedisTemplateService redisTemplateService;
|
||||
@Resource
|
||||
private EnvironmentGroupProjectService environmentGroupProjectService;
|
||||
|
||||
|
@ -203,33 +204,28 @@ public class ApiJmeterFileService {
|
|||
List<BodyFile> files = new LinkedList<>();
|
||||
FileUtils.getFiles(hashTree, files);
|
||||
if (CollectionUtils.isNotEmpty(files)) {
|
||||
for (BodyFile bodyFile : files) {
|
||||
File file = new File(bodyFile.getName());
|
||||
if (file != null && file.exists()) {
|
||||
byte[] fileByte = FileUtils.fileToByte(file);
|
||||
if (fileByte != null) {
|
||||
multipartFiles.put(file.getAbsolutePath(), fileByte);
|
||||
}
|
||||
}
|
||||
}
|
||||
getBodyFIleByte(multipartFiles, files);
|
||||
}
|
||||
return multipartFiles;
|
||||
}
|
||||
|
||||
public byte[] zipFilesToByteArray(BodyFileRequest request) {
|
||||
Map<String, byte[]> files = new LinkedHashMap<>();
|
||||
if (CollectionUtils.isNotEmpty(request.getBodyFiles())) {
|
||||
for (BodyFile bodyFile : request.getBodyFiles()) {
|
||||
File file = new File(bodyFile.getName());
|
||||
if (file != null && file.exists()) {
|
||||
byte[] fileByte = FileUtils.fileToByte(file);
|
||||
if (fileByte != null) {
|
||||
files.put(file.getAbsolutePath(), fileByte);
|
||||
}
|
||||
List<BodyFile> bodyFiles = redisTemplateService.getDebugFiles(request);
|
||||
getBodyFIleByte(files, bodyFiles);
|
||||
return listBytesToZip(files);
|
||||
}
|
||||
|
||||
private void getBodyFIleByte(Map<String, byte[]> files, List<BodyFile> bodyFiles) {
|
||||
for (BodyFile bodyFile : bodyFiles) {
|
||||
File file = new File(bodyFile.getName());
|
||||
if (file != null && file.exists()) {
|
||||
byte[] fileByte = FileUtils.fileToByte(file);
|
||||
if (fileByte != null) {
|
||||
files.put(file.getAbsolutePath(), fileByte);
|
||||
}
|
||||
}
|
||||
}
|
||||
return listBytesToZip(files);
|
||||
}
|
||||
|
||||
private byte[] zipFilesToByteArray(String testId, HashTree hashTree) {
|
||||
|
@ -269,7 +265,7 @@ public class ApiJmeterFileService {
|
|||
zos.close();
|
||||
return baos.toByteArray();
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
return new byte[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,21 @@
|
|||
package io.metersphere.api.service;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import io.metersphere.api.dto.BodyFileRequest;
|
||||
import io.metersphere.api.dto.definition.request.MsTestPlan;
|
||||
import io.metersphere.api.dto.scenario.request.BodyFile;
|
||||
import io.metersphere.commons.utils.FileUtils;
|
||||
import io.metersphere.commons.utils.LogUtil;
|
||||
import io.metersphere.dto.JmeterRunRequestDTO;
|
||||
import io.metersphere.utils.LoggerUtil;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Service
|
||||
|
@ -39,4 +50,43 @@ public class RedisTemplateService {
|
|||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void initDebug(JmeterRunRequestDTO request) {
|
||||
if (request.getHashTree() != null) {
|
||||
String key = StringUtils.join(request.getReportId(), "-", request.getTestId());
|
||||
redisTemplate.opsForValue().set(key, new MsTestPlan().getJmx(request.getHashTree()));
|
||||
|
||||
List<BodyFile> files = new ArrayList<>();
|
||||
FileUtils.getFiles(request.getHashTree(), files);
|
||||
String file = StringUtils.join(key, "-FILE");
|
||||
redisTemplate.opsForValue().set(file, JSON.toJSONString(files));
|
||||
}
|
||||
}
|
||||
|
||||
public List<BodyFile> getDebugFiles(BodyFileRequest request) {
|
||||
try {
|
||||
if (request != null) {
|
||||
String key = StringUtils.join(request.getReportId(), "-", request.getTestId(), "-FILE");
|
||||
Object script = redisTemplate.opsForValue().get(key);
|
||||
if (ObjectUtils.isNotEmpty(script)) {
|
||||
return JSON.parseArray(script.toString(), BodyFile.class);
|
||||
}
|
||||
redisTemplate.delete(key);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LoggerUtil.error("获取附件异常", request.getReportId(), e);
|
||||
}
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
public void deleteDebug(JmeterRunRequestDTO request) {
|
||||
try {
|
||||
String key = StringUtils.join(request.getReportId(), "-", request.getTestId());
|
||||
String file = StringUtils.join(key, "-FILE");
|
||||
redisTemplate.delete(file);
|
||||
redisTemplate.delete(key);
|
||||
} catch (Exception e) {
|
||||
LogUtil.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue