From 6f4d4e62e1ecc1a1b21f4154b677458a6bc5a50d Mon Sep 17 00:00:00 2001 From: AgAngle <1323481023@qq.com> Date: Mon, 25 Mar 2024 17:46:55 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E5=9C=BA=E6=99=AF=E8=AE=BE=E7=BD=AE=E7=9A=84=E6=AD=A5=E9=AA=A4?= =?UTF-8?q?=E7=AD=89=E5=BE=85=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/MsConstantTimerController.java | 4 +- .../MsConstantTimerControllerConverter.java | 39 ++++--------------- .../parser/jmeter/MsScenarioConverter.java | 28 ++++++++++++- .../TimeWaitingProcessorConverter.java | 33 ++++++++++++++-- .../ApiScenarioControllerTests.java | 2 +- .../api/controller/MsHTTPElementTest.java | 2 +- .../api/processor/TimeWaitingProcessor.java | 2 +- 7 files changed, 68 insertions(+), 42 deletions(-) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/controller/MsConstantTimerController.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/controller/MsConstantTimerController.java index a1578e6005..cf0ec0bc3a 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/controller/MsConstantTimerController.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/controller/MsConstantTimerController.java @@ -14,7 +14,5 @@ public class MsConstantTimerController extends AbstractMsTestElement { /** * 延迟时间 */ - private String delay; - - + private Long delay; } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsConstantTimerControllerConverter.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsConstantTimerControllerConverter.java index 7e1c952e17..8cd03fe72f 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsConstantTimerControllerConverter.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsConstantTimerControllerConverter.java @@ -1,51 +1,26 @@ package io.metersphere.api.parser.jmeter; import io.metersphere.api.dto.request.controller.MsConstantTimerController; -import io.metersphere.plugin.api.constants.ElementProperty; +import io.metersphere.api.parser.jmeter.processor.MsProcessorConverter; +import io.metersphere.api.parser.jmeter.processor.MsProcessorConverterFactory; import io.metersphere.plugin.api.dto.ParameterConfig; import io.metersphere.plugin.api.spi.AbstractJmeterElementConverter; +import io.metersphere.project.api.processor.TimeWaitingProcessor; +import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.LogUtils; import org.apache.commons.lang3.BooleanUtils; -import org.apache.jmeter.sampler.DebugSampler; -import org.apache.jmeter.save.SaveService; -import org.apache.jmeter.testelement.TestElement; -import org.apache.jmeter.timers.ConstantTimer; import org.apache.jorphan.collections.HashTree; public class MsConstantTimerControllerConverter extends AbstractJmeterElementConverter { - @Override public void toHashTree(HashTree tree, MsConstantTimerController element, ParameterConfig config) { if (BooleanUtils.isFalse(element.getEnable())) { LogUtils.info("MsConstantTimerController is disabled"); return; } - HashTree groupTree = tree.add(constantTimer(element)); - parseChild(groupTree, element, config); + MsProcessorConverter timeWaitingConverter = MsProcessorConverterFactory.getPostConverter(TimeWaitingProcessor.class); + TimeWaitingProcessor timeWaitingProcessor = BeanUtils.copyBean(new TimeWaitingProcessor(), element); + timeWaitingConverter.parse(tree, timeWaitingProcessor, config); } - - private DebugSampler constantTimer(MsConstantTimerController element) { - // 添加 debugSampler 步骤间生效 - DebugSampler debugSampler = new DebugSampler(); - debugSampler.setEnabled(true); - debugSampler.setName(ElementProperty.SCENARIO_CONSTANT_TIMER.name()); - debugSampler.setProperty(TestElement.TEST_CLASS, DebugSampler.class.getName()); - debugSampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI")); - - // 上面三行直接Set属性会导致DebugSampler构建时取不到值,可能是JMeter的Bug,需要SetProperty - debugSampler.setProperty("displayJMeterProperties", false); - debugSampler.setProperty("displayJMeterVariables", false); - debugSampler.setProperty("displaySystemProperties", false); - - ConstantTimer constantTimer = new ConstantTimer(); - constantTimer.setEnabled(element.getEnable()); - constantTimer.setName(element.getName() + " ms"); - constantTimer.setProperty(TestElement.TEST_CLASS, ConstantTimer.class.getName()); - constantTimer.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("ConstantTimerGui")); - constantTimer.setDelay(element.getDelay()); - debugSampler.addTestElement(constantTimer); - return debugSampler; - } - } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsScenarioConverter.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsScenarioConverter.java index 917d9f6f40..6566371768 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsScenarioConverter.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsScenarioConverter.java @@ -6,15 +6,18 @@ import io.metersphere.api.dto.ApiScenarioParamConfig; import io.metersphere.api.dto.request.MsScenario; import io.metersphere.api.dto.request.processors.MsProcessorConfig; import io.metersphere.api.dto.scenario.ScenarioConfig; +import io.metersphere.api.dto.scenario.ScenarioOtherConfig; import io.metersphere.api.dto.scenario.ScenarioStepConfig; import io.metersphere.api.dto.scenario.ScenarioVariable; import io.metersphere.api.parser.jmeter.processor.MsProcessorConverter; import io.metersphere.api.parser.jmeter.processor.MsProcessorConverterFactory; +import io.metersphere.api.parser.jmeter.processor.TimeWaitingProcessorConverter; import io.metersphere.api.parser.jmeter.processor.assertion.AssertionConverterFactory; import io.metersphere.plugin.api.dto.ParameterConfig; import io.metersphere.plugin.api.spi.AbstractJmeterElementConverter; import io.metersphere.project.api.assertion.MsAssertion; import io.metersphere.project.api.processor.MsProcessor; +import io.metersphere.project.api.processor.TimeWaitingProcessor; import io.metersphere.project.dto.environment.EnvironmentConfig; import io.metersphere.project.dto.environment.EnvironmentInfoDTO; import io.metersphere.project.dto.environment.processors.EnvProcessorConfig; @@ -27,6 +30,7 @@ import org.apache.jmeter.config.Arguments; import org.apache.jmeter.protocol.http.control.CookieManager; import org.apache.jmeter.save.SaveService; import org.apache.jmeter.testelement.TestElement; +import org.apache.jmeter.timers.ConstantTimer; import org.apache.jorphan.collections.HashTree; import java.util.List; @@ -58,6 +62,9 @@ public class MsScenarioConverter extends AbstractJmeterElementConverter { @Override public void parse(HashTree hashTree, TimeWaitingProcessor processor, ParameterConfig config) { + hashTree.add(getConstantTimerElement(processor)); + } + + private DebugSampler getConstantTimerElement(TimeWaitingProcessor processor) { + // 添加 debugSampler 步骤间生效 + DebugSampler debugSampler = new DebugSampler(); + debugSampler.setEnabled(true); + debugSampler.setName(ElementProperty.SCENARIO_CONSTANT_TIMER.name()); + debugSampler.setProperty(TestElement.TEST_CLASS, DebugSampler.class.getName()); + debugSampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass(JmeterAlias.TEST_BEAN_GUI)); + + // 下面三行直接Set属性会导致DebugSampler构建时取不到值,可能是JMeter的Bug,需要SetProperty + debugSampler.setProperty("displayJMeterProperties", false); + debugSampler.setProperty("displayJMeterVariables", false); + debugSampler.setProperty("displaySystemProperties", false); + + ConstantTimer constantTimer = getConstantTimer(processor); + + debugSampler.addTestElement(constantTimer); + return debugSampler; + } + + public static ConstantTimer getConstantTimer(TimeWaitingProcessor processor) { ConstantTimer constantTimer = new ConstantTimer(); constantTimer.setEnabled(processor.getEnable()); - constantTimer.setName(processor.getDelay() + " ms"); + constantTimer.setName(StringUtils.isBlank(processor.getName()) ? (processor.getDelay() + " ms") : processor.getName()); constantTimer.setProperty(TestElement.TEST_CLASS, ConstantTimer.class.getName()); constantTimer.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass(CONSTANT_TIMER_GUI)); constantTimer.setDelay(processor.getDelay().toString()); - hashTree.add(constantTimer); + return constantTimer; } } diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiScenarioControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiScenarioControllerTests.java index 6fa3819bef..8c58df00a9 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiScenarioControllerTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiScenarioControllerTests.java @@ -958,7 +958,7 @@ public class ApiScenarioControllerTests extends BaseTest { MsConstantTimerController constantTimerController = new MsConstantTimerController(); constantTimerController.setName(StringUtils.isNotBlank(name) ? name : "等待控制器"); constantTimerController.setEnable(enable); - constantTimerController.setDelay("1000"); + constantTimerController.setDelay(1000L); return constantTimerController; } diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/MsHTTPElementTest.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/MsHTTPElementTest.java index e6bb08fd29..ec2f39da65 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/MsHTTPElementTest.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/MsHTTPElementTest.java @@ -158,7 +158,7 @@ public class MsHTTPElementTest { processors.add(sqlProcessor1); TimeWaitingProcessor timeWaitingProcessor = new TimeWaitingProcessor(); - timeWaitingProcessor.setDelay(1000); + timeWaitingProcessor.setDelay(1000L); timeWaitingProcessor.setEnable(true); processors.add(timeWaitingProcessor); diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/api/processor/TimeWaitingProcessor.java b/backend/services/project-management/src/main/java/io/metersphere/project/api/processor/TimeWaitingProcessor.java index 75b4dfc305..3270c5be9a 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/api/processor/TimeWaitingProcessor.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/api/processor/TimeWaitingProcessor.java @@ -15,5 +15,5 @@ public class TimeWaitingProcessor extends MsProcessor { * 等待时间 * 单位:毫秒 */ - private Integer delay; + private Long delay; }