前提准备
1、maven项目
2、已安装JMeter、Jenkins、maven、jdk
环境要求:
jmeter>5.6.2
maven >3.9
jdk>1.8
Jenkins ?
备注:jmeter-maven-plugin 无需下载,可查阅相关地址:GitHub - jmeter-maven-plugin/jmeter-maven-plugin: The JMeter Maven Plugin
创建maven项目
打开ij编辑器,File-->New-->Project,选择Maven,点击next,输入Groupid跟ArtifactId(这个目前随便填就好)
添加jmeter相关文件
1、在maven项目,如果是模块项目的话,可以找一个模块即可,如api模块,在${project.base.directory}/src/test/创建文件夹录jmeter跟resources。(如果只有一个模块,同理)
jmeter文件夹内容存放来源:jmeter安装目录下bin文件下的配置文档+jmeter脚本(需要用来执行的jmeter脚本)
resource目录内容存文档来源:jmeter安装目录下extras文件
resources文件夹需要修改文件类型,在resources文件夹右键-Mark Directory as-test resources Root,如下
修改项目的pom
整个pom.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>Grit</groupId><artifactId>GritJmeter</artifactId><version>1.0-SNAPSHOT</version><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><!--此路径用于存储JMeter的原生测试结果文件,比如csv或者jtl--><jmeter.result.jtl.dir>${project.build.directory}\jmeter\results</jmeter.result.jtl.dir><!--此路径用于存储由模板转换过来的详细的测试结果的html文件--><jmeter.result.html.dir>${project.build.directory}\jmeter\html_detail_report</jmeter.result.html.dir><!--此路径用于存储由模板转换过来的测试结果的html文件--><jmeter.result.html.dir1>${project.build.directory}\jmeter\html_report</jmeter.result.html.dir1><ReportName>TestReport</ReportName></properties><dependencies><dependency><groupId>org.apache.jmeter</groupId><artifactId>ApacheJMeter_core</artifactId><version>5.4.3</version></dependency><dependency><groupId>org.apache.jmeter</groupId><artifactId>ApacheJMeter_java</artifactId><version>5.4.3</version></dependency><dependency><groupId>org.apache.jmeter</groupId><artifactId>jorphan</artifactId><version>5.4.3</version></dependency></dependencies><build><plugins><plugin><groupId>com.lazerycode.jmeter</groupId><artifactId>jmeter-maven-plugin</artifactId><version>3.6.0</version><configuration><!--指定jmeter版本号--><jmeterVersion>5.4.3</jmeterVersion><!-- JVM 设置--><jMeterProcessJVMSettings><xms>2048</xms><xmx>2048</xmx></jMeterProcessJVMSettings><!--JMeter能够创建.jtl(XML格式)测试结果和csv测试结果。 默认情况下,此插件使用csv格式。这里需要将其切换为xml格式。--><resultsFileFormat>xml</resultsFileFormat><!--这里是否生成result report需要设置为false,否则运行时会出现“前言中不允许有内容”的报错。因为若该项为true,无论你在.properties配置文件或者此pom文件中将结果文件的格式显示设置为xml,最后都只会生成csv的结果文件。而csv的格式无法转换成我们最后要的html结果文件,就会报上述的错。--><generateReports>true</generateReports><!--忽略失败--><ignoreResultFailures>true</ignoreResultFailures><!--报告文件尾部输出时间戳 --><appendResultsTimestamp>true</appendResultsTimestamp><!--指定日志级别--><overrideRootLogLevel>error</overrideRootLogLevel><!-- 将任何其他 Java 库添加到 JMeter 的 lib/ext 目录--><jmeterExtensions><artifact>kg.apc:jmeter-plugins-standard:1.4.0</artifact><artifact>kg.apc:jmeter-plugins-extras:1.4.0</artifact><artifact>kg.apc:jmeter-plugins-perfmon:2.1</artifact><artifact>kg.apc:jmeter-plugins-manager:1.7</artifact></jmeterExtensions><junitLibraries><!-- <artifact>com.lazerycode.junit:junit-test:1.0.0</artifact>--></junitLibraries><!-- 指定测试用例的路径--><testFilesDirectory>src\test\jmeter\</testFilesDirectory></configuration><executions><!-- Generate JMeter configuration --><execution><id>configuration</id><goals><goal>configure</goal></goals></execution><!-- Run JMeter tests --><execution><id>jmeter-tests</id><goals><goal>jmeter</goal></goals></execution><!-- Fail build on errors in test --><execution><id>jmeter-check-results</id><goals><goal>results</goal></goals></execution></executions></plugin></plugins></build>
</project>
properties 字段后的所有内容都是本次添加的jmter相关的配置信息,后续可做复制使用。另外,拿到这个配置内容,一般只需要修改以下内容就好
<!-- 指定测试用例的路径-->
<testFilesDirectory>src\test\jmeter\</testFilesDirectory>
执行
通过控制台命令执行
mvn clean verify
通过编辑器操作
通过编辑器中的LifeCycle中的操作来执行。
非首次则需要执行clean,再执行verify(首次则直接执行verify即可)
执行时控制台日志:
D:\Software\java\jdk1.8.0_181\bin\java.exe "-Dmaven.multiModuleProjectDirectory=D:\Program Files\GitData\GritJmeter" -Dmaven.home=D:\Software\apache-maven-3.9.1 -Dclassworlds.conf=D:\Software\apache-maven-3.9.1\bin\m2.conf -javaagent:D:\Software\ideaIU-2018.3.6.win\lib\idea_rt.jar=17454:D:\Software\ideaIU-2018.3.6.win\bin -Dfile.encoding=UTF-8 -classpath D:\Software\apache-maven-3.9.1\boot\plexus-classworlds-2.6.0.jar;D:\Software\apache-maven-3.9.1\boot\plexus-classworlds.license org.codehaus.classworlds.Launcher -Didea.version=2018.3.6 verify
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< Grit:GritJmeter >---------------------------
[INFO] Building GritJmeter 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.3.0:resources (default-resources) @ GritJmeter ---
[INFO] Copying 0 resource
[INFO]
[INFO] --- compiler:3.10.1:compile (default-compile) @ GritJmeter ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- jmeter:3.6.0:configure (configuration) @ GritJmeter ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] C O N F I G U R I N G J M E T E R
[INFO] -------------------------------------------------------
[INFO]
[INFO] Creating test configuration for execution ID: configuration
[INFO] Building JMeter directory structure...
[INFO] Generating JSON Test config...
[INFO] Configuring JMeter artifacts...
[INFO] Populating JMeter directory...
[INFO] Copying extensions to D:\Program Files\GitData\GritJmeter\target\39016a4e-390a-4b18-8df3-fb914d5aa91f\jmeter\lib\ext
Downloading dependencies: true
[INFO] Copying junit libraries to D:\Program Files\GitData\GritJmeter\target\39016a4e-390a-4b18-8df3-fb914d5aa91f\jmeter\lib\junit
Downloading dependencies: true
[INFO] Copying test plan libraries to D:\Program Files\GitData\GritJmeter\target\39016a4e-390a-4b18-8df3-fb914d5aa91f\jmeter\lib
Downloading dependencies: true
[INFO] Configuring JMeter properties...
[INFO]
[INFO] --- resources:3.3.0:testResources (default-testResources) @ GritJmeter ---
[INFO] Copying 6 resources
[INFO]
[INFO] --- compiler:3.10.1:testCompile (default-testCompile) @ GritJmeter ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- surefire:3.0.0:test (default-test) @ GritJmeter ---
[INFO]
[INFO] --- jar:3.3.0:jar (default-jar) @ GritJmeter ---
[INFO] Building jar: D:\Program Files\GitData\GritJmeter\target\GritJmeter-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- jmeter:3.6.0:jmeter (jmeter-tests) @ GritJmeter ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] P E R F O R M A N C E T E S T S
[INFO] -------------------------------------------------------
[INFO]
[INFO] Will generate HTML report in D:\Program Files\GitData\GritJmeter\target\jmeter\reports\cont
[INFO] Executing test: cont.jmx
[INFO] Arguments for forked JMeter JVM: [java, -Xms2048M, -Xmx2048M, -Djava.awt.headless=true, -jar, ApacheJMeter-5.4.3.jar, -d, D:\Program Files\GitData\GritJmeter\target\39016a4e-390a-4b18-8df3-fb914d5aa91f\jmeter, -e, -j, D:\Program Files\GitData\GritJmeter\target\jmeter\logs\cont.jmx.log, -l, D:\Program Files\GitData\GritJmeter\target\jmeter\results\cont-20231017.csv, -n, -o, D:\Program Files\GitData\GritJmeter\target\jmeter\reports\cont, -t, D:\Program Files\GitData\GritJmeter\target\jmeter\testFiles\cont.jmx, -L, ERROR, -Dsun.net.http.allowRestrictedHeaders, true]
[INFO]
[INFO] SLF4J: Class path contains multiple SLF4J bindings.
[INFO] SLF4J: Found binding in [jar:file:/D:/Program%20Files/GitData/GritJmeter/target/39016a4e-390a-4b18-8df3-fb914d5aa91f/jmeter/lib/log4j-slf4j-impl-2.17.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
[INFO] SLF4J: Found binding in [jar:file:/D:/Program%20Files/GitData/GritJmeter/target/39016a4e-390a-4b18-8df3-fb914d5aa91f/jmeter/lib/slf4j-nop-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
[INFO] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
[INFO] SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
[INFO] Creating summariser <summary>
[INFO] Created the tree successfully using D:\Program Files\GitData\GritJmeter\target\jmeter\testFiles\cont.jmx
[INFO] Starting standalone test @ Tue Oct 17 11:19:57 CST 2023 (1697512797293)
[INFO] Waiting for possible Shutdown/StopTestNow/HeapDump/ThreadDump message on port 4445
[INFO] summary + 1 in 00:00:04 = 0.3/s Avg: 3033 Min: 3033 Max: 3033 Err: 0 (0.00%) Active: 1 Started: 1 Finished: 0
[INFO] summary + 13 in 00:00:07 = 1.9/s Avg: 529 Min: 9 Max: 5276 Err: 0 (0.00%) Active: 0 Started: 1 Finished: 1
[INFO] summary = 14 in 00:00:10 = 1.3/s Avg: 708 Min: 9 Max: 5276 Err: 0 (0.00%)
[INFO] Tidying up ... @ Tue Oct 17 11:20:08 CST 2023 (1697512808570)
[INFO] ... end of run
[INFO] Completed Test: D:\Program Files\GitData\GritJmeter\target\jmeter\testFiles\cont.jmx
[INFO]
[INFO]
[INFO] --- jmeter:3.6.0:results (jmeter-check-results) @ GritJmeter ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] S C A N N I N G F O R R E S U L T S
[INFO] -------------------------------------------------------
[INFO]
[INFO] Will scan results using format: CSV
[INFO]
[INFO] Parsing results file 'D:\Program Files\GitData\GritJmeter\target\jmeter\results\cont-20231017.csv' as type: CSV
[INFO]
[INFO] -------------------------------------------------------
[INFO] P E R F O R M A N C E T E S T R E S U L T S
[INFO] -------------------------------------------------------
[INFO]
[INFO] Result (.csv) files scanned: 1
[INFO] Successful requests: 16
[INFO] Failed requests: 0
[INFO] Failures: 0.0% (0.0% accepted)
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 57.681 s
[INFO] Finished at: 2023-10-17T11:20:12+08:00
[INFO] ------------------------------------------------------------------------
[INFO] Shutdown detected, destroying JMeter process...
[INFO] Process finished with exit code 0
通过jmeter gui
cd 到maven项目代码目录下,执行命令
mvn jmeter:configure jmeter:gui
会自动打开jmeter界面
控制台日志
查看日志可知执行的时候获取的配置是默认配置execution ID: default-cli,而不是我们在pom中配置execution ID: configuration
Microsoft Windows [版本 10.0.17763.4974]
(c) 2018 Microsoft Corporation。保留所有权利。D:\Program Files\GitData\GritJmeter>mvn jmeter:configure jmeter:gui
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< Grit:GritJmeter >---------------------------
[INFO] Building GritJmeter 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- jmeter:3.6.0:configure (default-cli) @ GritJmeter ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] C O N F I G U R I N G J M E T E R
[INFO] -------------------------------------------------------
[INFO]
[INFO] Creating test configuration for execution ID: default-cli
[INFO] Building JMeter directory structure...
[INFO] Generating JSON Test config...
[INFO] Configuring JMeter artifacts...
[INFO] Populating JMeter directory...
[INFO] Copying extensions to D:\Program Files\GitData\GritJmeter\target\caab0619-1b6c-4fcb-b55c-13626e70662f\jmeter\lib\ext
Downloading dependencies: true
[INFO] Copying junit libraries to D:\Program Files\GitData\GritJmeter\target\caab0619-1b6c-4fcb-b55c-13626e70662f\jmeter\lib\junit
Downloading dependencies: true
[INFO] Copying test plan libraries to D:\Program Files\GitData\GritJmeter\target\caab0619-1b6c-4fcb-b55c-13626e70662f\jmeter\lib
Downloading dependencies: true
[INFO] Configuring JMeter properties...
[INFO]
[INFO] --- jmeter:3.6.0:gui (default-cli) @ GritJmeter ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] S T A R T I N G J M E T E R G U I
[INFO] -------------------------------------------------------
[INFO] Arguments for forked JMeter JVM: [java, -Xms2048M, -Xmx2048M, -jar, ApacheJMeter-5.4.3.jar, -d, D:\Program Files\GitData\GritJmeter\target\caab0619-1b6c-4fcb-b55c-13626e70662f\jmeter, -L, ERROR]
[INFO]
这个方式执行速度比较慢
查看报告
执行完成后会存在一个targer目录
通过Jenkins执行
安装Jenkins插件
HTML Publisher plugin
点击左边available plugins,查询HTML,选中HTML Publisher plugin,安装
同理,安装Performance Plugin、Maven Integration plugin
配置Jenkins运行工具
配置 maven
配置jdk
配置maven
创建可执行job
输入任务名称,选择构建一个maven项目
选择jdk(在配置Jenkins运行工具这一步,当时就已经配置了系统级的jdk,配置多个的话,则可自行选择一个,如果不选择,默认system系统的jdk)
如果是性能测试,则需要增加Publish Performance test result report
点击执行
运行成功
查看报告
问题记录
问题1:
testFilesDirectory 元素值只能是目录,不能是文件,不然会提示找到的不是目录类型而报错
问题2:pom.xml报错提示:Element 'configuration' cannot have character [children], because the type's content type is element
解决办法:发现pom文件内容中少了一个<导致。
配置文件中的beans节点下面只能是元素节点,不能有字符或者文本存在
比如,多余的标点符号,点,也有可能是空格。
问题3:执行结果 ,提示jmeter-maven-plugin版本3.6需要maven 版本3.5.2
解决方案:
问题4:Jenkins上查看报告为空
解决方案:Jenkins首页-点击Manage Jenkins-选择Script console
输入
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")
点击运行,再重新构建查看HTML报告。
参考:
Win10系统Jmeter+maven+Jenkins接口自动化环境搭建(一) | 柯南的博客记录