JMeter的API code运行HttpSampler不工作 [英] JMeter API Code to run HttpSampler doesnt work
问题描述
这是我写运行HTT prequest使用基于一些样品,我发现JMeter的API的类。
This is the class that I have written to run HTTPRequest using JMeter API based on some samples I found.
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的code,但我得到以下警告和错误。谁能帮我解决这个问题?
I'm trying the same code in Eclipse but I'm getting the following warnings and errors. Can anyone help me resolve it?
在此先感谢!
根据您的建议。我已经更新了code与回路控制器。请看以下JMeter的日志和Eclipse输出。
Based on your suggestion. I have updated the code with loop controller. Please find the JMeter Log and Eclipse Output below.
JMeter的日志
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的输出
The Eclipse Output
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
请帮我解决这个issue.Thanks!
Please help me resolve this issue.Thanks!
推荐答案
我相信,你需要添加以下行:
I believe that you need to add the following line:
hashTree.add("loopController", loopCtrl);
在 jm.configure(hashTree);
如果没有帮助 - 更新与Eclipse输出和jmeter.log文件内容(应该出现在你的项目根目录)你的问题。
If it doesn't help - update your question with Eclipse output and jmeter.log file contents (should appear in your project root)
请参阅以下资料供参考:
See the following material for reference:
- 创建一个全新的JMeter测试纯粹是在Java中的的 5种方法来启动一个JMeter测试,而无需使用JMeter的GUI
- 示例项目使用Java API证明JMeter测试的创建哪些
- 产生.jtl结果文件
- 汇总程序产生的输出
- 生成可以在JMeter的打开.jmx文件
- "Creating a brand new JMeter test purely in Java" chapter of the 5 Ways To Launch a JMeter Test without Using the JMeter GUI
- Sample project demonstrating creation of JMeter test using Java API which
- produces .jtl result file
- produces summarizer output
- generates .jmx file which can be opened in JMeter
这篇关于JMeter的API code运行HttpSampler不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!