JAVA执行JMeter脚本

目标:

实现JAVA调用JMeter脚本生成HTML报告


步骤:

  1. JMeter客户端生成JMX脚本文件
    <?xml version="1.0" encoding="UTF-8"?>
    <jmeterTestPlan version="1.2" properties="5.0" jmeter="5.1.1 r1855137">
      <hashTree>
        <TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="测试计划" enabled="true">
          <stringProp name="TestPlan.comments"></stringProp>
          <boolProp name="TestPlan.functional_mode">false</boolProp>
          <boolProp name="TestPlan.tearDown_on_shutdown">true</boolProp>
          <boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
          <elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="用户定义的变量" enabled="true">
            <collectionProp name="Arguments.arguments"/>
          </elementProp>
          <stringProp name="TestPlan.user_define_classpath"></stringProp>
        </TestPlan>
        <hashTree>
          <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="线程组" enabled="true">
            <stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
            <elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="循环控制器" enabled="true">
              <boolProp name="LoopController.continue_forever">false</boolProp>
              <stringProp name="LoopController.loops">1</stringProp>
            </elementProp>
            <stringProp name="ThreadGroup.num_threads">1</stringProp>
            <stringProp name="ThreadGroup.ramp_time">1</stringProp>
            <boolProp name="ThreadGroup.scheduler">false</boolProp>
            <stringProp name="ThreadGroup.duration"></stringProp>
            <stringProp name="ThreadGroup.delay"></stringProp>
          </ThreadGroup>
          <hashTree>
            <HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="HTTP请求" enabled="true">
              <elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="用户定义的变量" enabled="true">
                <collectionProp name="Arguments.arguments"/>
              </elementProp>
              <stringProp name="HTTPSampler.domain">127.1.0.0</stringProp>
              <stringProp name="HTTPSampler.port">80</stringProp>
              <stringProp name="HTTPSampler.protocol">http</stringProp>
              <stringProp name="HTTPSampler.contentEncoding"></stringProp>
              <stringProp name="HTTPSampler.path">/masterl/jmeter/testPlan?content=That is Test Plan!</stringProp>
              <stringProp name="HTTPSampler.method">GET</stringProp>
              <boolProp name="HTTPSampler.follow_redirects">true</boolProp>
              <boolProp name="HTTPSampler.auto_redirects">false</boolProp>
              <boolProp name="HTTPSampler.use_keepalive">true</boolProp>
              <boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp>
              <stringProp name="HTTPSampler.embedded_url_re"></stringProp>
              <stringProp name="HTTPSampler.connect_timeout"></stringProp>
              <stringProp name="HTTPSampler.response_timeout"></stringProp>
            </HTTPSamplerProxy>
            <hashTree/>
          </hashTree>
        </hashTree>
      </hashTree>
    </jmeterTestPlan>

  2. JAVA调用JMeter脚本执行目标JMX脚本文件
    public static RunRespose runJMX(String jmxPath) {
            // 获取当前时间戳
            String s = getTime();
            String replay_result_path = jmeterJmxJtlPath + File.separator + s;
            File f_1 = new File(replay_result_path);
            if (!f_1.exists()) {
                f_1.mkdirs();
            }
            String html_path = jmeterJmxHtmlPath + File.separator + s;
            File f_2 = new File(html_path);
            if (!f_2.exists()) {
                f_2.mkdirs();
            }
            try {
                StringBuilder command = new StringBuilder();
                //此代码块是为了Windows环境正常执行,研发环境可忽略
                if (Objects.equals(File.separator, "\\")) {
                    command.append("cmd.exe /c ");
                }
                command.append(apacheJmeterPath).append(File.separator)
                        .append("jmeter -n -t ").append(jmxPath).append(" -l ")
                        .append(replay_result_path).append(File.separator).append("replay_result.jtl").append(" -e -o ")
                        .append(html_path);
                logger.info("{}", command.toString());
                // 等待执行完成
                Runtime.getRuntime().exec(command.toString()).waitFor();
            } catch (Exception e) {
                return new RunRespose();
            }
            String hp = html_path + File.separator + "index.html";
            String vp = File.separator + s + File.separator + "index.html";
            return new RunRespose(hp, vp);
        }

    将需要执行的JMX脚本文件地址作为参数传入方法中;

  3. 上方代码可以简单的看做成Shell脚本
    /datafs/JMeter/apache-jmeter/bin/jmeter -n -t /datafs/JMeter/jmx/dbApi.jmx -l /datafs/JMeter/test.jtl  -e -o /datafs/JMeter/HTML
  4. 最终结果


    HTML目录中的index.html文件即为执行结果文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值