feat(接口自动化): 接口自动化分多接点执行

This commit is contained in:
fit2-zhao 2021-04-16 18:05:06 +08:00 committed by fit2-zhao
parent d5c2624f14
commit 78f15b801c
7 changed files with 43 additions and 29 deletions

View File

@ -8,6 +8,8 @@ import lombok.Setter;
@Setter
public class RunRequest {
private String testId;
private String userId;
private boolean isDebug;
private String runMode;
private String jmx;
private RunModeConfig config;

View File

@ -11,6 +11,7 @@ import io.metersphere.commons.constants.ApiRunMode;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.SessionUtils;
import io.metersphere.commons.utils.UrlTestUtils;
import io.metersphere.config.JmeterProperties;
import io.metersphere.dto.BaseSystemConfigDTO;
@ -49,6 +50,7 @@ import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
@Service
public class JMeterService {
@ -226,13 +228,16 @@ public class JMeterService {
file = new File(path + "/");
}
FileSystemResource resource = new FileSystemResource(file);
ByteArrayResource byteArrayResource = new ByteArrayResource(this.fileToByte(file)) {
@Override
public String getFilename() throws IllegalStateException {
return resource.getFilename();
}
};
jarFiles.add(byteArrayResource);
byte[] fileByte = this.fileToByte(file);
if (fileByte != null) {
ByteArrayResource byteArrayResource = new ByteArrayResource(fileByte) {
@Override
public String getFilename() throws IllegalStateException {
return resource.getFilename();
}
};
jarFiles.add(byteArrayResource);
}
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
@ -251,20 +256,23 @@ public class JMeterService {
File file = new File(bodyFile.getName());
if (file != null && !file.exists()) {
FileSystemResource resource = new FileSystemResource(file);
ByteArrayResource byteArrayResource = new ByteArrayResource(this.fileToByte(file)) {
@Override
public String getFilename() throws IllegalStateException {
return resource.getFilename();
}
};
multipartFiles.add(byteArrayResource);
byte[] fileByte = this.fileToByte(file);
if (fileByte != null) {
ByteArrayResource byteArrayResource = new ByteArrayResource(fileByte) {
@Override
public String getFilename() throws IllegalStateException {
return resource.getFilename();
}
};
multipartFiles.add(byteArrayResource);
}
}
}
}
return multipartFiles;
}
public void runTest(String testId, HashTree hashTree, String runMode, RunModeConfig config) {
public void runTest(String testId, HashTree hashTree, String runMode, boolean isDebug, RunModeConfig config) {
// 获取JMX使用到的附件
List<Object> multipartFiles = getMultipartFiles(hashTree);
// 获取JAR
@ -296,8 +304,10 @@ public class JMeterService {
try {
RunRequest runRequest = new RunRequest();
runRequest.setTestId(testId);
runRequest.setDebug(isDebug);
runRequest.setRunMode(runMode);
runRequest.setConfig(config);
runRequest.setUserId(Objects.requireNonNull(SessionUtils.getUser()).getId());
runRequest.setJmx(new MsTestPlan().getJmx(hashTree));
MultiValueMap<String, Object> postParameters = new LinkedMultiValueMap<>();
postParameters.put("files", multipartFiles);
@ -310,11 +320,9 @@ public class JMeterService {
HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(postParameters, headers);
String result = restTemplate.postForObject(uri, request, String.class);
if (result == null) {
MSException.throwException(Translator.get("start_engine_fail"));
if (result == null || !StringUtils.equals("SUCCESS",result)) {
MSException.throwException("执行失败:"+ result);
}
} catch (MSException e) {
throw e;
} catch (Exception e) {
e.printStackTrace();
MSException.throwException("Please check node-controller status.");

View File

@ -146,10 +146,11 @@ public class MsKafkaListener {
}
}
if (report != null && StringUtils.equals(ReportTriggerMode.API.name(), report.getTriggerMode()) || StringUtils.equals(ReportTriggerMode.SCHEDULE.name(), report.getTriggerMode())) {
sendTask(report, reportUrl, testResult);
if (report != null) {
if (StringUtils.equals(ReportTriggerMode.API.name(), report.getTriggerMode()) || StringUtils.equals(ReportTriggerMode.SCHEDULE.name(), report.getTriggerMode())) {
sendTask(report, reportUrl, testResult);
}
}
}
private static void sendTask(ApiTestReport report, String reportUrl, TestResult testResult) {

View File

@ -7,7 +7,6 @@ import io.metersphere.base.domain.TestResourcePoolExample;
import io.metersphere.base.mapper.TestResourceMapper;
import io.metersphere.base.mapper.TestResourcePoolMapper;
import io.metersphere.commons.exception.MSException;
import io.metersphere.i18n.Translator;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
@ -25,7 +24,7 @@ public class ResourcePoolCalculation {
public TestResource getPool() {
// 获取可以执行的资源池
TestResourcePoolExample example = new TestResourcePoolExample();
example.createCriteria().andStatusEqualTo("VALID").andTypeEqualTo("NODE").andNameEqualTo("赵勇资源池");
example.createCriteria().andStatusEqualTo("VALID").andTypeEqualTo("NODE");
List<TestResourcePool> pools = testResourcePoolMapper.selectByExample(example);
// 暂时随机获取一个正常状态NODE
TestResource testResource = null;
@ -38,7 +37,7 @@ public class ResourcePoolCalculation {
testResource = testResources.get(index);
}
if (testResource == null) {
MSException.throwException(Translator.get("run_load_test_file_init_error"));
MSException.throwException("未获取到资源池,请检查配置【系统设置-系统-测试资源池】");
}
return testResource;
}

View File

@ -14,6 +14,8 @@ public class TestResult {
private String reportName;
private String userId;
private boolean isDebug;
private String runMode;

View File

@ -884,7 +884,8 @@ public class ApiAutomationService {
// 调用执行方法
List<String> reportIds = new LinkedList<>();
HashTree hashTree = generateHashTree(apiScenarios, request, reportIds);
jMeterService.runSerial(JSON.toJSONString(reportIds), hashTree, request.getReportId(), runMode, request.getConfig());
// jMeterService.runSerial(JSON.toJSONString(reportIds), hashTree, request.getReportId(), runMode, request.getConfig());
jMeterService.runTest(JSON.toJSONString(reportIds), hashTree, runMode, false, request.getConfig());
return request.getId();
}
@ -951,9 +952,9 @@ public class ApiAutomationService {
createScenarioReport(request.getId(), request.getScenarioId(), request.getScenarioName(), ReportTriggerMode.MANUAL.name(), request.getExecuteType(), request.getProjectId(),
SessionUtils.getUserId(), null);
// 调用执行方法
// jMeterService.runTest(request.getId(), hashTree, ApiRunMode.SCENARIO.name(), null);
jMeterService.runTest(request.getId(), hashTree, ApiRunMode.SCENARIO.name(), true, null);
// 调用执行方法
jMeterService.runDefinition(request.getId(), hashTree, request.getReportId(), ApiRunMode.SCENARIO.name());
// jMeterService.runDefinition(request.getId(), hashTree, request.getReportId(), ApiRunMode.SCENARIO.name());
return request.getId();
}

View File

@ -535,7 +535,8 @@ public class ApiDefinitionService {
runMode = ApiRunMode.API_PLAN.name();
}
// 调用执行方法
jMeterService.runDefinition(request.getId(), hashTree, request.getReportId(), runMode);
//jMeterService.runDefinition(request.getId(), hashTree, request.getReportId(), runMode);
jMeterService.runTest(request.getId(), hashTree, runMode, request.getReportId() != null, null);
return request.getId();
}