如何创建和Java程序运行Apache JMeter的测试脚本? [英] How to create and run Apache JMeter Test Scripts from a Java program?
问题描述
我想用被Apache JMeter的提供的API来从Java程序中创建和运行测试脚本。我明白线程组和采样器的基础知识。我可以通过使用JMeter的API来创建那些在我的Java类。
I want to use the API provided by Apache JMeter to create and run test scripts from a Java program. I have understood the basics of ThreadGroup and Samplers. I can create those in my Java class by using the JMeter API.
ThreadGroup threadGroup = new ThreadGroup();
LoopController lc = new LoopController();
lc.setLoops(5);
lc.setContinueForever(true);
threadGroup.setSamplerController(lc);
threadGroup.setNumThreads(5);
threadGroup.setRampUp(1);
HTTPSampler sampler = new HTTPSampler();
sampler.setDomain("localhost");
sampler.setPort(8080);
sampler.setPath("/jpetstore/shop/viewCategory.shtml");
sampler.setMethod("GET");
Arguments arg = new Arguments();
arg.addArgument("categoryId", "FISH");
sampler.setArguments(arg);
不过,我没有得到关于如何创建一个测试脚本线程组和采样器相结合,然后从同一程序执行它的想法。有任何想法吗?
However, I am not getting any idea on how to create a test script combining the thread group and sampler and then execute it from the same program. Any ideas?
推荐答案
如果我理解正确的话,你想从一个Java程序中以编程方式运行整个测试计划。就个人而言,我觉得它更容易地创建一个测试计划.JMX文件和JMeter的非GUI模式运行:)
If I understand correctly, you want to run an entire test plan programmatically from within a Java program. Personally, I find it easier to create a test plan .JMX file and run it in JMeter non-GUI mode :)
下面是总部设在原来的问题所使用的控制器和采样器在一个简单的Java例子。
Here is a simple Java example based on the controller and sampler used in the original question.
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.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 JMeterTestFromCode {
public static void main(String[] args){
// Engine
StandardJMeterEngine jm = new StandardJMeterEngine();
// jmeter.properties
JMeterUtils.loadJMeterProperties("c:/tmp/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("loopCtrl", loopCtrl);
hashTree.add("threadGroup", threadGroup);
hashTree.add("httpSampler", httpSampler);
jm.configure(hashTree);
jm.run();
}
}
依赖
这些是基于JMeter的2.9和所使用的HTTPSampler要求裸JAR文件的最小值。
其他采样将最有可能有不同的库JAR依赖关系。
These are the bare mininum JARs required based on JMeter 2.9 and the HTTPSampler used. Other samplers will most likely have different library JAR dependencies.
- ApacheJMeter_core.jar
- jorphan.jar
- 阿瓦隆框架-4.1.4.jar
- ApacheJMeter_http.jar
- 的commons-logging-1.1.1.jar
- logkit-2.0.jar
- 口 - 2.0.8.jar
- 的commons-IO-2.2.jar
- 公地lang3-3.1.jar
注意
- 我还硬的路径在C jmeter.properties:\\ tmp目录上的Windows后第一次从JMeter的安装/ bin目录复制它 。
- 我不知道如何设置转发代理的HTTPSampler。
这篇关于如何创建和Java程序运行Apache JMeter的测试脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!