Java生成JMeter配置文件.jmx以及调用示例

 

1 Maven 配置jar包

<dependency>

            <groupId>org.apache.jmeter</groupId>

            <artifactId>ApacheJMeter_http</artifactId>

            <version>${jmeter.version}</version>

            <exclusions>

                <exclusion>

                    <groupId>org.apache.logging.log4j</groupId>

                    <artifactId>log4j-slf4j-impl</artifactId>

                </exclusion>

            </exclusions>

        </dependency>

        <dependency>

            <groupId>org.apache.jmeter</groupId>

            <artifactId>ApacheJMeter_functions</artifactId>

            <version>${jmeter.version}</version>

            <exclusions>

                <exclusion>

                    <groupId>org.apache.logging.log4j</groupId>

                    <artifactId>log4j-slf4j-impl</artifactId>

                </exclusion>

            </exclusions>

        </dependency>

        <dependency>

            <groupId>org.apache.jmeter</groupId>

            <artifactId>ApacheJMeter_jdbc</artifactId>

            <version>${jmeter.version}</version>

            <exclusions>

                <exclusion>

                    <groupId>org.apache.logging.log4j</groupId>

                    <artifactId>log4j-slf4j-impl</artifactId>

                </exclusion>

            </exclusions>

        </dependency>

        <dependency>

            <groupId>org.apache.jmeter</groupId>

            <artifactId>ApacheJMeter_java</artifactId>

            <version>${jmeter.version}</version>

            <exclusions>

                <exclusion>

                    <groupId>org.apache.logging.log4j</groupId>

                    <artifactId>log4j-slf4j-impl</artifactId>

                </exclusion>

            </exclusions>

        </dependency>

 

2 Java生成JMeter的jmx配置文件

2.1加载配置文件

使用静态块加载JMeter配置文件

 

static{

        {

            JMeterUtils.setJMeterHome(new File("G:\\配置软件\\压测工具\\JMeter\\apache-jmeter-5.1.1").getAbsolutePath());

            JMeterUtils.loadJMeterProperties(new File("G:\\配置软件\\压测工具\\JMeter\\apache-jmeter-5.1.1\\bin/jmeter.properties").getAbsolutePath());

            JMeterUtils.setProperty("saveservice_properties", "G:\\配置软件\\压测工具\\JMeter\\apache-jmeter-5.1.1\\bin/saveservice.properties");

            JMeterUtils.setProperty("user_properties", "G:\\配置软件\\压测工具\\JMeter\\apache-jmeter-5.1.1\\bin/user.properties");

            JMeterUtils.setProperty("upgrade_properties", "G:\\配置软件\\压测工具\\JMeter\\apache-jmeter-5.1.1\\bin/upgrade.properties");

            JMeterUtils.setProperty("system_properties", "G:\\配置软件\\压测工具\\JMeter\\apache-jmeter-5.1.1\\bin/system.properties");

           JMeterUtils.setProperty("proxy.cert.directory", new File("").getAbsolutePath());

            JMeterUtils.setLocale(Locale.SIMPLIFIED_CHINESE);

            JMeterUtils.initLocale();

 

        }

    }

 

2.2 生成jmx配置文件

2.2.1 新建TestPlan

//TestPlan

        TestPlan testPlan = new TestPlan("Test Plan");

        testPlan.setFunctionalMode(false);

        testPlan.setSerialized(false);

        testPlan.setTearDownOnShutdown(true);

        testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());

        testPlan.setProperty(TestElement.GUI_CLASS,"TestPlanGui");

        testPlan.setProperty(new BooleanProperty(TestElement.ENABLED, true));

        testPlan.setProperty(new StringProperty("TestPlan.comments", ""));

        testPlan.setProperty(new StringProperty("TestPlan.user_define_classpath", ""));

        Arguments arguments = new Arguments();

        testPlan.setProperty(new TestElementProperty("TestPlan.user_defined_variables",arguments));

 

对应JMeter_GUI:

 

2.2.2 新建ThreadGroup

对应设置请求次数、线程数等

        ThreadGroup threadGroup = new ThreadGroup();

        threadGroup.setNumThreads(1);

        threadGroup.setRampUp(1);

        threadGroup.setDelay(0);

        threadGroup.setDuration(0);

        threadGroup.setProperty(new StringProperty(ThreadGroup.ON_SAMPLE_ERROR, "continue"));

        threadGroup.setScheduler(false);

        threadGroup.setName("Group1");

        threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());

        threadGroup.setProperty(TestElement.GUI_CLASS,"ThreadGroup");

        threadGroup.setProperty(new BooleanProperty(TestElement.ENABLED, true));

 

对应JMeter_GUI:

2.2.3 新建HTTP Request

主要设置请求IP、请求端口、请求类型、请求参数

