fix(接口测试): 为性能测试提供获取接口用例jmx的接口

为性能测试提供获取接口用例jmx的接口
This commit is contained in:
song-tianyang 2022-10-14 15:49:35 +08:00 committed by 刘瑞斌
parent 71916312a3
commit e4be8748a3
3 changed files with 80 additions and 3 deletions

View File

@ -0,0 +1,13 @@
package io.metersphere.api.dto;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
@Getter
@Setter
public class ApiCaseExportJmxRequest {
private List<String> caseIds;
private String envId;
}

View File

@ -6,10 +6,8 @@ import io.metersphere.api.dto.*;
import io.metersphere.api.dto.definition.*;
import io.metersphere.api.exec.api.ApiCaseExecuteService;
import io.metersphere.api.exec.api.ApiExecuteService;
import io.metersphere.base.domain.ApiScenario;
import io.metersphere.service.definition.ApiDefinitionExecResultService;
import io.metersphere.service.definition.ApiTestCaseService;
import io.metersphere.base.domain.ApiDefinitionExecResultExpand;
import io.metersphere.base.domain.ApiScenario;
import io.metersphere.base.domain.ApiTestCase;
import io.metersphere.base.domain.ApiTestEnvironment;
import io.metersphere.commons.constants.NoticeConstants;
@ -22,6 +20,8 @@ import io.metersphere.dto.MsExecResponseDTO;
import io.metersphere.log.annotation.MsAuditLog;
import io.metersphere.notice.annotation.SendNotice;
import io.metersphere.request.ResetOrderRequest;
import io.metersphere.service.definition.ApiDefinitionExecResultService;
import io.metersphere.service.definition.ApiTestCaseService;
import io.metersphere.service.scenario.ApiScenarioService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@ -254,4 +254,15 @@ public class ApiTestCaseController {
public List<ApiScenario> getScenarioCaseByIds(@RequestBody List<String> ids) {
return apiScenarioService.getScenarioCaseByIds(ids);
}
/**
* 性能测试调用同步接口用例的jmx
*
* @param request
* @return
*/
@PostMapping("/export/test/case/jmx")
public List<JmxInfoDTO> getScenarioCaseByIds(@RequestBody ApiCaseExportJmxRequest request) {
return apiTestCaseService.exportJmx(request.getCaseIds(), request.getEnvId());
}
}

View File

@ -9,6 +9,8 @@ import io.metersphere.api.dto.datacount.ApiDataCountResult;
import io.metersphere.api.dto.datacount.response.ExecuteResultCountDTO;
import io.metersphere.api.dto.definition.*;
import io.metersphere.api.dto.definition.request.ElementUtil;
import io.metersphere.api.dto.definition.request.MsTestPlan;
import io.metersphere.api.dto.definition.request.MsThreadGroup;
import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.*;
@ -1164,4 +1166,55 @@ public class ApiTestCaseService {
ServiceUtils.buildVersionInfo(apiScenarioDTOS);
return apiScenarioDTOS;
}
public List<JmxInfoDTO> exportJmx(List<String> caseIds, String envId) {
ApiTestCaseExample example = new ApiTestCaseExample();
example.createCriteria().andIdIn(caseIds);
List<ApiTestCaseWithBLOBs> apiTestCases = apiTestCaseMapper.selectByExampleWithBLOBs(example);
List<JmxInfoDTO> list = new ArrayList<>();
apiTestCases.forEach(item -> {
list.add(parse2Jmx(item, envId));
});
return list;
}
private JmxInfoDTO parse2Jmx(ApiTestCaseWithBLOBs apiTestCase, String envId) {
String request = apiTestCase.getRequest();
MsHTTPSamplerProxy msHTTPSamplerProxy = JSON.parseObject(request, MsHTTPSamplerProxy.class);
msHTTPSamplerProxy.setName(apiTestCase.getId());
msHTTPSamplerProxy.setUseEnvironment(envId);
LinkedList<MsTestElement> hashTree = new LinkedList<>();
hashTree.add(msHTTPSamplerProxy);
MsThreadGroup msThreadGroup = new MsThreadGroup();
msThreadGroup.setHashTree(hashTree);
msThreadGroup.setName("ThreadGroup");
msThreadGroup.setLabel("ThreadGroup");
msThreadGroup.setId(UUID.randomUUID().toString());
LinkedList<MsTestElement> planHashTree = new LinkedList<>();
planHashTree.add(msThreadGroup);
MsTestPlan msTestPlan = new MsTestPlan();
msTestPlan.setHashTree(planHashTree);
msTestPlan.setId(UUID.randomUUID().toString());
msTestPlan.setName("TestPlan");
msTestPlan.setLabel("TestPlan");
HashMap<String, String> envMap = new HashMap<>();
envMap.put(apiTestCase.getProjectId(), envId);
RunDefinitionRequest runRequest = new RunDefinitionRequest();
runRequest.setEnvironmentMap(envMap);
runRequest.setEnvironmentId(envId);
runRequest.setId(apiTestCase.getId());
runRequest.setTestElement(msTestPlan);
runRequest.setProjectId(apiTestCase.getProjectId());
JmxInfoDTO jmxInfoDTO = DataFormattingUtil.getJmxInfoDTO(runRequest, new ArrayList<>());
jmxInfoDTO.setId(apiTestCase.getId());
jmxInfoDTO.setVersion(apiTestCase.getVersion());
jmxInfoDTO.setName(apiTestCase.getName());
return jmxInfoDTO;
}
}