refactor: 接口用例、用例列表、场景用例列表增加创建性能测试的功能

接口用例、用例列表、场景用例列表增加创建性能测试的功能  原来是跳转之后后台直接创建,现在跳转到创建页面
This commit is contained in:
song.tianyang 2021-01-04 12:39:19 +08:00
parent 8e5105d579
commit b0f9723dd4
7 changed files with 65 additions and 129 deletions

View File

@ -370,16 +370,14 @@ public class APITestController {
apiAutomationService.updateSchedule(schedule);
}
@PostMapping(value = "/genPerformanceTest", consumes = {"multipart/form-data"})
public String genPerformanceTest(@RequestPart("request") RunDefinitionRequest runRequest, @RequestPart(value = "files") List<MultipartFile> bodyFiles) {
SaveTestPlanRequest request = new SaveTestPlanRequest();
request.setName(runRequest.getName());
request.setProjectId(runRequest.getProjectId());
request.setAdvancedConfiguration("{\"timeout\":2000,\"responseTimeout\":0,\"statusCode\":[],\"params\":[],\"domains\":[]}");
request.setLoadConfiguration("[]");
@PostMapping(value = "/genPerformanceTestXml", consumes = {"multipart/form-data"})
public JmxInfoDTO genPerformanceTest(@RequestPart("request") RunDefinitionRequest runRequest, @RequestPart(value = "files") List<MultipartFile> bodyFiles) {
HashTree hashTree = runRequest.getTestElement().generateHashTree();
String jmxString = runRequest.getTestElement().getJmx(hashTree);
String testID = performanceTestService.save(request, jmxString.getBytes());
return testID;
JmxInfoDTO dto = new JmxInfoDTO();
dto.setName(runRequest.getName()+".JMX");
dto.setXml(jmxString);
return dto;
}
}

View File

@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.metersphere.api.dto.JmxInfoDTO;
import io.metersphere.api.dto.automation.*;
import io.metersphere.api.dto.definition.RunDefinitionRequest;
import io.metersphere.api.dto.definition.request.*;
@ -144,11 +145,10 @@ public class ApiAutomationController {
apiAutomationService.createSchedule(request);
}
//一键创建性能测试
@PostMapping(value = "/genPerformanceTest")
public String genPerformanceTest(@RequestBody RunScenarioRequest runRequest) {
@PostMapping(value = "/genPerformanceTestJmx")
public JmxInfoDTO genPerformanceTestJmx(@RequestBody RunScenarioRequest runRequest) {
runRequest.setExecuteType(ExecuteType.Completed.name());
return apiAutomationService.genPerformanceTest(runRequest);
return apiAutomationService.genPerformanceTestJmx(runRequest);
}
}

View File

