19.JMeter + Ant + Jenkins自动化


目录

1、JMeter自动化环境准备

2、jtl文件配置

3、报告优化

4、Jenkins

5、效果


1、JMeter自动化环境准备

  • 1)JDK环境配置
  • 2)JMeter安装
  • jmeter脚本xxx.jmx文件放到与build.xml文件一个目录
  • build.xml是ant的构建文件
  • 修改jmeter.properties文件
  • jmeter.save.saveservice.output_format=xml
  • 3)Ant环境配置
    #    ant包解压后,需要配置环境变量
    $ vim .zshrc
    
    export ANT_HOME="/Users/xxx/Software/apache-ant-1.10.12"
    export PATH="$PATH:${ANT_HOME}/bin”
    
    #    命令行执行让配置生效
    source .zshrc即可
    
    #    验证环境变量是否配置正确
    ant -version
  • 4)Jenkins安装
  • 5)其他配置:
    • a)需要在JMeter目录下新建一个文件夹TestCase【文件夹名称不要使用下划线,空格字符等】,并将测试脚本放入这个文件夹
    • b)将JMeter的extras文件夹中的ant-jmeter-1.1.1.jar放入Ant的lib文件夹中
    • c)将JMeter的extras文件夹中的jmeter-results-detail-report_21.xsl,build.xml,collapse.png,expand.png放入Ant的bin目录下
  • 6)build.xml配置
    • 在Ant的bin目录中打开build.xml文件,找到以下内容
<?xml version="1.0" encoding="UTF-8"?>
<project name="ant-jmeter-test" default="run" basedir=".">
    <tstamp>
        <format property="time" pattern="yyyyMMddhhmm" />
    </tstamp>
    <!-- 需要改成自己本地的 Jmeter 目录-->
    <property name="jmeter.home" value="/Users/xxx/Software/apache-jmeter-5.4.1" />
    <!-- jmeter生成jtl格式的结果报告的路径-->
    <property name="jmeter.result.jtl.dir" value="/Users/xxx/xxx_test/ant_file/jtl" />
    <!-- jmeter生成html格式的结果报告的路径-->
    <property name="jmeter.result.html.dir" value="/Users/xxx/xxx_test/ant_file/html" />
    <!-- 生成的报告的前缀-->
    <property name="ReportName" value="TestReport" />
    <property name="jmeter.result.jtlName" value="${jmeter.result.jtl.dir}/${ReportName}${time}.jtl" />
    <property name="jmeter.result.htmlName" value="${jmeter.result.html.dir}/${ReportName}${time}.html" />
    <path id="xslt.classpath">
        <fileset dir="${jmeter.home}/lib" includes="xalan*.jar"/>
        <fileset dir="${jmeter.home}/lib" includes="serializer*.jar"/>
    </path>
    <target name="run">
        <antcall target="test" />
        <antcall target="report" />
    </target>
    <target name="test">
        <taskdef name="jmeter" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask" />
        <jmeter jmeterhome="${jmeter.home}" resultlog="${jmeter.result.jtlName}">
            <!-- 声明要运行的脚本。"*.jmx"指包含此目录下的所有jmeter脚本-->
            <testplans dir="/Users/xxx/xxx_test/ant_file" includes="*.jmx" />
            <property name="jmeter.save.saveservice.output_format" value="xml"/>
        </jmeter>
    </target>
    <target name="report">
        <tstamp> <format property="report.datestamp" pattern="yyyy/MM/dd HH:mm" /></tstamp>
        <xslt classpathref="xslt.classpath"
              force="true"
              in="${jmeter.result.jtlName}"
              out="${jmeter.result.htmlName}"
              style="${jmeter.home}/extras/jmeter-results-detail-report_21.xsl">
            <param name="dateReport" expression="${report.datestamp}"/>
        </xslt>
        <copy todir="${jmeter.result.html.dir}">
            <fileset dir="${jmeter.home}/extras">
                <include name="collapse.png" />
                <include name="expand.png" />
            </fileset>
        </copy>
    </target>
</project>
  • 需要修改的参数说明:
    • jmeter.home, Jmeter目录路径
    • jmeter.result.jtl.dir, jmeter生成jtl格式的结果文件的路径
    • report.title, 测试报告的标题
    • jmeter.result.html.dir, jmeter生成jtl格式的 结果报告的路径
