JMeter 系列之—-04 支持CI

任何工具要集成到CI,最简洁的思路如下:

  1. 工具支持用命令行启动并执行;这里不管是其自有的命令行,还是借用其他工具(如ant,newman等)的命令行;
  2. 执行结果生成可读的报告,可显示。

针对Jmeter支持CI,首先来补充下知识。

一、知识补充

1.1 命令行执行Jmeter

jmeter -n -t \<testplan filename\> -l \<listener filename\>[官方介绍](https://jmeter.apache.org/usermanual/get-started.html#non_gui)

示例: jmeter -n -t testplan.jmx -l test.jtl -j /usr/local/jmeter/log/test.log -e -o web

参数介绍如下:
-h, –help -> prints usage information and exit
-n, –nongui -> This specifies JMeter is to run in cli mode
-t, –testfile <argument> -> [name of JMX file that contains the Test Plan].
-l, –jtlfile <argument> -> [name of JTL file to log sample results to].
-j, –jmeter logfile -> [name of JMeter run log file].
-R, –runremote -> [list of remote servers] Run the test in the specified remote servers. 在Jmter.properties文件中指定的所有远程服务器
-g, [path to CSV file] generate report dashboard only
-e, generate report dashboard after load test. 测试结束后,生成测试报告
-o, output folder where to generate the report dashboard after load test. Folder must not exist or be empty. 指定测试报告的存放位置

-H, –proxyHost <argument> -> 设置 JMeter 使用的代理主机
-P, –proxyPort <argument> -> 设置 JMeter 使用的代理主机的端口号

1.1 ant命令支持Jmeter

Jmeter也支持ant命令运行,参数说明如下,具体实现方案在后面。

参数 说明 REQUIRED
jmeterhome JMeter install location. Yes
testplan The location of the test plan file. Either testplan or testplans
resultlog The location of the result log file. Either resultlog or resultlogdir
resultlogdir The directory to place result log files. When used, result log file names will match the test plan files names, with the extension renamed from .jmx to .jtl. Either resultlog or resultlogdir
failureproperty The name of a property to set to "true" in the event of a test plan failure. No
jmeterproperties The location of an alternate jmeter.properties file to use. No
jmeterlogfile The location of the JMeter log file. No
runremote If "true", runs remote servers specified in jmeter.properties. Default is "false". No
proxyhost Host name of a proxy to run the tests through. No
proxyport Port of the proxy host specified. No
proxyuser Username of the proxy host specified. No
proxypass Password of the proxy host specified. No

另用到了xsltproc这个工具,将xml格式的测试报告按照指定的模板,转成html。其实jmeter本身自带html测试报告也挺强大的,够用的话可不考虑外部工具。

二、具体实现

2.1 实现方案1(Jenkins + Jmeter+ xsltproc)

  1. 修改jmeter.properties中,jmeter.save.saveservice.output_format=xml

  2. 安装xsltproc:下载并解压到非中文目录----xsltproc是一个快速的XSLT引擎,通过XSL层叠样式表把XML转化为HTML、PDF等格式的文件

  3. 命令执行jmeter脚本,具体如下

批处理命令---------------------------------------------------------------------

@echo off

rem 配置地址
set jmxPath="D:\java\apache-jmeter-space"
set jmeterPath="D:\Java\apache-jmeter-5.2.1"
set jobName=test

rem 执行Jmeter
d:
cd %jmeterPath%\bin
jmeter -n -t %jmxPath%\script\%jobName%.jmx -l %jmxPath%\script\%jobName%.jtl -j %jmxPath%\script\%jobName%.log 

rem pause
@echo off

rem 生成当前日期
for /f "tokens=1,2,3 delims=/- " %%a in ("%date%") do @set D=%%a%%b%%c
for /f "tokens=1,2,3 delims=:." %%a in ("%time%") do @set T=%%a%%b%%c
set T=%T: =0%
rem echo 显示输出日期时间字符串
set d=%D%%T%
echo 当前时间: %d%

rem 配置地址
set jmxPath="D:\java\apache-jmeter-space"
set jmeterPath="D:\Java\apache-jmeter-5.2.1"
set jobName=test
set resultPath=%jobName%_%d%

rem 创建日期文件夹
mkdir %jmxPath%\%resultPath%

rem 剪切日志
move %jmxPath%\script\%jobName%.jtl %jmxPath%\%resultPath%\
copy %jmxPath%\script\%jobName%.log %jmxPath%\%resultPath%\
copy %jmeterPath%\extras\collapse.png %jmxPath%\%resultPath%
copy %jmeterPath%\extras\expand.png %jmxPath%\%resultPath%

rem 生成监听器截图
call java -jar %jmeterPath%\lib\ext\CMDRunner.jar --tool Reporter --generate-png %jmxPath%\%resultPath%\ResponseTimesOverTime.png --input-jtl %jmxPath%\%resultPath%\%jobName%.jtl --plugin-type ResponseTimesOverTime
call java -jar %jmeterPath%\lib\ext\CMDRunner.jar --tool Reporter --generate-png %jmxPath%\%resultPath%\CPUMemory.png --input-jtl %jmxPath%\%resultPath%\%jobName%.jtl --plugin-type PerfMon

rem 执行xsltproc
%jmeterPath%\bin\xsltproc %jmeterPath%\extras\jmeter-results-detail-report_21.xsl %jmxPath%\%resultPath%\%jobName%.jtl > %jmxPath%\%resultPath%\%jobName%_total.html

copy %jmxPath%\%resultPath%\* %WORKSPACE%\

rem pause

linux shell命令--------------------------------------------------------------------------

#1). 清理jtl,log,html报告

#2). jmeter命令
jmxPath="/data/jmeter/jmeter_load_test"

/data/jmeter/apache-jmeter-5.2.1/bin/jmeter.sh  -n -t /data/jmeter/script/test.jmx -l test.jtl -j $jmxPath/result_${BUILD_NUMBER}.log -e -o $jmxPath/result_${BUILD_NUMBER}

#3). 脚本执行完成,若单元测试有失败的用例,则可以用shell脚本终止构建继续执行
#!/bin/bash
dtime=$(date "+%Y%m%d%H%M")
echo "找到失败的用例:"
grep -c " <failure>true</failure>" ${WORKSPACE}/test.jtl
if [ $? = 0 ]
then 
  exit 1
else
  exit 0
fi

2.2 方案2(Jenkins + Jmeter + Ant + xsltproc)

  1. 把jmeter的extras目录下的ant-jmeter-1.1.1.jar复制到ant文件夹的lib目录下

  2. jmeter的extras目录下有官方推介的build.xml文件,git当然我们也可以自己新建,内容如下

<?xml version="1.0" encoding="utf-8"?>
<project name="jmeter_test" default="run" basedir="."> 
  <tstamp> 
    <format property="time" pattern="yyyyMMddhhmm"/> 
  </tstamp>  
  <!-- 需要改成自己本地的 Jmeter 目录-->  
  <property name="jmeter.home" value="D:\java\apache-jmeter-5.2.1"/>  
  <!-- jmeter生成jtl格式的结果报告的路径-->  
  <property name="jmeter.space.dir" value="D:\java\apache-jmeter-space"/>  
  <!-- 生成的报告的前缀 -->  
  <property name="JobName" value="test"/>  
  <property name="jmeter.result.jtlName"  value="${jmeter.space.dir}/${JobName}_${time}/${JobName}.jtl"/>  
  <property name="jmeter.result.htmlName" value="${jmeter.space.dir}/${JobName}_${time}/${JobName}.html"/> 
  <property name="jmeter.result.html.dir" value="${jmeter.space.dir}/${JobName}_${time}/"/> 
  <target name="run"> 
    <antcall target="test"/>  
    <antcall target="report"/> 
  </target>  
  <target name="test"> 
      <copy todir="${jmeter.space.dir}/${JobName}_${time}/script"> 
      <fileset dir="script"> 
        <include name="*.jmx"/>  
      </fileset> 
    </copy> 
	
    <taskdef name="jmeter" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask"/>  
    <jmeter jmeterhome="${jmeter.home}" resultlog="${jmeter.result.jtlName}"> 
      <!-- 声明要运行的脚本。"*.jmx"指包含此目录下的所有jmeter脚本 -->  
      <testplans dir="${jmeter.space.dir}/${JobName}_${time}/script" includes="${JobName}.jmx"/>  
      <property name="jmeter.save.saveservice.output_format" value="xml"/> 
      <!-- 加入jmeter的参数,这样可以根据需要生成jmeter的CSV报告,需要设置 jmeter.save.saveservice.output_format的value=csv
      <jmeterarg value="-e" />
      <jmeterarg value="-o${jmeter.result.html.dir}" />
      -->
    </jmeter> 
  </target>  
  <path id="xslt.classpath"> 
    <fileset dir="${jmeter.home}/lib" includes="xalan*.jar"/>  
    <fileset dir="${jmeter.home}/lib" includes="serializer*.jar"/> 
  </path>  
  <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.space.dir}/${JobName}_${time}/"> 
      <fileset dir="${jmeter.home}/extras"> 
        <include name="collapse.png"/>  
        <include name="expand.png"/> 
      </fileset> 
    </copy> 
  </target> 
</project>

剩余要做的就是配置jenkins job,配置构建步骤,构建后步骤--发布测试报告,将测试息结果展示出来

注意:
默认的报告比较丑,可以在jenkins ->Manage Jenkins ->Script Console中运行如下命令优化(注意:这里执行命令重启jenkins会失效)
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")

posted @ 2020-05-21 23:22  雨 燕  阅读(263)  评论(0编辑  收藏  举报