运行HttpSampler的JMeter API代码不起作用

时间:2015-07-21 13:50:23

标签: api jmeter

这是我根据我找到的一些示例使用JMeter API编写的用于运行HTTPRequest的类。

import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.engine.StandardJMeterEngine;
//import org.apache.jmeter.protocol.java.sampler.JUnitSampler;
import org.apache.jmeter.protocol.http.sampler.*;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.threads.SetupThreadGroup;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;

public class Jmeter {


public static void main(String[] args){

    // Engine
    StandardJMeterEngine jm = new StandardJMeterEngine();
    // jmeter.properties
    JMeterUtils.loadJMeterProperties("D:/apache-jmeter-2.13/bin/jmeter.properties");

    HashTree hashTree = new HashTree();     

    // HTTP Sampler
    HTTPSampler httpSampler = new HTTPSampler();
    httpSampler.setDomain("www.google.com");
    httpSampler.setPort(80);
    httpSampler.setPath("/");
    httpSampler.setMethod("GET");

    // Loop Controller
    TestElement loopCtrl = new LoopController();
    ((LoopController)loopCtrl).setLoops(1);
    ((LoopController)loopCtrl).addTestElement(httpSampler);
    ((LoopController)loopCtrl).setFirst(true);

    // Thread Group
    SetupThreadGroup threadGroup = new SetupThreadGroup();
    threadGroup.setNumThreads(1);
    threadGroup.setRampUp(1);
    threadGroup.setSamplerController((LoopController)loopCtrl);

    // Test plan
    TestPlan testPlan = new TestPlan("MY TEST PLAN");

    hashTree.add("testPlan", testPlan);
    hashTree.add("threadGroup", threadGroup);
    hashTree.add("httpSampler", httpSampler);       

    jm.configure(hashTree);
    System.out.println(jm.isActive());
    jm.run();
}
}

我在Eclipse中尝试相同的代码但是我收到了以下警告和错误。有人可以帮我解决吗?

提前致谢!

根据您的建议。我用循环控制器更新了代码。请在下面找到JMeter Log和Eclipse Output。

JMeter Log

2015/07/22 11:05:18 INFO  - jmeter.util.JMeterUtils: Setting Locale to en_US 
2015/07/22 11:05:18 INFO  - jmeter.protocol.http.sampler.HTTPSamplerBase: Cannot find .className property for htmlParser, using default 
2015/07/22 11:05:18 INFO  - jmeter.protocol.http.sampler.HTTPSamplerBase: Parser for text/html is  
2015/07/22 11:05:18 INFO  - jmeter.protocol.http.sampler.HTTPSamplerBase: Parser for application/xhtml+xml is  
2015/07/22 11:05:18 INFO  - jmeter.protocol.http.sampler.HTTPSamplerBase: Parser for application/xml is  
2015/07/22 11:05:18 INFO  - jmeter.protocol.http.sampler.HTTPSamplerBase: Parser for text/xml is  
2015/07/22 11:05:18 INFO  - jmeter.protocol.http.sampler.HTTPSamplerBase: Parser for text/vnd.wap.wml is   org.apache.jmeter.protocol.http.parser.RegexpHTMLParser 
2015/07/22 11:05:18 INFO  - jmeter.protocol.http.sampler.HTTPJavaImpl: Maximum connection retries = 10 
2015/07/22 11:05:18 INFO  - jmeter.engine.StandardJMeterEngine: Running the test! 
2015/07/22 11:05:18 INFO  - jmeter.samplers.SampleEvent: List of sample_variables: [] 
2015/07/22 11:05:18 INFO  - jmeter.samplers.SampleEvent: List of sample_variables: [] 
2015/07/22 11:05:18 INFO  - jmeter.engine.util.CompoundVariable: Note: Function class names must contain the string: '.functions.' 
2015/07/22 11:05:18 INFO  - jmeter.engine.util.CompoundVariable: Note: Function class names must not contain the string: '.gui.' 
2015/07/22 11:05:18 WARN  - jmeter.engine.util.CompoundVariable: Did not find any functions 
2015/07/22 11:05:18 INFO  - jmeter.engine.StandardJMeterEngine: Starting ThreadGroup: 1 :  
2015/07/22 11:05:18 INFO  - jmeter.engine.StandardJMeterEngine: Starting 1 threads for group . 
2015/07/22 11:05:18 INFO  - jmeter.engine.StandardJMeterEngine: Thread will continue on error 
2015/07/22 11:05:18 INFO  - jmeter.threads.ThreadGroup: Starting thread group number 1 threads 1 ramp-up 1 perThread 1000.0 delayedStart=false 
2015/07/22 11:05:18 INFO  - jmeter.threads.JMeterThread: jmeterthread.startearlier=true (see jmeter.properties) 
2015/07/22 11:05:18 INFO  - jmeter.threads.JMeterThread: Running PostProcessors in forward order 
2015/07/22 11:05:18 INFO  - jmeter.threads.ThreadGroup: Started thread group number 1 
2015/07/22 11:05:18 INFO  - jmeter.engine.StandardJMeterEngine: All thread groups have been started 
2015/07/22 11:05:18 INFO  - jmeter.threads.JMeterThread: Thread started:  1-1 
2015/07/22 11:05:18 INFO  - jmeter.threads.JMeterThread: Thread is done:  1-1 
2015/07/22 11:05:18 INFO  - jmeter.threads.JMeterThread: Thread finished:  1-1 
2015/07/22 11:05:18 INFO  - jmeter.engine.StandardJMeterEngine: Notifying test listeners of end of test 
2015/07/22 11:05:18 INFO  - jmeter.services.FileServer: Default base='D:\JMeter_Performance\JmeterSeleniumTest' 

Eclipse输出

WARN    2015-07-22 11:05:18.374 [jmeter.u] (): Unexpected value set for boolean property:'server.exitaftertest', defaulting to:false
WARN    2015-07-22 11:05:18.407 [jmeter.u] (): Unexpected value set for boolean property:'jmeterengine.startlistenerslater', defaulting to:true
INFO    2015-07-22 11:05:18.407 [jmeter.e] (): Listeners will be started after enabling running version
INFO    2015-07-22 11:05:18.407 [jmeter.e] (): To revert to the earlier behaviour, define jmeterengine.startlistenerslater=false
WARN    2015-07-22 11:05:18.407 [jmeter.u] (): Unexpected value set for boolean property:'jmeterengine.remote.system.exit', defaulting to:false
WARN    2015-07-22 11:05:18.407 [jmeter.u] (): Unexpected value set for boolean property:'jmeterengine.stopfail.system.exit', defaulting to:true
WARN    2015-07-22 11:05:18.408 [jmeter.u] (): Unexpected value set for boolean property:'jmeterengine.force.system.exit', defaulting to:false
Exception in thread "main" java.lang.NoClassDefFoundError: com/thoughtworks/xstream/converters/ConversionException
at org.apache.jmeter.engine.StandardJMeterEngine.run(StandardJMeterEngine.java:450)
at JmeterClass.JMeterFromScratch.main(JMeterFromScratch.java:56)
Caused by: java.lang.ClassNotFoundException: com.thoughtworks.xstream.converters.ConversionException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more

请帮我解决这个问题。谢谢!

1 个答案:

答案 0 :(得分:1)

我认为您需要添加以下行:

hashTree.add("loopController", loopCtrl);

之前jm.configure(hashTree);

如果它没有帮助 - 使用Eclipse输出和jmeter.log文件内容更新您的问题(应该出现在您的项目根目录中)

请参阅以下材料以供参考: