From 999edfd3fb04ce6fe718e02efbe8dd3c6bdc4606 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Tue, 27 Jul 2021 18:34:29 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=8C=96):=20=E8=B0=83=E8=AF=95=E7=BB=93=E6=9E=9C=E6=8F=90?= =?UTF-8?q?=E5=8F=96=E5=8F=98=E9=87=8F=E6=98=BE=E7=A4=BA=E4=B8=A2=E5=A4=B1?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D=20#1005450=20--bug=3D10054?= =?UTF-8?q?50=20--user=3D=E8=B5=B5=E5=8B=87=20=E3=80=90=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=8C=96=E3=80=91=E5=93=8D=E5=BA=94=E7=BB=93?= =?UTF-8?q?...=20https://www.tapd.cn/55049933/s/1029850?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../metersphere/api/jmeter/MsResultCollector.java | 13 +++++++++++++ .../io/metersphere/api/service/MsResultService.java | 10 ++++++++-- .../org/apache/jmeter/samplers/SampleResult.java | 10 ++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) 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; + } + /** * 定制自定义添加 ================= */