fix(接口测试): 优化场景前置等待时间配置

This commit is contained in:
AgAngle 2024-04-11 16:51:37 +08:00 committed by Craftsman
parent 7a1f17c22e
commit 8acefab96d
6 changed files with 44 additions and 54 deletions

View File

@ -20,11 +20,6 @@ public class ScenarioOtherConfig {
* 启用场景步骤等待时间 * 启用场景步骤等待时间
*/ */
private Boolean enableStepWait = false; private Boolean enableStepWait = false;
/**
* 场景步骤等待时间
* 每一个步骤执行后都会等待相应的时间
*/
private Long stepWaitTime;
/** /**
* 失败策略 * 失败策略
* @see FailureStrategy * @see FailureStrategy

View File

@ -7,8 +7,11 @@ import io.metersphere.api.dto.debug.ApiResourceRunRequest;
import io.metersphere.api.dto.request.MsScenario; import io.metersphere.api.dto.request.MsScenario;
import io.metersphere.api.dto.scenario.ApiScenarioDetail; import io.metersphere.api.dto.scenario.ApiScenarioDetail;
import io.metersphere.api.dto.scenario.ApiScenarioParseParam; import io.metersphere.api.dto.scenario.ApiScenarioParseParam;
import io.metersphere.api.dto.scenario.ScenarioConfig;
import io.metersphere.api.service.ApiExecuteService; import io.metersphere.api.service.ApiExecuteService;
import io.metersphere.api.service.scenario.ApiScenarioService; import io.metersphere.api.service.scenario.ApiScenarioService;
import io.metersphere.project.api.processor.MsProcessor;
import io.metersphere.project.api.processor.TimeWaitingProcessor;
import io.metersphere.sdk.constants.ApiBatchRunMode; import io.metersphere.sdk.constants.ApiBatchRunMode;
import io.metersphere.sdk.constants.ApiExecuteRunMode; import io.metersphere.sdk.constants.ApiExecuteRunMode;
import io.metersphere.sdk.constants.TaskTriggerMode; import io.metersphere.sdk.constants.TaskTriggerMode;
@ -22,10 +25,14 @@ import io.metersphere.system.uid.IDGenerator;
import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable;
import org.quartz.JobExecutionContext; import org.quartz.JobExecutionContext;
import org.quartz.JobKey; import org.quartz.JobKey;
import org.quartz.TriggerKey; import org.quartz.TriggerKey;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
public class ApiScenarioScheduleJob extends BaseScheduleJob { public class ApiScenarioScheduleJob extends BaseScheduleJob {
@Override @Override
protected void businessExecute(JobExecutionContext context) { protected void businessExecute(JobExecutionContext context) {
@ -74,11 +81,8 @@ public class ApiScenarioScheduleJob extends BaseScheduleJob {
scenarioReport.setRunMode(ApiBatchRunMode.PARALLEL.name()); scenarioReport.setRunMode(ApiBatchRunMode.PARALLEL.name());
scenarioReport.setPoolId(apiRunModeConfigDTO.getPoolId()); scenarioReport.setPoolId(apiRunModeConfigDTO.getPoolId());
scenarioReport.setEnvironmentId(parseParam.getEnvironmentId()); scenarioReport.setEnvironmentId(parseParam.getEnvironmentId());
if (parseParam.getScenarioConfig() != null scenarioReport.setWaitingTime(apiScenarioService.getGlobalWaitTime(parseParam.getScenarioConfig()));
&& parseParam.getScenarioConfig().getOtherConfig() != null
&& BooleanUtils.isTrue(parseParam.getScenarioConfig().getOtherConfig().getEnableStepWait())) {
scenarioReport.setWaitingTime(parseParam.getScenarioConfig().getOtherConfig().getStepWaitTime());
}
apiScenarioService.initApiReport(apiScenarioDetail, scenarioReport); apiScenarioService.initApiReport(apiScenarioDetail, scenarioReport);
// 初始化报告步骤 // 初始化报告步骤
@ -87,6 +91,8 @@ public class ApiScenarioScheduleJob extends BaseScheduleJob {
apiExecuteService.execute(runRequest, taskRequest, parseConfig); apiExecuteService.execute(runRequest, taskRequest, parseConfig);
} }
public static JobKey getJobKey(String scenarioId) { public static JobKey getJobKey(String scenarioId) {
return new JobKey(scenarioId, ApiScenarioScheduleJob.class.getName()); return new JobKey(scenarioId, ApiScenarioScheduleJob.class.getName());
} }

View File

@ -6,18 +6,15 @@ import io.metersphere.api.dto.ApiScenarioParamConfig;
import io.metersphere.api.dto.request.MsScenario; import io.metersphere.api.dto.request.MsScenario;
import io.metersphere.api.dto.request.processors.MsProcessorConfig; import io.metersphere.api.dto.request.processors.MsProcessorConfig;
import io.metersphere.api.dto.scenario.ScenarioConfig; 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.ScenarioStepConfig;
import io.metersphere.api.dto.scenario.ScenarioVariable; import io.metersphere.api.dto.scenario.ScenarioVariable;
import io.metersphere.api.parser.jmeter.processor.MsProcessorConverter; import io.metersphere.api.parser.jmeter.processor.MsProcessorConverter;
import io.metersphere.api.parser.jmeter.processor.MsProcessorConverterFactory; import io.metersphere.api.parser.jmeter.processor.MsProcessorConverterFactory;
import io.metersphere.api.parser.jmeter.processor.ScenarioTimeWaitingProcessorConverter;
import io.metersphere.api.parser.jmeter.processor.assertion.AssertionConverterFactory; import io.metersphere.api.parser.jmeter.processor.assertion.AssertionConverterFactory;
import io.metersphere.plugin.api.dto.ParameterConfig; import io.metersphere.plugin.api.dto.ParameterConfig;
import io.metersphere.plugin.api.spi.AbstractJmeterElementConverter; import io.metersphere.plugin.api.spi.AbstractJmeterElementConverter;
import io.metersphere.project.api.assertion.MsAssertion; import io.metersphere.project.api.assertion.MsAssertion;
import io.metersphere.project.api.processor.MsProcessor; 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.EnvironmentConfig;
import io.metersphere.project.dto.environment.EnvironmentInfoDTO; import io.metersphere.project.dto.environment.EnvironmentInfoDTO;
import io.metersphere.project.dto.environment.processors.EnvProcessorConfig; import io.metersphere.project.dto.environment.processors.EnvProcessorConfig;
@ -61,9 +58,6 @@ public class MsScenarioConverter extends AbstractJmeterElementConverter<MsScenar
// 添加场景和环境变量 // 添加场景和环境变量
addArguments(tree, msScenario, envInfo); addArguments(tree, msScenario, envInfo);
// 添加场景每个步骤的全局等待时间
addScenarioStepTimeWaiting(tree, msScenario, msParameter);
// 添加环境的前置 // 添加环境的前置
addEnvScenarioProcessor(tree, msScenario, config, envInfo, true); addEnvScenarioProcessor(tree, msScenario, config, envInfo, true);
// 添加场景前置 // 添加场景前置
@ -82,26 +76,6 @@ public class MsScenarioConverter extends AbstractJmeterElementConverter<MsScenar
addScenarioAssertions(tree, msScenario, config); addScenarioAssertions(tree, msScenario, config);
} }
/**
* 添加步骤等待
* @param tree
* @param msScenario
*/
private void addScenarioStepTimeWaiting(HashTree tree, MsScenario msScenario, ParameterConfig config) {
if (isRootScenario(msScenario.getRefType())) {
// 获取场景前后置
ScenarioConfig scenarioConfig = msScenario.getScenarioConfig();
ScenarioOtherConfig otherConfig = scenarioConfig.getOtherConfig();
if (BooleanUtils.isTrue(otherConfig.getEnableStepWait())) {
TimeWaitingProcessor timeWaitingProcessor = new TimeWaitingProcessor();
timeWaitingProcessor.setDelay(otherConfig.getStepWaitTime());
timeWaitingProcessor.setName(msScenario.getName());
MsProcessorConverter timeWaitingConverter = MsProcessorConverterFactory.getPreConverter(TimeWaitingProcessor.class);
timeWaitingConverter.parse(tree, timeWaitingProcessor, config);
}
}
}
/** /**
* 添加场景和环境变量 * 添加场景和环境变量
* @param tree * @param tree
@ -282,14 +256,7 @@ public class MsScenarioConverter extends AbstractJmeterElementConverter<MsScenar
// 添加场景前置处理器 // 添加场景前置处理器
scenarioPreProcessors.forEach(processor -> { scenarioPreProcessors.forEach(processor -> {
processor.setProjectId(msScenario.getProjectId()); processor.setProjectId(msScenario.getProjectId());
MsProcessorConverter converter; getConverterFunc.apply(processor.getClass()).parse(tree, processor, config);
if (processor instanceof TimeWaitingProcessor) {
// 场景的的等待时间需要包一层 debugSampler
converter = new ScenarioTimeWaitingProcessorConverter();
} else {
converter = getConverterFunc.apply(processor.getClass());
}
converter.parse(tree, processor, config);
}); });
} }

View File

@ -348,12 +348,11 @@ public class ApiScenarioBatchRunService {
} }
private void updateReportWaitTime(String reportId, ApiScenarioParseParam parseParam) { private void updateReportWaitTime(String reportId, ApiScenarioParseParam parseParam) {
if (parseParam.getScenarioConfig() != null Long globalWaitTime = apiScenarioService.getGlobalWaitTime(parseParam.getScenarioConfig());
&& parseParam.getScenarioConfig().getOtherConfig() != null if (globalWaitTime != null) {
&& BooleanUtils.isTrue(parseParam.getScenarioConfig().getOtherConfig().getEnableStepWait())) {
ApiScenarioReport apiScenarioReport = new ApiScenarioReport(); ApiScenarioReport apiScenarioReport = new ApiScenarioReport();
apiScenarioReport.setId(reportId); apiScenarioReport.setId(reportId);
apiScenarioReport.setWaitingTime(parseParam.getScenarioConfig().getOtherConfig().getStepWaitTime()); apiScenarioReport.setWaitingTime(globalWaitTime);
apiScenarioReportMapper.updateByPrimaryKeySelective(apiScenarioReport); apiScenarioReportMapper.updateByPrimaryKeySelective(apiScenarioReport);
} }
} }

View File

@ -27,6 +27,8 @@ import io.metersphere.api.service.definition.ApiTestCaseService;
import io.metersphere.api.utils.ApiDataUtils; import io.metersphere.api.utils.ApiDataUtils;
import io.metersphere.api.utils.ApiScenarioBatchOperationUtils; import io.metersphere.api.utils.ApiScenarioBatchOperationUtils;
import io.metersphere.plugin.api.spi.AbstractMsTestElement; import io.metersphere.plugin.api.spi.AbstractMsTestElement;
import io.metersphere.project.api.processor.MsProcessor;
import io.metersphere.project.api.processor.TimeWaitingProcessor;
import io.metersphere.project.domain.FileAssociation; import io.metersphere.project.domain.FileAssociation;
import io.metersphere.project.domain.FileMetadata; import io.metersphere.project.domain.FileMetadata;
import io.metersphere.project.domain.Project; import io.metersphere.project.domain.Project;
@ -1390,11 +1392,8 @@ public class ApiScenarioService extends MoveNodeService {
scenarioReport.setRunMode(ApiBatchRunMode.PARALLEL.name()); scenarioReport.setRunMode(ApiBatchRunMode.PARALLEL.name());
scenarioReport.setPoolId(poolId); scenarioReport.setPoolId(poolId);
scenarioReport.setEnvironmentId(parseParam.getEnvironmentId()); scenarioReport.setEnvironmentId(parseParam.getEnvironmentId());
if (parseParam.getScenarioConfig() != null scenarioReport.setWaitingTime(getGlobalWaitTime(parseParam.getScenarioConfig()));
&& parseParam.getScenarioConfig().getOtherConfig() != null
&& BooleanUtils.isTrue(parseParam.getScenarioConfig().getOtherConfig().getEnableStepWait())) {
scenarioReport.setWaitingTime(parseParam.getScenarioConfig().getOtherConfig().getStepWaitTime());
}
initApiReport(apiScenario, scenarioReport); initApiReport(apiScenario, scenarioReport);
// 初始化报告步骤 // 初始化报告步骤
@ -2850,4 +2849,29 @@ public class ApiScenarioService extends MoveNodeService {
apiStepResourceInfo.setProjectName(project.getName()); apiStepResourceInfo.setProjectName(project.getName());
return apiStepResourceInfo; return apiStepResourceInfo;
} }
/**
* 获取场景前置的总等待时间
* @param scenarioConfig
* @return
*/
public Long getGlobalWaitTime(ScenarioConfig scenarioConfig) {
Long waitTime = null;
if (scenarioConfig != null
&& scenarioConfig.getPreProcessorConfig() != null
&& scenarioConfig.getPreProcessorConfig().getProcessors() != null) {
waitTime = 0L;
for (MsProcessor processor : scenarioConfig
.getPreProcessorConfig()
.getProcessors()) {
if (processor instanceof TimeWaitingProcessor timeWaitingProcessor
&& timeWaitingProcessor.getEnable()
&& timeWaitingProcessor.getDelay() != null) {
waitTime += timeWaitingProcessor.getDelay();
}
}
waitTime = waitTime > 0 ? waitTime : null;
}
return waitTime;
}
} }

View File

@ -456,7 +456,6 @@ public class ApiScenarioControllerTests extends BaseTest {
responseCodeAssertion.setName("test"); responseCodeAssertion.setName("test");
scenarioConfig.getAssertionConfig().getAssertions().add(responseCodeAssertion); scenarioConfig.getAssertionConfig().getAssertions().add(responseCodeAssertion);
ScenarioOtherConfig scenarioOtherConfig = new ScenarioOtherConfig(); ScenarioOtherConfig scenarioOtherConfig = new ScenarioOtherConfig();
scenarioOtherConfig.setStepWaitTime(1000L);
scenarioOtherConfig.setFailureStrategy(ScenarioOtherConfig.FailureStrategy.CONTINUE.name()); scenarioOtherConfig.setFailureStrategy(ScenarioOtherConfig.FailureStrategy.CONTINUE.name());
scenarioOtherConfig.setEnableCookieShare(true); scenarioOtherConfig.setEnableCookieShare(true);
scenarioConfig.setOtherConfig(scenarioOtherConfig); scenarioConfig.setOtherConfig(scenarioOtherConfig);