From 217800b24d8077f606b597de39806a72746fdcc2 Mon Sep 17 00:00:00 2001 From: "song.tianyang" Date: Fri, 19 Feb 2021 18:45:36 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E8=A1=A8key=E5=AD=97=E6=AE=B5=E9=95=BF?= =?UTF-8?q?=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改定时任务表key字段长度 --- .../resources/db/migration/V77__modify_schedule_test_id.sql | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 backend/src/main/resources/db/migration/V77__modify_schedule_test_id.sql diff --git a/backend/src/main/resources/db/migration/V77__modify_schedule_test_id.sql b/backend/src/main/resources/db/migration/V77__modify_schedule_test_id.sql new file mode 100644 index 0000000000..15a8e1591e --- /dev/null +++ b/backend/src/main/resources/db/migration/V77__modify_schedule_test_id.sql @@ -0,0 +1,2 @@ +ALTER TABLE schedule + MODIFY COLUMN `key` VARCHAR(255); From 567ae41aa3f4af60f5af97af04cae8d010fdcf6b Mon Sep 17 00:00:00 2001 From: "song.tianyang" Date: Sat, 20 Feb 2021 11:27:04 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E3=80=81=E6=8E=A5=E5=8F=A3=E8=87=AA=E5=8A=A8=E5=8C=96=E8=BD=AC?= =?UTF-8?q?=E6=80=A7=E8=83=BD=E6=B5=8B=E8=AF=95=E6=97=B6=E8=87=AA=E5=B8=A6?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E4=BC=9A=E5=B8=A6=E6=9D=A5jmeter=E6=8A=A5?= =?UTF-8?q?=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复接口测试、接口自动化转性能测试时自带函数会带来jmeter报错的问题 --- .../api/controller/APITestController.java | 89 ++++++++----------- .../api/service/APITestService.java | 62 +++++++++++++ .../api/service/ApiAutomationService.java | 19 ++-- 3 files changed, 110 insertions(+), 60 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/controller/APITestController.java b/backend/src/main/java/io/metersphere/api/controller/APITestController.java index 82e2ec81e8..61eb6e2fef 100644 --- a/backend/src/main/java/io/metersphere/api/controller/APITestController.java +++ b/backend/src/main/java/io/metersphere/api/controller/APITestController.java @@ -31,6 +31,7 @@ import io.metersphere.service.CheckPermissionService; import io.metersphere.service.FileService; import io.metersphere.service.ScheduleService; import io.metersphere.track.request.testplan.SaveTestPlanRequest; +import org.apache.commons.lang3.StringUtils; import org.apache.http.entity.ContentType; import org.apache.jorphan.collections.HashTree; import org.apache.shiro.authz.annotation.Logical; @@ -212,19 +213,19 @@ public class APITestController { //查询完成率、进行中、已完成 List countResultByStatelList = apiDefinitionService.countStateByProjectID(projectId); apiCountResult.countStatus(countResultByStatelList); - long allCount = apiCountResult.getFinishedCount()+apiCountResult.getRunningCount()+apiCountResult.getNotStartedCount(); + long allCount = apiCountResult.getFinishedCount() + apiCountResult.getRunningCount() + apiCountResult.getNotStartedCount(); - if(allCount!=0){ - float complateRageNumber =(float)apiCountResult.getFinishedCount()*100/allCount; + if (allCount != 0) { + float complateRageNumber = (float) apiCountResult.getFinishedCount() * 100 / allCount; DecimalFormat df = new DecimalFormat("0.0"); - apiCountResult.setCompletionRage(df.format(complateRageNumber)+"%"); + apiCountResult.setCompletionRage(df.format(complateRageNumber) + "%"); } - apiCountResult.setHttpCountStr("HTTP  

"+apiCountResult.getHttpApiDataCountNumber()); - apiCountResult.setRpcCountStr("RPC  

"+apiCountResult.getRpcApiDataCountNumber()); - apiCountResult.setTcpCountStr("TCP  

"+apiCountResult.getTcpApiDataCountNumber()); - apiCountResult.setSqlCountStr("SQL  

"+apiCountResult.getSqlApiDataCountNumber()); - return apiCountResult; + apiCountResult.setHttpCountStr("HTTP  

" + apiCountResult.getHttpApiDataCountNumber()); + apiCountResult.setRpcCountStr("RPC  

" + apiCountResult.getRpcApiDataCountNumber()); + apiCountResult.setTcpCountStr("TCP  

" + apiCountResult.getTcpApiDataCountNumber()); + apiCountResult.setSqlCountStr("SQL  

" + apiCountResult.getSqlApiDataCountNumber()); + return apiCountResult; } @GetMapping("/testCaseInfoCount/{projectId}") @@ -245,21 +246,21 @@ public class APITestController { //未覆盖 已覆盖: 统计当前接口下是否含有案例 List countResultByApiCoverageList = apiDefinitionService.countApiCoverageByProjectID(projectId); apiCountResult.countApiCoverage(countResultByApiCoverageList); - long allCount = apiCountResult.getCoverageCount()+apiCountResult.getUncoverageCount(); + long allCount = apiCountResult.getCoverageCount() + apiCountResult.getUncoverageCount(); - if(allCount!=0){ - float coverageRageNumber =(float)apiCountResult.getCoverageCount()*100/allCount; + if (allCount != 0) { + float coverageRageNumber = (float) apiCountResult.getCoverageCount() * 100 / allCount; DecimalFormat df = new DecimalFormat("0.0"); - apiCountResult.setCoverageRage(df.format(coverageRageNumber)+"%"); + apiCountResult.setCoverageRage(df.format(coverageRageNumber) + "%"); } - apiCountResult.setHttpCountStr("HTTP  

"+apiCountResult.getHttpApiDataCountNumber()); - apiCountResult.setRpcCountStr("RPC  

"+apiCountResult.getRpcApiDataCountNumber()); - apiCountResult.setTcpCountStr("TCP  

"+apiCountResult.getTcpApiDataCountNumber()); - apiCountResult.setSqlCountStr("SQL  

"+apiCountResult.getSqlApiDataCountNumber()); + apiCountResult.setHttpCountStr("HTTP  

" + apiCountResult.getHttpApiDataCountNumber()); + apiCountResult.setRpcCountStr("RPC  

" + apiCountResult.getRpcApiDataCountNumber()); + apiCountResult.setTcpCountStr("TCP  

" + apiCountResult.getTcpApiDataCountNumber()); + apiCountResult.setSqlCountStr("SQL  

" + apiCountResult.getSqlApiDataCountNumber()); - return apiCountResult; + return apiCountResult; } @GetMapping("/testSceneInfoCount/{projectId}") @@ -318,28 +319,28 @@ public class APITestController { apiCountResult.countScheduleExecute(allExecuteResult); long allCount = apiCountResult.getExecutedCount(); - if(allCount!=0){ - float coverageRageNumber =(float)apiCountResult.getSuccessCount()*100/allCount; + if (allCount != 0) { + float coverageRageNumber = (float) apiCountResult.getSuccessCount() * 100 / allCount; DecimalFormat df = new DecimalFormat("0.0"); - apiCountResult.setSuccessRage(df.format(coverageRageNumber)+"%"); + apiCountResult.setSuccessRage(df.format(coverageRageNumber) + "%"); } - return apiCountResult; + return apiCountResult; } @GetMapping("/faliureCaseAboutTestPlan/{projectId}/{limitNumber}") public List faliureCaseAboutTestPlan(@PathVariable String projectId, @PathVariable int limitNumber) { - List selectDataList = apiDefinitionExecResultService.findFaliureCaseInfoByProjectIDAndLimitNumberInSevenDays(projectId,limitNumber); + List selectDataList = apiDefinitionExecResultService.findFaliureCaseInfoByProjectIDAndLimitNumberInSevenDays(projectId, limitNumber); List returnList = new ArrayList<>(limitNumber); - for(int dataIndex = 0;dataIndex < limitNumber;dataIndex ++){ + for (int dataIndex = 0; dataIndex < limitNumber; dataIndex++) { ExecutedCaseInfoDTO dataDTO = new ExecutedCaseInfoDTO(); - dataDTO.setSortIndex(dataIndex+1); + dataDTO.setSortIndex(dataIndex + 1); - if(dataIndex thirdHashTreeElementList =innerHashTreeElement.elements("hashTree"); - for (Element element:thirdHashTreeElementList) { - List sampleProxyElementList = element.elements("HTTPSamplerProxy"); - for (Element itemElement: sampleProxyElementList) { - itemElement.attribute("testname").setText(testName); - } - } - - jmxString = root.asXML(); - }catch (Exception e){ - e.printStackTrace(); - } + //将jmx处理封装为通用方法 + jmxString = apiTestService.updateJmxString(jmxString,testName,true); JmxInfoDTO dto = new JmxInfoDTO(); - dto.setName(runRequest.getName()+".jmx"); + dto.setName(runRequest.getName() + ".jmx"); dto.setXml(jmxString); - return dto; + return dto; } } diff --git a/backend/src/main/java/io/metersphere/api/service/APITestService.java b/backend/src/main/java/io/metersphere/api/service/APITestService.java index 23ae3400e9..10cf3ab666 100644 --- a/backend/src/main/java/io/metersphere/api/service/APITestService.java +++ b/backend/src/main/java/io/metersphere/api/service/APITestService.java @@ -32,6 +32,9 @@ import io.metersphere.track.service.TestCaseService; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.constants.CommonConstants; import org.aspectj.util.FileUtil; +import org.dom4j.Document; +import org.dom4j.DocumentHelper; +import org.dom4j.Element; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; @@ -457,4 +460,63 @@ public class APITestService { copyBodyFiles(test.getId(), sourceId); }); } + + public String updateJmxString(String jmxString,String testName,boolean updateHTTPSamplerProxyName) { + try { + //将ThreadGroup的testname改为接口名称 + Document doc = DocumentHelper.parseText(jmxString);// 获取可续保保单列表报文模板 + Element root = doc.getRootElement(); + Element rootHashTreeElement = root.element("hashTree"); + Element innerHashTreeElement = rootHashTreeElement.elements("hashTree").get(0); + Element theadGroupElement = innerHashTreeElement.elements("ThreadGroup").get(0); + theadGroupElement.attribute("testname").setText(testName); + + List thirdHashTreeElementList = innerHashTreeElement.elements("hashTree"); + for (Element element : thirdHashTreeElementList) { + if(updateHTTPSamplerProxyName){ + List sampleProxyElementList = element.elements("HTTPSamplerProxy"); + for (Element itemElement : sampleProxyElementList) { + itemElement.attribute("testname").setText(testName); + } + } + //检查有没有自定义参数 + List scriptHashTreeElementList = element.elements("hashTree"); + for (Element scriptHashTreeElement : scriptHashTreeElementList) { + boolean isRemove = false; + List removeElement = new ArrayList<>(); + List scriptElementItemList = scriptHashTreeElement.elements(); + for (Element hashTreeItemElement : scriptElementItemList) { + String className = hashTreeItemElement.attributeValue("testclass"); + String qname = hashTreeItemElement.getQName().getName(); + + if (isRemove) { + if (org.apache.commons.lang3.StringUtils.equals("hashTree", qname)) { + removeElement.add(hashTreeItemElement); + } + } + isRemove = false; + if (org.apache.commons.lang3.StringUtils.equals(className, "JSR223PostProcessor")) { + List scriptElements = hashTreeItemElement.elements("stringProp"); + for (Element scriptElement : scriptElements) { + String scriptName = scriptElement.attributeValue("name"); + String contentValue = scriptElement.getStringValue(); + + if ("script".equals(scriptName) && contentValue.startsWith("io.metersphere.api.jmeter.JMeterVars.addVars")) { + isRemove = true; + removeElement.add(hashTreeItemElement); + } + } + } + } + for (Element itemElement : removeElement) { + scriptHashTreeElement.remove(itemElement); + } + } + } + jmxString = root.asXML(); + } catch (Exception e) { + e.printStackTrace(); + } + return jmxString; + } } diff --git a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java index 3de0a658c2..6882ef38a5 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -62,6 +62,8 @@ public class ApiAutomationService { @Resource private ApiScenarioMapper apiScenarioMapper; @Resource + private APITestService apiTestService; + @Resource private ExtApiScenarioMapper extApiScenarioMapper; @Resource private TestPlanApiScenarioMapper testPlanApiScenarioMapper; @@ -680,14 +682,17 @@ public class ApiAutomationService { HashTree jmeterHashTree = generateHashTree(apiScenarios, request, null); String jmx = testPlan.getJmx(jmeterHashTree); + + jmx = apiTestService.updateJmxString(jmx,testName,false); + //将ThreadGroup的testname改为接口名称 - Document doc = DocumentHelper.parseText(jmx);// 获取可续保保单列表报文模板 - Element root = doc.getRootElement(); - Element rootHashTreeElement = root.element("hashTree"); - Element innerHashTreeElement = rootHashTreeElement.elements("hashTree").get(0); - Element theadGroupElement = innerHashTreeElement.elements("ThreadGroup").get(0); - theadGroupElement.attribute("testname").setText(testName); - jmx = root.asXML(); +// Document doc = DocumentHelper.parseText(jmx);// 获取可续保保单列表报文模板 +// Element root = doc.getRootElement(); +// Element rootHashTreeElement = root.element("hashTree"); +// Element innerHashTreeElement = rootHashTreeElement.elements("hashTree").get(0); +// Element theadGroupElement = innerHashTreeElement.elements("ThreadGroup").get(0); +// theadGroupElement.attribute("testname").setText(testName); +// jmx = root.asXML(); String name = request.getName() + ".jmx";