diff --git a/backend/framework/plugin/plugin-api-sdk/src/main/java/io/metersphere/plugin/api/constants/ElementProperty.java b/backend/framework/plugin/plugin-api-sdk/src/main/java/io/metersphere/plugin/api/constants/ElementProperty.java index f9f8025f47..d4b9ef2488 100644 --- a/backend/framework/plugin/plugin-api-sdk/src/main/java/io/metersphere/plugin/api/constants/ElementProperty.java +++ b/backend/framework/plugin/plugin-api-sdk/src/main/java/io/metersphere/plugin/api/constants/ElementProperty.java @@ -1,5 +1,5 @@ package io.metersphere.plugin.api.constants; public enum ElementProperty { - MS_RESOURCE_ID, MS_STEP_ID, MS_FAKE_ERROR, MS_REPORT_ID, PROJECT_ID + MS_RESOURCE_ID, MS_STEP_ID, MS_FAKE_ERROR, MS_REPORT_ID, PROJECT_ID,SCENARIO_CONSTANT_TIMER } 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 new file mode 100644 index 0000000000..a1578e6005 --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/controller/MsConstantTimerController.java @@ -0,0 +1,20 @@ +package io.metersphere.api.dto.request.controller; + +import io.metersphere.plugin.api.spi.AbstractMsTestElement; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@Data +@EqualsAndHashCode(callSuper = true) +public class MsConstantTimerController extends AbstractMsTestElement { + /** + * id + */ + private String id; + /** + * 延迟时间 + */ + private String 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 new file mode 100644 index 0000000000..7e1c952e17 --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsConstantTimerControllerConverter.java @@ -0,0 +1,51 @@ +package io.metersphere.api.parser.jmeter; + +import io.metersphere.api.dto.request.controller.MsConstantTimerController; +import io.metersphere.plugin.api.constants.ElementProperty; +import io.metersphere.plugin.api.dto.ParameterConfig; +import io.metersphere.plugin.api.spi.AbstractJmeterElementConverter; +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); + } + + 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/utils/ApiDataUtils.java b/backend/services/api-test/src/main/java/io/metersphere/api/utils/ApiDataUtils.java index 47e59f1455..d2d24cb0c6 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/utils/ApiDataUtils.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/utils/ApiDataUtils.java @@ -11,6 +11,7 @@ import com.fasterxml.jackson.databind.json.JsonMapper; import com.fasterxml.jackson.databind.jsontype.NamedType; import com.fasterxml.jackson.databind.type.CollectionType; import io.metersphere.api.dto.request.MsCommonElement; +import io.metersphere.api.dto.request.controller.MsConstantTimerController; import io.metersphere.api.dto.request.controller.MsIfController; import io.metersphere.api.dto.request.controller.MsLoopController; import io.metersphere.api.dto.request.controller.MsOnceOnlyController; @@ -40,7 +41,7 @@ public class ApiDataUtils { namedTypes.add(new NamedType(MsIfController.class, MsIfController.class.getSimpleName())); namedTypes.add(new NamedType(MsLoopController.class, MsLoopController.class.getSimpleName())); namedTypes.add(new NamedType(MsOnceOnlyController.class, MsOnceOnlyController.class.getSimpleName())); - + namedTypes.add(new NamedType(MsConstantTimerController.class, MsConstantTimerController.class.getSimpleName())); setObjectMapper(objectMapper); namedTypes.forEach(objectMapper::registerSubtypes); } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/utils/JmeterElementConverterRegister.java b/backend/services/api-test/src/main/java/io/metersphere/api/utils/JmeterElementConverterRegister.java index b1f2db3db3..0792a4b1ac 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/utils/JmeterElementConverterRegister.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/utils/JmeterElementConverterRegister.java @@ -32,6 +32,7 @@ public class JmeterElementConverterRegister { register(MsIfControllerConverter.class); register(MsLoopControllerConverter.class); register(MsOnceOnlyControllerConverter.class); + register(MsConstantTimerControllerConverter.class); } /** 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 2d7b0c9650..bf9d5060d7 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 @@ -910,6 +910,23 @@ public class ApiScenarioControllerTests extends BaseTest { stepDetails.put(loopController.getId(), JSON.parseObject(ApiDataUtils.toJSONString(foreach))); steps.add(loopController); + MsConstantTimerController constantTimerController = constantTimerController("test-constant-timer", true); + ApiScenarioStepRequest constantTimer = BeanUtils.copyBean(new ApiScenarioStepRequest(), constantTimerController); + constantTimer.setRefType(ApiScenarioStepRefType.DIRECT.name()); + constantTimer.setProjectId(DEFAULT_PROJECT_ID); + constantTimer.setStepType(ApiScenarioStepType.CONSTANT_TIMER.name()); + constantTimer.setId(IDGenerator.nextStr()); + stepDetails.put(constantTimer.getId(), JSON.parseObject(ApiDataUtils.toJSONString(constantTimerController))); + steps.add(constantTimer); + constantTimerController = constantTimerController("test-constant-timer", false); + constantTimer = BeanUtils.copyBean(new ApiScenarioStepRequest(), constantTimerController); + constantTimer.setRefType(ApiScenarioStepRefType.DIRECT.name()); + constantTimer.setProjectId(DEFAULT_PROJECT_ID); + constantTimer.setStepType(ApiScenarioStepType.CONSTANT_TIMER.name()); + constantTimer.setId(IDGenerator.nextStr()); + stepDetails.put(constantTimer.getId(), JSON.parseObject(ApiDataUtils.toJSONString(constantTimerController))); + steps.add(constantTimer); + request.setId(addApiScenario.getId()); request.setSteps(steps); request.setStepDetails(new HashMap<>()); @@ -936,6 +953,15 @@ public class ApiScenarioControllerTests extends BaseTest { return msIfController; } + public MsConstantTimerController constantTimerController(String name, boolean enable) { + //等待控制器 + MsConstantTimerController constantTimerController = new MsConstantTimerController(); + constantTimerController.setName(StringUtils.isNotBlank(name) ? name : "等待控制器"); + constantTimerController.setEnable(enable); + constantTimerController.setDelay("1000"); + return constantTimerController; + } + //一次控制器 public MsOnceOnlyController onceController(String name, boolean enable) { //一次控制器