我尝试使用Jmeter开源+java来模拟压力测试并从Jenkins运行它们。该项目是用 Maven 构建的,所以我没有 Jenkins 的问题。但在将请求发送到服务器后我无法捕获输出信息。
public void runTest( String url,String user,int URLnumber) {
StandardJMeterEngine jm = new StandardJMeterEngine();
// jmeter.properties
JMeterUtils.loadJMeterProperties("./jmeter.properties");
HashTree hashTree = new HashTree();
// HTTP Sampler
HTTPSampler httpSampler = new HTTPSampler();
httpSampler.setDomain(url);
//httpSampler.setPort(80);
httpSampler.setPath("/");
httpSampler.setMethod("POST");
httpSampler.addArgument("post_id","51015");
httpSampler.addArgument("variant","");
// 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(25);
threadGroup.setRampUp(5);
threadGroup.setSamplerController((LoopController)loopCtrl);
org.apache.jmeter.reporters.Summariser summariser = new Summariser();
// 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();
}
可能有人知道如何通过 Jenkins 配置 Jmeter,无需编码,并在 Jenkins 控制台中获取结果。但我认为这是不可能的。
最佳答案
你见过Jenkins Performance Plugin吗?哪个支持JMeter?
如果出于任何原因不适合,Jenkins 至少可以执行以下目标:
- Shell 脚本
- Ant 任务
- Maven 简介
JMeter 可以通过上述所有方法启动,而无需编写一行 Java 代码。请参阅5 Ways To Launch a JMeter Test without Using the JMeter GUI了解详情。
关于java - Jmeter - java 编码。如何获取输出数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23826089/