diff --git a/backend/src/main/java/io/metersphere/api/jmeter/MsResultCollector.java b/backend/src/main/java/io/metersphere/api/jmeter/MsResultCollector.java index 7d94b9d061..38dafac3cb 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/MsResultCollector.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/MsResultCollector.java @@ -21,14 +21,17 @@ import io.metersphere.api.dto.RunningParamKeys; import io.metersphere.api.service.MsResultService; import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.LogUtil; +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.jmeter.engine.util.NoThreadClone; import org.apache.jmeter.reporters.AbstractListenerElement; import org.apache.jmeter.samplers.*; import org.apache.jmeter.testelement.TestStateListener; import org.apache.jmeter.testelement.property.BooleanProperty; +import org.apache.jmeter.threads.JMeterVariables; import java.io.Serializable; +import java.util.Map; /** * 实时结果监听 @@ -124,6 +127,16 @@ public class MsResultCollector extends AbstractListenerElement implements Sample @Override public void sampleOccurred(SampleEvent event) { SampleResult result = event.getResult(); + JMeterVariables variables = JMeterVars.get(result.hashCode()); + if (variables != null && CollectionUtils.isNotEmpty(variables.entrySet())) { + StringBuilder builder = new StringBuilder(); + for (Map.Entry entry : variables.entrySet()) { + builder.append(entry.getKey()).append(":").append(entry.getValue()).append("\n"); + } + if (StringUtils.isNotEmpty(builder)) { + result.setExtVars(builder.toString()); + } + } if (isSampleWanted(result.isSuccessful()) && !StringUtils.equals(result.getSampleLabel(), RunningParamKeys.RUNNING_DEBUG_SAMPLER_NAME)) { msResultService.setCache(this.getName(), result); } diff --git a/backend/src/main/java/io/metersphere/api/service/MsResultService.java b/backend/src/main/java/io/metersphere/api/service/MsResultService.java index 518ef3e5a7..6c71137d92 100644 --- a/backend/src/main/java/io/metersphere/api/service/MsResultService.java +++ b/backend/src/main/java/io/metersphere/api/service/MsResultService.java @@ -9,6 +9,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.jmeter.assertions.AssertionResult; import org.apache.jmeter.protocol.http.sampler.HTTPSampleResult; import org.apache.jmeter.samplers.SampleResult; +import org.apache.jmeter.threads.JMeterVariables; import org.springframework.http.HttpMethod; import org.springframework.stereotype.Service; import sun.security.util.Cache; @@ -188,9 +189,13 @@ public class MsResultService { responseResult.setResponseSize(result.getResponseData().length); responseResult.setResponseTime(result.getTime()); responseResult.setResponseMessage(result.getResponseMessage()); - if (JMeterVars.get(result.hashCode()) != null && CollectionUtils.isNotEmpty(JMeterVars.get(result.hashCode()).entrySet())) { + JMeterVariables variables = JMeterVars.get(result.hashCode()); + if (StringUtils.isNotEmpty(result.getExtVars())) { + responseResult.setVars(result.getExtVars()); + JMeterVars.remove(result.hashCode()); + } else if (variables != null && CollectionUtils.isNotEmpty(variables.entrySet())) { StringBuilder builder = new StringBuilder(); - for (Map.Entry entry : JMeterVars.get(result.hashCode()).entrySet()) { + for (Map.Entry entry : variables.entrySet()) { builder.append(entry.getKey()).append(":").append(entry.getValue()).append("\n"); } if (StringUtils.isNotEmpty(builder)) { @@ -198,6 +203,7 @@ public class MsResultService { } JMeterVars.remove(result.hashCode()); } + for (AssertionResult assertionResult : result.getAssertionResults()) { ResponseAssertionResult responseAssertionResult = getResponseAssertionResult(assertionResult); if (responseAssertionResult.isPass()) { diff --git a/backend/src/main/java/org/apache/jmeter/samplers/SampleResult.java b/backend/src/main/java/org/apache/jmeter/samplers/SampleResult.java index 529ab10c9d..a2418f70e0 100644 --- a/backend/src/main/java/org/apache/jmeter/samplers/SampleResult.java +++ b/backend/src/main/java/org/apache/jmeter/samplers/SampleResult.java @@ -170,6 +170,8 @@ public class SampleResult implements Serializable, Cloneable, Searchable { private String resourceId; + private String extVars; + public String getSamplerId() { return this.samplerId; } @@ -185,6 +187,14 @@ public class SampleResult implements Serializable, Cloneable, Searchable { return this.scenario; } + public void setExtVars(String vars) { + this.extVars = vars; + } + + public String getExtVars() { + return this.extVars; + } + /** * 定制自定义添加 ================= */