gpt4 book ai didi

java - 在Java中运行Jmeter测试时出现空指针异常

转载 作者:行者123 更新时间:2023-12-02 09:21:08 24 4
gpt4 key购买 nike

以下是我在 Java8 中运行 jmeter 测试计划的简单代码。

import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.protocol.http.sampler.HTTPSampler;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.threads.ThreadGroup;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;


public class JmxSuite {

public static void main(String[] args){
StandardJMeterEngine jmeter = new StandardJMeterEngine();
JMeterUtils.setJMeterHome("C:\\PLACES\\apache-jmeter-2.11");



HashTree testPlanTree = new HashTree();

HTTPSampler httpSampler = new HTTPSampler();
httpSampler.setDomain("exapmle.com");
httpSampler.setPort(80);
httpSampler.setPath("/");
httpSampler.setMethod("GET");


LoopController loopController = new LoopController();
loopController.setLoops(1);
loopController.addTestElement(httpSampler);
loopController.setFirst(true);
loopController.initialize();


ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setNumThreads(1);
threadGroup.setRampUp(1);
threadGroup.setSamplerController(loopController);

TestPlan testPlan = new TestPlan("RKSV Jmeter Testing");

testPlanTree.add("testPlan", testPlan);
testPlanTree.add("loopController", loopController);
testPlanTree.add("threadGroup", threadGroup);
testPlanTree.add("httpSampler", httpSampler);

jmeter.configure(testPlanTree);
jmeter.run();
}
}

但是我在运行时不断收到以下错误。

INFO    2016-04-07 15:40:44.060 [jmeter.e] (): Listeners will be started after enabling running version
INFO 2016-04-07 15:40:44.079 [jmeter.e] (): To revert to the earlier behaviour, define jmeterengine.startlistenerslater=false
INFO 2016-04-07 15:40:44.108 [jmeter.p] (): No response parsers defined: text/html only will be scanned for embedded resources
INFO 2016-04-07 15:40:44.115 [jmeter.p] (): Maximum connection retries = 10
INFO 2016-04-07 15:40:44.121 [jmeter.e] (): Running the test!
INFO 2016-04-07 15:40:44.141 [jmeter.s] (): List of sample_variables: []
INFO 2016-04-07 15:40:44.141 [jmeter.s] (): List of sample_variables: []
Exception in thread "main" java.lang.NullPointerException
at org.apache.jmeter.util.JMeterUtils.setProperty(JMeterUtils.java:885)
at org.apache.jmeter.threads.JMeterContextService.startTest(JMeterContextService.java:92)
at org.apache.jmeter.engine.StandardJMeterEngine.run(StandardJMeterEngine.java:313)
at JmxSuite.main(JmxSuite.java:48)

下面是我的 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Places</groupId>
<artifactId>Places</artifactId>
<version>0.0.1-SNAPSHOT</version>


<dependencies>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_java</artifactId>
<version>2.11</version>
</dependency>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_http</artifactId>
<version>2.11</version>
</dependency>
</dependencies>


</project>

可能的原因是什么?我使用了此 5 ways to run jmeter 中第四个主题的代码

最佳答案

您在文章中遗漏了几行,请仔细检查您的代码,即:

JMeterUtils.loadJMeterProperties("C:\\PLACES\\apache-jmeter-2.11\\bin\\jmeter.properties");
JMeterUtils.initLogging();
JMeterUtils.initLocale();

还有一个示例项目 jmeter-from-code可以作为引用或骨架。

还可以考虑使用最新版本的 JMeter,目前是 Apache JMeter 2.13

关于java - 在Java中运行Jmeter测试时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36473205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com