HTTPSamplerProxy httpSamplerProxy = new HTTPSamplerProxy();

        Arguments HTTPsamplerArguments = new Arguments();

        HTTPArgument httpArgument = new HTTPArgument();

        httpArgument.setProperty(new BooleanProperty("HTTPArgument.always_encode",false));

        httpArgument.setProperty(new StringProperty("Argument.value", request));

        httpArgument.setProperty(new StringProperty("Argument.metadata","="));

        ArrayList<TestElementProperty> list1 = new ArrayList<>();

        list1.add(new TestElementProperty("",httpArgument));

        HTTPsamplerArguments.setProperty(new CollectionProperty("Arguments.arguments",list1));

        httpSamplerProxy.setProperty(new TestElementProperty("HTTPsampler.Arguments",HTTPsamplerArguments));

        httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.domain", url));

        httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.port", port));

        httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.protocol", "http"));

        httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.path", api));

        httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.method", "POST"));

        httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.contentEncoding", ""));

        httpSamplerProxy.setProperty(new BooleanProperty("HTTPSampler.follow_redirects", true));

        httpSamplerProxy.setProperty(new BooleanProperty("HTTPSampler.postBodyRaw", true));

        httpSamplerProxy.setProperty(new BooleanProperty("HTTPSampler.auto_redirects", false));

        httpSamplerProxy.setProperty(new BooleanProperty("HTTPSampler.use_keepalive", true));

        httpSamplerProxy.setProperty(new BooleanProperty("HTTPSampler.DO_MULTIPART_POST", false));

        httpSamplerProxy.setProperty(new StringProperty("TestElement.gui_class", "org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui"));

        httpSamplerProxy.setProperty(new StringProperty("TestElement.test_class", "org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy"));

        httpSamplerProxy.setProperty(new StringProperty("TestElement.name", "HTTP Request"));

        httpSamplerProxy.setProperty(new StringProperty("TestElement.enabled", "true"));

        httpSamplerProxy.setProperty(new BooleanProperty("HTTPSampler.postBodyRaw", true));

        httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.embedded_url_re", ""));

        httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.connect_timeout", ""));

        httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.response_timeout", ""));

 

对应JMeter_GUI:

 

2.2.4新建请求header

        Header header = new Header();

        header.setProperty(new StringProperty("Header.name","Content-Type"));

        header.setProperty(new StringProperty("Header.value","application/json"));

        TestElementProperty HeaderElement = new TestElementProperty("",header);

        list2.add(HeaderElement);

        headerManager.setProperty(new CollectionProperty("HeaderManager.headers",list2));

        headerManager.setProperty(new StringProperty("TestElement.test_class","org.apache.jmeter.protocol.http.control.HeaderManager"));

        headerManager.setProperty(new StringProperty("TestElement.name","HTTP Header Manager"));

        headerManager.setProperty(new StringProperty("TestElement.enabled","true"));

        headerManager.setProperty(new StringProperty("TestElement.gui_class","org.apache.jmeter.protocol.http.gui.HeaderPanel"));

 

对应JMeter_GUI:

 

2.2.5 生成jmx文件

        ListedHashTree hashTreeResultCollectorAndHeaderManager = new ListedHashTree();

        hashTreeResultCollectorAndHeaderManager.add(resultCollector);

        hashTreeResultCollectorAndHeaderManager.add(headerManager);

 

        ListedHashTree hashTreeHTTPSamplerProxy = new ListedHashTree();

        hashTreeHTTPSamplerProxy.add(httpSamplerProxy,hashTreeResultCollectorAndHeaderManager);

 

        ListedHashTree hashTreeThreadGroup = new ListedHashTree();

        hashTreeThreadGroup.add(threadGroup,hashTreeHTTPSamplerProxy);

 

 

        ListedHashTree hashTreeTestPlan = new ListedHashTree();

        hashTreeTestPlan.add(testPlan,hashTreeThreadGroup);

 

        SaveService.saveTree(hashTreeTestPlan, new FileOutputStream("F:\\test.jmx"));

3 Java执行jmx文件

两种方式实现

3.1 调用cmd

    @Test

    public  void testCmd() throws  IOException {

        String command = "G:\\配置软件\\压测工具\\JMeter\\apache-jmeter-5.1.1\\bin\\jmeter -n -t F:\\test.jmx  -l F:\\test.jtl";

        Runtime.getRuntime().exec("cmd.exe /C start " + command);

    }

 

执行结果:

  1. 唤起windows的cmd。(linux同理也可以使用命令行执行JMeter)

 

     2. 执行jmx文件,并调用

 

3.2 使用JMeter_Java调用

 

    @Test

    public  void test() throws  IOException {

        File file = new File("F:\\test.jmx");

        HashTree hashTree = SaveService.loadTree(file);

        JMeterEngine engine = new StandardJMeterEngine();

        engine.configure(hashTree);

        ((StandardJMeterEngine) engine).run();

    }

 

执行结果:

 

  • 6
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
根据提供的引用内容,jmeter报错java.lang.reflect.InvocationTargetException通常是由于测试脚本中的某个方法调用引发了异常。该异常是java.lang.reflect.InvocationTargetException,它表示在调用方法时发生了异常,并且同时可能伴随着空指针异常。 为了解决这个问题,你可以尝试以下方法: 1. 检查测试脚本中的方法调用:查看测试脚本中的方法调用,确保方法的参数和返回值类型与预期一致。如果方法调用的参数有误或者方法不存在,可能会导致InvocationTargetException异常。 2. 检查空指针异常:空指针异常可能是InvocationTargetException异常的原因之一。你可以通过查看异常堆栈信息,找到引发空指针异常的代码行,并检查该行代码中是否存在空指针引用。 3. 异常处理:在测试脚本中,你可以使用try-catch语句来捕获InvocationTargetException异常,并进行相应的处理。例如,你可以在catch块中打印异常信息或者采取其他适当的措施来处理异常情况。 下面是一个示例代码,演示了如何处理InvocationTargetException异常: ```java try { // 调用方法 someMethod(); } catch (InvocationTargetException e) { // 捕获InvocationTargetException异常 Throwable cause = e.getCause(); if (cause instanceof NullPointerException) { // 处理空指针异常 System.out.println("空指针异常:" + cause.getMessage()); } else { // 其他异常处理 System.out.println("其他异常:" + cause.getMessage()); } } ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值