Reputation: 143
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();
}
}
I'm trying the same code in Eclipse but I'm getting the following warnings and errors. Can anyone help me resolve it?
Thanks in advance!
Based on your suggestion. I have updated the code with loop controller. Please find the JMeter Log and Eclipse Output below.
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'
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
Please help me resolve this issue.Thanks!
Upvotes: 0
Views: 1446
Reputation: 168002
I believe that you need to add the following line:
hashTree.add("loopController", loopCtrl);
before jm.configure(hashTree);
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:
Upvotes: 1