一.jmeter编写脚本
二.SVN上创建脚本目录
data:参数化文件
result:执行脚本,产生的文件存放的地方
创建文件夹jtls>jtl 文件
创建文件夹logs>log文件
创建文件夹htmls>html 文件
script:Jmeter 脚本 .jmx文件
三.jenkins配置
1.创建自由风格任务
立即构建,jenkins工作目录下,有从svn拉取的Jmeter脚本项目
增加构建步骤
写shell,创建文件夹和执行jmeter脚本
写脚本
shell如下
echo "进入我的目录"
base_workspace=/var/lib/jenkins/workspace/JMeter_Test_Login/result
jmx_workspace=/var/lib/jenkins/workspace/JMeter_Test_Login/script
cd $base_workspace
echo "创建日志目录"
if [ ! -d "logs" ];then
mkdir logs
fi
echo "创建jtls目录"
if [ ! -d "jtls" ];then
mkdir jtls
fi
echo "创建htmls目录"
if [ ! -d "htmls" ];then
mkdir htmls
fi
echo "判断jtls/HQW-Supplier-Login.jtl是否存在,存在则删除之"
if [ -f "jtls/HQW-Supplier-Login.jtl" ];then
rm -rf jtls/*.jtl
fi
echo "判断htmls/HQW-Supplier-Login.html是否存在,存在则删除之"
if [ -d "htmls/HQW-Supplier-Login.html" ];then
rm -rf htmls/*.html
fi
echo "进入jmeter目录"
cd /usr/local/jmeter/apache-jmeter-5.1.1/bin
sh jmeter.sh -n -t $jmx_workspace/HQW-Supplier-Login.jmx -e -o $base_workspace/htmls/HQW-Supplier-Login.html -l $base_workspace/jtls/HQW-Supplier-Login.jtl -j $base_workspace/logs/HQW-Supplier-Login.log
优化报告Ant
服务器安装Ant
配置环境变量
export ANT_HOME=/usr/local/apache-ant-1.10.6
export PATH=$JAVA_HOME/bin:$ANT_HOME/bin:$PATH
将 jmeter的extras目录中ant-jmeter-1.1.1.jar包拷贝至ant安装目录下的lib目录中
将文件放入/usr/local/jmeter/apache-jmeter-5.1.1/extras中
echo "进入我的目录"
base_workspace=/var/lib/jenkins/workspace/JMeter_Test_Login/result
jmx_workspace=/var/lib/jenkins/workspace/JMeter_Test_Login/script
cd $base_workspace
echo "创建jtls目录"
if [ ! -d "jtls" ];then
mkdir jtls
fi
echo "创建htmls目录"
if [ ! -d "htmls" ];then
mkdir htmls
fi
echo "判断jtls/HQW-Supplier-Login.jtl是否存在,存在则删除之"
if [ -f "jtls/HQW-Supplier-Login.jtl" ];then
rm -rf jtls/*.jtl
fi
echo "判断htmls/HQW-Supplier-Login.html是否存在,存在则删除之"
if [ -f "htmls/HQW-Supplier-Login.html" ];then
rm -rf htmls/*.html
fi
,jenkins配置
/usr/local/jmeter/apache-jmeter-5.1.1/extras/build.xml
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="/usr/local/jmeter/apache-jmeter-5.1.1" />
<!-- 填写发件人的邮箱地址-->
<property name="username" value="975387401@qq.com"/>
<property name="password" value="如果使用QQ需要先在QQ邮箱设置里开启smtp服务,会获得一个授权码,此处填写那个授权码"/>
<!-- 填写发件人的邮箱地址-->
<property name="mail_from" value="975387401@qq.com"/>
<!-- 填写接收邮件人的邮箱地址-->
<property name="mail_to" value="975387401@qq.com"/>
<!-- 发送给哪些人,多个用逗号隔开-->
<property name="mail_to" value="767769407@qq.com"/>
-->
<!-- 抄送给哪些人,多个用逗号隔开/-->
<!--property name="mail_cc" value="++++1@163.com,++++2@163.com"/-->
<property name="report.title" value="接口测试报告"/>
<!-- jmeter生成jtl格式的结果报告的路径-->
<property name="jmeter.result.jtl.dir" value="/var/lib/jenkins/workspace/JMeter_Test_Login/result/jtls" />
<!-- jmeter生成html格式的结果报告的路径-->
<property name="jmeter.result.html.dir" value="/var/lib/jenkins/workspace/JMeter_Test_Login/result/htmls" />
<!-- 生成的报告的前缀-->
<property name="ReportName" value="HQW-Supplier-Login" />
<property name="detail" value="HQW-Supplier-Login-detail" />
<property name="jmeter.result.jtlName" value="${jmeter.result.jtl.dir}/${ReportName}.jtl" />
<property name="jmeter.result.htmlName" value="${jmeter.result.html.dir}/${ReportName}.html" />
<property name="jmeter.result.htmlNamedetail" value="${jmeter.result.html.dir}/${detail}${time}.html" />
<!--定义次build.xml要执行全部的任务-->
<target name="run">
<antcall target="test" />
<antcall target="report" />
<!--
<antcall target="mail" />
-->
</target>
<!--第一个任务,执行test-->
<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="/var/lib/jenkins/workspace/JMeter_Test_Login/script" includes="*.jmx" />
<property name="jmeter.save.saveservice.output_format" value="xml"/>
</jmeter>
</target>
<!--引入外部jar包-->
<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-shanhe-me.xsl">
<param name="dateReport" expression="${report.datestamp}"/>
</xslt>
<xslt
classpathref="xslt.classpath"
force="true"
in="${jmeter.result.jtlName}"
out="${jmeter.result.htmlNamedetail}"
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>
<!--将控制台输出到文本中-->
<!--
<record name="${jmeter.result.html.dir}/${ReportName}${time}.txt" loglevel="info" append="no" action="start"/>
-->
<!--执行第三个任务,发送邮件-->
<target name="mail">
<!--读取输出的控制台日志文件并写入邮件正文-->
<loadfile property="email_content" srcFile="${jmeter.result.html.dir}/${ReportName}${time}.txt" encoding="UTF-8"/>
<loadfile property="output" srcFile="${jmeter.result.html.dir}/${ReportName}${time}.html" encoding="UTF-8"/>
<!-- 此处配置SMTP服务器 POP3授权码 发送邮箱地址 端口 发送邮箱主题-->
<mail tolist="${mail_to}"
mailhost="smtp.qq.com"
mailport="465"
user="${username}"
password="${password}"
ssl="true"
subject="jmeter 测试报告 ${ReportName}${time}"
messagemimetype="text/html">
<from address="${mail_from}" />
<fileset dir="${jmeter.result.html.dir}">
<include name="${ReportName}${time}.html"/>
</fileset>
<message>
自动发送的测试报告
${ReportName}${time}
${output}
请勿回复
</message>
</mail>
</target>
</project>
--------------------------------------
进入/usr/local/jmeter/apache-jmeter-5.1.1/bin 修改jmeter.properties
改为:jmeter.save.saveservice.output_format=xml
jmeter.save.saveservice.data_type=true
jmeter.save.saveservice.label=true
jmeter.save.saveservice.response_code=true
# response_data is not currently supported for CSV output
jmeter.save.saveservice.response_data=true
# Save ResponseData for failed samples
jmeter.save.saveservice.response_data.on_error=true
jmeter.save.saveservice.response_message=true
jmeter.save.saveservice.successful=true
jmeter.save.saveservice.thread_name=true
jmeter.save.saveservice.time=true
jmeter.save.saveservice.subresults=true
jmeter.save.saveservice.assertions=true
jmeter.save.saveservice.latency=true
# Only available with HttpClient4
jmeter.save.saveservice.connect_time=true
jmeter.save.saveservice.samplerData=true
jmeter.save.saveservice.responseHeaders=true
jmeter.save.saveservice.requestHeaders=true
jmeter.save.saveservice.encoding=true
jmeter.save.saveservice.bytes=true
# Only available with HttpClient4
jmeter.save.saveservice.sent_bytes=true
jmeter.save.saveservice.url=true
jmeter.save.saveservice.filename=true
jmeter.save.saveservice.hostname=true
jmeter.save.saveservice.thread_counts=true
jmeter.save.saveservice.sample_count=true
#meter.save.saveservice.idle_time=true
# Timestamp format - this only affects CSV output files
# legitimate values: none, ms, or a format suitable for SimpleDateFormat
#jmeter.save.saveservice.timestamp_format=ms
jmeter.save.saveservice.timestamp_format=yyyy/MM/dd HH:mm:ss.SSS
sampleresult.default.encoding=utf-8