From eb4bfcc08bbec647be1f74f49e2d52559645caf9 Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Fri, 17 Apr 2020 18:53:32 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=20jmeter.properties=20?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E5=8A=A0=E8=BD=BD=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/utils/MsJMeterUtils.java | 44 +++++++++++++++++++ .../io/metersphere/report/JtlResolver.java | 7 ++- 2 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 backend/src/main/java/io/metersphere/commons/utils/MsJMeterUtils.java diff --git a/backend/src/main/java/io/metersphere/commons/utils/MsJMeterUtils.java b/backend/src/main/java/io/metersphere/commons/utils/MsJMeterUtils.java new file mode 100644 index 0000000000..85a9b638f4 --- /dev/null +++ b/backend/src/main/java/io/metersphere/commons/utils/MsJMeterUtils.java @@ -0,0 +1,44 @@ +package io.metersphere.commons.utils; + +import org.apache.commons.lang3.reflect.FieldUtils; +import org.apache.jmeter.util.JMeterUtils; +import org.apache.jorphan.util.JOrphanUtils; + +import java.io.InputStream; +import java.util.Properties; + +public class MsJMeterUtils { + /** + * Load the JMeter properties file; if not found, then + * default to "org/apache/jmeter/jmeter.properties" from the classpath + * + *

+ * c.f. loadProperties + * + * @param file Name of the file from which the JMeter properties should be loaded + */ + public static void loadJMeterProperties(String file) { + InputStream is = null; + try { + JMeterUtils.loadJMeterProperties(file); + } catch (Exception e) { + try { + Properties p = new Properties(System.getProperties()); + // In jar file classpath is + is = ClassLoader.getSystemResourceAsStream( + "BOOT-INF/classes/org/apache/jmeter/jmeter.properties"); // $NON-NLS-1$ + if (is == null) { + throw new RuntimeException("Could not read JMeter properties file:" + file); + } + p.load(is); + + FieldUtils.writeStaticField(JMeterUtils.class, "appProperties", p, true); + } catch (Exception ex) { + throw new RuntimeException("Could not read JMeter properties file:" + file); + } + } finally { + JOrphanUtils.closeQuietly(is); + } + } + +} diff --git a/backend/src/main/java/io/metersphere/report/JtlResolver.java b/backend/src/main/java/io/metersphere/report/JtlResolver.java index 87082b87c5..010f52f02a 100644 --- a/backend/src/main/java/io/metersphere/report/JtlResolver.java +++ b/backend/src/main/java/io/metersphere/report/JtlResolver.java @@ -3,7 +3,7 @@ package io.metersphere.report; import com.opencsv.bean.CsvToBean; import com.opencsv.bean.CsvToBeanBuilder; import com.opencsv.bean.HeaderColumnNameMappingStrategy; -import io.metersphere.base.domain.LoadTestReportWithBLOBs; +import io.metersphere.commons.utils.MsJMeterUtils; import io.metersphere.report.base.*; import io.metersphere.report.dto.ErrorsTop5DTO; import io.metersphere.report.dto.RequestStatisticsDTO; @@ -19,7 +19,6 @@ import org.apache.jmeter.report.processor.graph.AbstractOverTimeGraphConsumer; import org.apache.jmeter.report.processor.graph.impl.ActiveThreadsGraphConsumer; import org.apache.jmeter.report.processor.graph.impl.HitsPerSecondGraphConsumer; import org.apache.jmeter.report.processor.graph.impl.ResponseTimeOverTimeGraphConsumer; -import org.apache.jmeter.util.JMeterUtils; import java.io.Reader; import java.io.StringReader; @@ -500,8 +499,8 @@ public class JtlResolver { public static Map getResultDataMap(String jtlString, AbstractOverTimeGraphConsumer timeGraphConsumer) { AbstractOverTimeGraphConsumer abstractOverTimeGraphConsumer = timeGraphConsumer; abstractOverTimeGraphConsumer.setGranularity(60000); - // 这个路径不存在 - JMeterUtils.loadJMeterProperties("jmeter.properties"); + // 使用反射获取properties + MsJMeterUtils.loadJMeterProperties("jmeter.properties"); // 这个路径不存在 SampleMetadata sampleMetaData = createTestMetaData(); SampleContext sampleContext = new SampleContext(); abstractOverTimeGraphConsumer.setSampleContext(sampleContext);