作者:a126128 | 来源:互联网 | 2023-08-27 16:40
所需安装软件及环境: jmeter(配置环境变量); ant(配置环境变量); tomcat; jenkins;
步骤: 1、Jmeter、Ant下载及配置; 2、Jmeter+ant执行脚本、生成报告; 3、jenkins构建自动化;
1、Jmeter、Ant下载及配置: (1)、 jmeter下载及配置:
传送门:Jmeter下载、配置、启动_win10
(2)、ant:
下载地址:http://ant.apache.org/bindownload.cgi
配置环境变量:
ANT_HOME: E:\Ant\apache-ant-1.10.7-bin\apache-ant-1.10.7
Path: %ANT_HOME%\bin
查看是否安装成功:
cd E:\Ant\apache-ant-1.10.7-bin\apache-ant-1.10.7\bin
$ ant -version
2、Jmeter+ant执行脚本、生成报告: (1)、在Jmeter的根目录下,新建一文件夹TestCase,并在该文件夹下新建build.xml文件、report文件夹,以及编写任意脚本testAnt.jmx:
(2)、修改Jmeter,bin目录下jmeter.properties文件中jmeter.save.saveservice.output_format=csv为jmeter.save.saveservice.output_format=xml,并去掉注释(原因: jmeter执行结果文件默认保存的是csv格式,不是xml格式,csv无法转化成html格式):
(3)、将Jmeter的extras文件夹下的ant-jmeter-1.1.1.jia包放置到Ant\lib下;
(4)、编辑build.xml,将内容复制到文件内,并修改相应的安装位置:
< ? xml version&#61; "1.0" encoding&#61; "UTF-8" ? > < project name&#61; "ant-jmeter-test" default &#61; "run" basedir&#61; "." > < format property&#61; "time" pattern&#61; "yyyyMMdd_hhmm" / > < ! -- 需要改成自己本地的 Jmeter 目录-- > < property name&#61; "jmeter.home" value&#61; "E:\jmeter\apache-jmeter-5.1.1\apache-jmeter-5.1.1" / > < ! -- jmeter生成jtl格式的结果报告的路径-- > < property name&#61; "jmeter.result.jtl.dir" value&#61; "E:\jmeter\apache-jmeter-5.1.1\apache-jmeter-5.1.1\TestCase\report\jtl" / > < ! -- jmeter生成html格式的结果报告的路径-- > < property name&#61; "jmeter.result.html.dir" value&#61; "E:\jmeter\apache-jmeter-5.1.1\apache-jmeter-5.1.1\TestCase\report\html" / > < ! -- 生成的报告的前缀-- > < property name&#61; "ReportName" value&#61; "TestReport" / > < property name&#61; "jmeter.result.jtlName" value&#61; "${jmeter.result.jtl.dir}/${ReportName}${time}.jtl" / > < property name&#61; "jmeter.result.htmlName" value&#61; "${jmeter.result.html.dir}/${ReportName}${time}.html" / > < target name&#61; "run" > < antcall target&#61; "test" / > < antcall target&#61; "report" / > < target name&#61; "test" > < taskdef name&#61; "jmeter" classname&#61; "org.programmerplanet.ant.taskdefs.jmeter.JMeterTask" / > < jmeter jmeterhome&#61; "${jmeter.home}" resultlog&#61; "${jmeter.result.jtlName}" > < ! -- 声明要运行的脚本。"*.jmx" 指包含此目录下的所有jmeter脚本-- > < testplans dir&#61; "E:\jmeter\apache-jmeter-5.1.1\apache-jmeter-5.1.1\TestCase" includes&#61; "testAnt.jmx" / > < property name&#61; "jmeter.save.saveservice.output_format" value&#61; "xml" / > < path id&#61; "xslt.classpath" > < fileset dir&#61; "${jmeter.home}/lib" includes&#61; "xalan*.jar" / > < fileset dir&#61; "${jmeter.home}/lib" includes&#61; "serializer*.jar" / > < target name&#61; "report" > < format property&#61; "report.datestamp" pattern&#61; "yyyy/MM/dd HH:mm" / > < xslt classpathref&#61; "xslt.classpath" force&#61; "true" in&#61; "${jmeter.result.jtlName}" out&#61; "${jmeter.result.htmlName}" style&#61; "${jmeter.home}/extras/jmeter-results-detail-report_21.xsl" > < param name&#61; "dateReport" expression&#61; "${report.datestamp}" / > < ! -- 因为上面生成报告的时候&#xff0c;不会将相关的图片也一起拷贝至目标目录&#xff0c;所以&#xff0c;需要手动拷贝 -- > < copy todir&#61; "${jmeter.result.html.dir}" > < fileset dir&#61; "${jmeter.home}/extras" > < include name&#61; "collapse.png" / > < include name&#61; "expand.png" / >
&#xff08;5&#xff09;、运行ant&#xff1a;
cd E:\jmeter\apache-jmeter-5.1.1\apache-jmeter-5.1.1\TestCase
命令行运行&#xff1a;
$ ant
如有同样问题&#xff1a;NaN\color{red}{如有同样问题&#xff1a;NaN} 如 有 同 样 问 题 &#xff1a; N a N
解决&#xff1a;把Jmeter的lib包中把xalan−2.7.2.jar和serializer−2.7.2.jar复制到Ant的lib包中&#xff1a;\color{red}{解决&#xff1a;把Jmeter的lib包中把xalan-2.7.2.jar和 serializer-2.7.2.jar复制到Ant的lib包中&#xff1a;} 解 决 &#xff1a; 把 J m e t e r 的 l i b 包 中 把 x a l a n − 2 . 7 . 2 . j a r 和 s e r i a l i z e r − 2 . 7 . 2 . j a r 复 制 到 A n t 的 l i b 包 中 &#xff1a;
再次运行&#xff1a;\color{red}{再次运行&#xff1a;} 再 次 运 行 &#xff1a;
3、jenkins构建自动化&#xff1a; &#xff08;1&#xff09;、下载tomcat(https://tomcat.apache.org/download-80.cgi)
&#xff08;2&#xff09;、下载jenkins(https://jenkins.io/download/)&#xff0c;jenkins.war格式的文件
启动jenkins&#xff08;两种方式&#xff09;&#xff1a;
1&#xff09;、在当前目录java -jar jenkins.war
2&#xff09;、或将其放在tomcat解压目录下的webapps下面&#xff0c;进入tomcat\bin下双击startup.bat启动tomcat&#xff1b;
&#xff08;3&#xff09;、查看&#xff1a;http://localhost:8088/jenkins
输入默认密码&#xff08;查看密码&#xff1a;C:\Users\78755.jenkins\secrets\initialAdminPassword&#xff09;
&#xff08;4&#xff09;、创建管理员用户&#xff1a;
&#xff08;5&#xff09;、创建任务&#xff0c;选择创建一个自由风格的软件项目&#xff1a;
&#xff08;6&#xff09;、配置&#xff1a; 丢弃旧的构建&#xff0c;保持天数根据需要自行填写&#xff1a;
源码管理&#xff1a;无
构建&#xff1a;增加构建步骤Invoke Ant &#xff0c;高级&#xff0c;填写Bulid File&#xff1a;E:\jmeter\apache-jmeter-5.1.1\apache-jmeter-5.1.1\TestCase\build.xml&#xff08;填写自己文件放置的路径&#xff09;; 增加构建后操作步骤&#xff0c;选择插件&#xff1a;Publish html reports、Publish Performance test result report&#xff1b; 配置“Publish html reports”&#xff0c;HTML directory to archive填写html报告保存的路径&#xff1a; 配置“Publish Performance test result report”&#xff0c;Source data files(autodetects format)填写jtl格式报告的保存路径&#xff1a;E:\jmeter\apache-jmeter-5.1.1\apache-jmeter-5.1.1\TestCase\report\jtl*.jtl其余参数根据自行需要进行调整&#xff0c;我这里使用默认值&#xff0c;暂不做修改&#xff1b; 保存&#xff0c;配置生效&#xff1b;
立即构建&#xff1a;
构建中&#xff1a; 构建成功历史构建前侧图标为蓝色&#xff08;失败则为红色&#xff09;&#xff0c;可查看测试报告、历史构建以及点进去每一次构建详情&#xff1a; 查看控制台输出&#xff1a;
在本地存放测试报告的文件夹亦可查看&#xff1a;E:\jmeter\apache-jmeter-5.1.1\apache-jmeter-5.1.1\TestCase\report\html