# xxx @ xxxdeMacBook-Pro in ~/xxx_test/ant_file [8:25:26]
$ ls -lR
total 88
-rw-r--r--@ 1 xxx  staff   2424  9 13 08:24 build.xml
drwxr-xr-x  4 xxx  staff    128  9 13 08:25 html
-rw-r--r--  1 xxx  staff  40231  9 13 08:07 httpbin.jmx
drwxr-xr-x  2 xxx  staff     64  9 13 08:21 jtl

./html:
total 16
-rw-r--r--  1 xxx  staff  121  3 10  2022 collapse.png
-rw-r--r--  1 xxx  staff  126  3 10  2022 expand.png

./jtl:

# xxx @ xxxdeMacBook-Pro in ~/xxx_test/ant_file [8:25:30]
$ ant
Buildfile: /Users/xxx/xxx_test/ant_file/build.xml

run:

test:
   [jmeter] Executing test plan: /Users/xxx/xxx_test/ant_file/httpbin.jmx ==> /Users/xxx/xxx_test/ant_file/jtl/TestReport202209130825.jtl
   [jmeter] Creating summariser <summary>
   [jmeter] Created the tree successfully using /Users/xxx/xxx_test/ant_file/httpbin.jmx
   [jmeter] Starting standalone test @ Tue Sep 13 08:25:57 CST 2022 (1663028757056)
   [jmeter] Waiting for possible Shutdown/StopTestNow/HeapDump/ThreadDump message on port 4445
   [jmeter] summary +      9 in 00:00:03 =    3.2/s Avg:   288 Min:     1 Max:   630 Err:     0 (0.00%) Active: 1 Started: 1 Finished: 0
   [jmeter] summary +      7 in 00:00:03 =    2.1/s Avg:   353 Min:   264 Max:   583 Err:     1 (14.29%) Active: 0 Started: 1 Finished: 1
   [jmeter] summary =     16 in 00:00:06 =    2.6/s Avg:   317 Min:     1 Max:   630 Err:     1 (6.25%)
   [jmeter] Tidying up ...    @ Tue Sep 13 08:26:03 CST 2022 (1663028763305)
   [jmeter] ... end of run

report:
     [xslt] Processing /Users/xxx/xxx_test/ant_file/jtl/TestReport202209130825.jtl to /Users/xxx/xxx_test/ant_file/html/TestReport202209130825.html
     [xslt] Loading stylesheet /Users/xxx/Software/apache-jmeter-5.4.1/extras/jmeter-results-detail-report_21.xsl

BUILD SUCCESSFUL
Total time: 8 seconds

# xxx @ xxxdeMacBook-Pro in ~/xxx_test/ant_file [8:26:04]
$

2、jtl文件配置

  • 1)打开测试计划
  • 2)在测试计划下,第一层子级添加"查看结果树"
  • 3)在查看结果树,需要设置将所有的数据写入一个文件
  • 4)命令行运行
    •  ant -buildfile build.xml

3、报告优化

  • 进入JMeter的bin目录,打开jmeter.properties文件。
  • 然后将以下代码取消注释状态,并改为true
  • 从data_type 到 idle_time
507 #jmeter.save.saveservice.data_type=true
508 #jmeter.save.saveservice.label=true
509 #jmeter.save.saveservice.response_code=true
510 # response_data is not currently supported for CSV output
511 #jmeter.save.saveservice.response_data=false
512 # Save ResponseData for failed samples
513 #jmeter.save.saveservice.response_data.on_error=false
514 #jmeter.save.saveservice.response_message=true
515 #jmeter.save.saveservice.successful=true
516 #jmeter.save.saveservice.thread_name=true
517 #jmeter.save.saveservice.time=true
518 #jmeter.save.saveservice.subresults=true
519 #jmeter.save.saveservice.assertions=true
520 #jmeter.save.saveservice.latency=true
521 # Only available with HttpClient4
522 #jmeter.save.saveservice.connect_time=true
523 #jmeter.save.saveservice.samplerData=false
524 #jmeter.save.saveservice.responseHeaders=false
525 #jmeter.save.saveservice.requestHeaders=false
526 #jmeter.save.saveservice.encoding=false
527 #jmeter.save.saveservice.bytes=true
528 # Only available with HttpClient4
529 #jmeter.save.saveservice.sent_bytes=true
530 #jmeter.save.saveservice.url=true
531 #jmeter.save.saveservice.filename=false
532 #jmeter.save.saveservice.hostname=false
533 #jmeter.save.saveservice.thread_counts=true
534 #jmeter.save.saveservice.sample_count=false
535 #jmeter.save.saveservice.idle_time=true

4、Jenkins

  • 1)插件安装
    • ant
    • html
    • 其他的默认安装即可
  • 2)配置自动化测试的Job任务

5、效果


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值