如何从 Java 程序创建和运行 Apache JMeter 测试脚本? [英] How to create and run Apache JMeter Test Scripts from a Java program?

查看:29
本文介绍了如何从 Java 程序创建和运行 Apache JMeter 测试脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Apache JMeter 提供的 API 从 Java 程序创建和运行测试脚本.我已经了解了 ThreadGroup 和 Samplers 的基础知识.我可以使用 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
  • avalon-framework-4.1.4.jar
  • ApacheJMeter_http.jar
  • commons-logging-1.1.1.jar
  • logkit-2.0.jar
  • oro-2.0.8.jar
  • commons-io-2.2.jar
  • commons-lang3-3.1.jar

注意

  • 首先从 JMeter 安装/bin 目录中复制 jmeter.properties 的路径后,我还在 Windows 上的 c: mp 中硬连线了它.
  • 我不确定如何为 HTTPSampler 设置转发代理.

这篇关于如何从 Java 程序创建和运行 Apache JMeter 测试脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