@ -6,6 +6,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.metersphere.api.dto.DeleteAPIReportRequest;
import io.metersphere.api.dto.JmxInfoDTO;
import io.metersphere.api.dto.automation.*;
import io.metersphere.api.dto.datacount.ApiDataCountResult;
import io.metersphere.api.dto.definition.RunDefinitionRequest;
@ -546,80 +547,7 @@ public class ApiAutomationService {
request, ApiScenarioTestJob.getJobKey(request.getResourceId()), ApiScenarioTestJob.getTriggerKey(request.getResourceId()), ApiScenarioTestJob.class);
}
public String genPerformanceTest1(RunScenarioRequest runRequest) {
SaveTestPlanRequest request = new SaveTestPlanRequest();
request.setName(runRequest.getName());
request.setProjectId(runRequest.getProjectId());
request.setAdvancedConfiguration("{\"timeout\":2000,\"responseTimeout\":0,\"statusCode\":[],\"params\":[],\"domains\":[]}");
request.setLoadConfiguration("[]");
List<String> ids = runRequest.getScenarioIds();
if (runRequest.isSelectAllDate()) {
ids = this.getAllScenarioIdsByFontedSelect(
runRequest.getModuleIds(), request.getName(), request.getProjectId(), runRequest.getFilters(), runRequest.getUnSelectIds());
}
List<ApiScenarioWithBLOBs> apiScenarios = extApiScenarioMapper.selectIds(ids);
MsTestPlan testPlan = new MsTestPlan();
testPlan.setHashTree(new LinkedList<>());
HashTree jmeterHashTree = new ListedHashTree();
try {
boolean isFirst = true;
for (ApiScenarioWithBLOBs item : apiScenarios) {
if (item.getStepTotal() == 0) {
MSException.throwException(item.getName() + "" + Translator.get("automation_exec_info"));
break;
}
MsThreadGroup group = new MsThreadGroup();
group.setLabel(item.getName());
group.setName(UUID.randomUUID().toString());
// 批量执行的结果直接存储为报告
if (isFirst) {
group.setName(request.getId());
isFirst = false;
}
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JSONObject element = JSON.parseObject(item.getScenarioDefinition());
MsScenario scenario = JSONObject.parseObject(item.getScenarioDefinition(), MsScenario.class);
// 多态JSON普通转换会丢失内容需要通过 ObjectMapper 获取
if (element != null && StringUtils.isNotEmpty(element.getString("hashTree"))) {
LinkedList<MsTestElement> elements = mapper.readValue(element.getString("hashTree"),
new TypeReference<LinkedList<MsTestElement>>() {
});
scenario.setHashTree(elements);
}
if (StringUtils.isNotEmpty(element.getString("variables"))) {
LinkedList<KeyValue> variables = mapper.readValue(element.getString("variables"),
new TypeReference<LinkedList<KeyValue>>() {
});
scenario.setVariables(variables);
}
group.setEnableCookieShare(scenario.isEnableCookieShare());
LinkedList<MsTestElement> scenarios = new LinkedList<>();
scenarios.add(scenario);
// 创建场景报告
// createScenarioReport(group.getName(), item.getId(), item.getName(), runRequest.getTriggerMode() == null ? ReportTriggerMode.MANUAL.name() : runRequest.getTriggerMode(),
// runRequest.getExecuteType(), item.getProjectId(), runRequest.getReportUserID());
group.setHashTree(scenarios);
testPlan.getHashTree().add(group);
}
} catch (Exception ex) {
MSException.throwException(ex.getMessage());
}
testPlan.toHashTree(jmeterHashTree, testPlan.getHashTree(), new ParameterConfig());
String jmxString = testPlan.getJmx(jmeterHashTree);
String testID = performanceTestService.save(request, jmxString.getBytes());
return testID;
}
public String genPerformanceTest(RunScenarioRequest request) {
public JmxInfoDTO genPerformanceTestJmx(RunScenarioRequest request) {
List<ApiScenarioWithBLOBs> apiScenarios = null;
List<String> ids = request.getScenarioIds();
if (request.isSelectAllDate()) {
@ -676,13 +604,11 @@ public class ApiAutomationService {
testPlan.toHashTree(jmeterHashTree, testPlan.getHashTree(), new ParameterConfig());
String jmx = testPlan.getJmx(jmeterHashTree);
String name = request.getName() + ".JMX";
SaveTestPlanRequest saveRequest = new SaveTestPlanRequest();
saveRequest.setName(request.getName());
saveRequest.setProjectId(request.getProjectId());
saveRequest.setAdvancedConfiguration("{\"timeout\":2000,\"responseTimeout\":0,\"statusCode\":[],\"params\":[],\"domains\":[]}");
saveRequest.setLoadConfiguration("[]");
String testID = performanceTestService.save(saveRequest, jmx.getBytes());
return testID;
JmxInfoDTO dto = new JmxInfoDTO();
dto.setName(name);
dto.setXml(jmx);
return dto;
}
}

View File

@ -139,20 +139,6 @@ public class PerformanceTestService {
return loadTest.getId();
}
public String save(SaveTestPlanRequest request, byte[] file) {
if (file == null) {
throw new IllegalArgumentException(Translator.get("file_cannot_be_null"));
}
checkQuota(request, true);
final LoadTestWithBLOBs loadTest = saveLoadTest(request);
final FileMetadata fileMetadata = fileService.saveFile(file, request.getName() + ".JMX", new Long(file.length));
LoadTestFile loadTestFile = new LoadTestFile();
loadTestFile.setTestId(loadTest.getId());
loadTestFile.setFileId(fileMetadata.getId());
loadTestFileMapper.insert(loadTestFile);
return loadTest.getId();
}
private LoadTestWithBLOBs saveLoadTest(SaveTestPlanRequest request) {
LoadTestExample example = new LoadTestExample();

View File

@ -40,7 +40,7 @@
},
createPerformance(row) {
this.infoDb = false;
let url = "/api/automation/genPerformanceTest";
let url = "/api/automation/genPerformanceTestJmx";
let run = {};
let scenarioIds = [];
scenarioIds.push(row.id);
@ -49,12 +49,16 @@
run.id = getUUID();
run.name = row.name;
this.$post(url, run, response => {
let performanceId = response.data;
if(performanceId!=null){
this.$router.push({
path: "/performance/test/edit/"+performanceId,
})
}
let jmxObj = {};
jmxObj.name = response.data.name;
jmxObj.xml = response.data.xml;
this.$store.commit('setTest', {
name: row.name,
jmx: jmxObj
})
this.$router.push({
path: "/performance/test/create"
})
});
},
}

View File

@ -484,14 +484,25 @@ export default {
let bodyFiles = getBodyUploadFiles(reqObj, runData);
reqObj.reportId = "run";
let url = "/api/genPerformanceTest";
let url = "/api/genPerformanceTestXml";
this.$fileUpload(url, null, bodyFiles, reqObj, response => {
let performanceId = response.data;
if(performanceId!=null){
this.$router.push({
path: "/performance/test/edit/"+performanceId,
})
}
let jmxObj = {};
jmxObj.name = response.data.name;
jmxObj.xml = response.data.xml;
this.$store.commit('setTest', {
name: row.name,
jmx: jmxObj
})
this.$router.push({
path: "/performance/test/create"
})
// let performanceId = response.data;
// if(performanceId!=null){
// this.$router.push({
// path: "/performance/test/edit/"+performanceId,
// })
// }
}, erro => {
this.$emit('runRefresh', {});
});

View File

@ -89,15 +89,26 @@
let bodyFiles = getBodyUploadFiles(reqObj, this.runData);
reqObj.reportId = "run";
let url = "/api/genPerformanceTest";
// let url = "/api/genPerformanceTest";
let url = "/api/genPerformanceTestXml";
this.$fileUpload(url, null, bodyFiles, reqObj, response => {
let performanceId = response.data;
if(performanceId!=null){
this.$router.push({
path: "/performance/test/edit/"+performanceId,
})
}
let jmxObj = {};
jmxObj.name = response.data.name;
jmxObj.xml = response.data.xml;
this.$store.commit('setTest', {
name: row.name,
jmx: jmxObj
})
this.$router.push({
path: "/performance/test/create"
})
// let performanceId = response.data;
// if(performanceId!=null){
// this.$router.push({
// path: "/performance/test/edit/"+performanceId,
// })
// }
}, erro => {
this.$emit('runRefresh', {});
});