• 尝试使用jmeter-maven-plugin


    前提准备

    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文件内容如下:

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <modelVersion>4.0.0modelVersion>
    6. <groupId>GritgroupId>
    7. <artifactId>GritJmeterartifactId>
    8. <version>1.0-SNAPSHOTversion>
    9. <properties>
    10. <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    11. <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
    12. <jmeter.result.jtl.dir>${project.build.directory}\jmeter\resultsjmeter.result.jtl.dir>
    13. <jmeter.result.html.dir>${project.build.directory}\jmeter\html_detail_reportjmeter.result.html.dir>
    14. <jmeter.result.html.dir1>${project.build.directory}\jmeter\html_reportjmeter.result.html.dir1>
    15. <ReportName>TestReportReportName>
    16. properties>
    17. <dependencies>
    18. <dependency>
    19. <groupId>org.apache.jmetergroupId>
    20. <artifactId>ApacheJMeter_coreartifactId>
    21. <version>5.4.3version>
    22. dependency>
    23. <dependency>
    24. <groupId>org.apache.jmetergroupId>
    25. <artifactId>ApacheJMeter_javaartifactId>
    26. <version>5.4.3version>
    27. dependency>
    28. <dependency>
    29. <groupId>org.apache.jmetergroupId>
    30. <artifactId>jorphanartifactId>
    31. <version>5.4.3version>
    32. dependency>
    33. dependencies>
    34. <build>
    35. <plugins>
    36. <plugin>
    37. <groupId>com.lazerycode.jmetergroupId>
    38. <artifactId>jmeter-maven-pluginartifactId>
    39. <version>3.6.0version>
    40. <configuration>
    41. <jmeterVersion>5.4.3jmeterVersion>
    42. <jMeterProcessJVMSettings>
    43. <xms>2048xms>
    44. <xmx>2048xmx>
    45. jMeterProcessJVMSettings>
    46. <resultsFileFormat>xmlresultsFileFormat>
    47. <generateReports>truegenerateReports>
    48. <ignoreResultFailures>trueignoreResultFailures>
    49. <appendResultsTimestamp>trueappendResultsTimestamp>
    50. <overrideRootLogLevel>erroroverrideRootLogLevel>
    51. <jmeterExtensions>
    52. <artifact>kg.apc:jmeter-plugins-standard:1.4.0artifact>
    53. <artifact>kg.apc:jmeter-plugins-extras:1.4.0artifact>
    54. <artifact>kg.apc:jmeter-plugins-perfmon:2.1artifact>
    55. <artifact>kg.apc:jmeter-plugins-manager:1.7artifact>
    56. jmeterExtensions>
    57. <junitLibraries>
    58. junitLibraries>
    59. <testFilesDirectory>src\test\jmeter\testFilesDirectory>
    60. configuration>
    61. <executions>
    62. <execution>
    63. <id>configurationid>
    64. <goals>
    65. <goal>configuregoal>
    66. goals>
    67. execution>
    68. <execution>
    69. <id>jmeter-testsid>
    70. <goals>
    71. <goal>jmetergoal>
    72. goals>
    73. execution>
    74. <execution>
    75. <id>jmeter-check-resultsid>
    76. <goals>
    77. <goal>resultsgoal>
    78. goals>
    79. execution>
    80. executions>
    81. plugin>
    82. plugins>
    83. build>
    84. project>

    properties 字段后的所有内容都是本次添加的jmter相关的配置信息,后续可做复制使用。另外,拿到这个配置内容,一般只需要修改以下内容就好


                        src\test\jmeter\

    执行
    通过控制台命令执行

    mvn clean verify

    通过编辑器操作

    通过编辑器中的LifeCycle中的操作来执行。

    非首次则需要执行clean,再执行verify(首次则直接执行verify即可)

    执行时控制台日志:

    1. 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
    2. [INFO] Scanning for projects...
    3. [INFO]
    4. [INFO] --------------------------< Grit:GritJmeter >---------------------------
    5. [INFO] Building GritJmeter 1.0-SNAPSHOT
    6. [INFO] from pom.xml
    7. [INFO] --------------------------------[ jar ]---------------------------------
    8. [INFO]
    9. [INFO] --- resources:3.3.0:resources (default-resources) @ GritJmeter ---
    10. [INFO] Copying 0 resource
    11. [INFO]
    12. [INFO] --- compiler:3.10.1:compile (default-compile) @ GritJmeter ---
    13. [INFO] Nothing to compile - all classes are up to date
    14. [INFO]
    15. [INFO] --- jmeter:3.6.0:configure (configuration) @ GritJmeter ---
    16. [INFO]
    17. [INFO] -------------------------------------------------------
    18. [INFO] C O N F I G U R I N G J M E T E R
    19. [INFO] -------------------------------------------------------
    20. [INFO]
    21. [INFO] Creating test configuration for execution ID: configuration
    22. [INFO] Building JMeter directory structure...
    23. [INFO] Generating JSON Test config...
    24. [INFO] Configuring JMeter artifacts...
    25. [INFO] Populating JMeter directory...
    26. [INFO] Copying extensions to D:\Program Files\GitData\GritJmeter\target\39016a4e-390a-4b18-8df3-fb914d5aa91f\jmeter\lib\ext
    27. Downloading dependencies: true
    28. [INFO] Copying junit libraries to D:\Program Files\GitData\GritJmeter\target\39016a4e-390a-4b18-8df3-fb914d5aa91f\jmeter\lib\junit
    29. Downloading dependencies: true
    30. [INFO] Copying test plan libraries to D:\Program Files\GitData\GritJmeter\target\39016a4e-390a-4b18-8df3-fb914d5aa91f\jmeter\lib
    31. Downloading dependencies: true
    32. [INFO] Configuring JMeter properties...
    33. [INFO]
    34. [INFO] --- resources:3.3.0:testResources (default-testResources) @ GritJmeter ---
    35. [INFO] Copying 6 resources
    36. [INFO]
    37. [INFO] --- compiler:3.10.1:testCompile (default-testCompile) @ GritJmeter ---
    38. [INFO] Nothing to compile - all classes are up to date
    39. [INFO]
    40. [INFO] --- surefire:3.0.0:test (default-test) @ GritJmeter ---
    41. [INFO]
    42. [INFO] --- jar:3.3.0:jar (default-jar) @ GritJmeter ---
    43. [INFO] Building jar: D:\Program Files\GitData\GritJmeter\target\GritJmeter-1.0-SNAPSHOT.jar
    44. [INFO]
    45. [INFO] --- jmeter:3.6.0:jmeter (jmeter-tests) @ GritJmeter ---
    46. [INFO]
    47. [INFO] -------------------------------------------------------
    48. [INFO] P E R F O R M A N C E T E S T S
    49. [INFO] -------------------------------------------------------
    50. [INFO]
    51. [INFO] Will generate HTML report in D:\Program Files\GitData\GritJmeter\target\jmeter\reports\cont
    52. [INFO] Executing test: cont.jmx
    53. [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]
    54. [INFO]
    55. [INFO] SLF4J: Class path contains multiple SLF4J bindings.
    56. [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]
    57. [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]
    58. [INFO] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
    59. [INFO] SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
    60. [INFO] Creating summariser <summary>
    61. [INFO] Created the tree successfully using D:\Program Files\GitData\GritJmeter\target\jmeter\testFiles\cont.jmx
    62. [INFO] Starting standalone test @ Tue Oct 17 11:19:57 CST 2023 (1697512797293)
    63. [INFO] Waiting for possible Shutdown/StopTestNow/HeapDump/ThreadDump message on port 4445
    64. [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
    65. [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
    66. [INFO] summary = 14 in 00:00:10 = 1.3/s Avg: 708 Min: 9 Max: 5276 Err: 0 (0.00%)
    67. [INFO] Tidying up ... @ Tue Oct 17 11:20:08 CST 2023 (1697512808570)
    68. [INFO] ... end of run
    69. [INFO] Completed Test: D:\Program Files\GitData\GritJmeter\target\jmeter\testFiles\cont.jmx
    70. [INFO]
    71. [INFO]
    72. [INFO] --- jmeter:3.6.0:results (jmeter-check-results) @ GritJmeter ---
    73. [INFO]
    74. [INFO] -------------------------------------------------------
    75. [INFO] S C A N N I N G F O R R E S U L T S
    76. [INFO] -------------------------------------------------------
    77. [INFO]
    78. [INFO] Will scan results using format: CSV
    79. [INFO]
    80. [INFO] Parsing results file 'D:\Program Files\GitData\GritJmeter\target\jmeter\results\cont-20231017.csv' as type: CSV
    81. [INFO]
    82. [INFO] -------------------------------------------------------
    83. [INFO] P E R F O R M A N C E T E S T R E S U L T S
    84. [INFO] -------------------------------------------------------
    85. [INFO]
    86. [INFO] Result (.csv) files scanned: 1
    87. [INFO] Successful requests: 16
    88. [INFO] Failed requests: 0
    89. [INFO] Failures: 0.0% (0.0% accepted)
    90. [INFO]
    91. [INFO] ------------------------------------------------------------------------
    92. [INFO] BUILD SUCCESS
    93. [INFO] ------------------------------------------------------------------------
    94. [INFO] Total time: 57.681 s
    95. [INFO] Finished at: 2023-10-17T11:20:12+08:00
    96. [INFO] ------------------------------------------------------------------------
    97. [INFO] Shutdown detected, destroying JMeter process...
    98. [INFO]
    99. Process finished with exit code 0
    通过jmeter gui

    cd 到maven项目代码目录下,执行命令

    mvn jmeter:configure jmeter:gui

    会自动打开jmeter界面

    控制台日志

    查看日志可知执行的时候获取的配置是默认配置execution ID: default-cli,而不是我们在pom中配置execution ID: configuration

    1. Microsoft Windows [版本 10.0.17763.4974]
    2. (c) 2018 Microsoft Corporation。保留所有权利。
    3. D:\Program Files\GitData\GritJmeter>mvn jmeter:configure jmeter:gui
    4. [INFO] Scanning for projects...
    5. [INFO]
    6. [INFO] --------------------------< Grit:GritJmeter >---------------------------
    7. [INFO] Building GritJmeter 1.0-SNAPSHOT
    8. [INFO] from pom.xml
    9. [INFO] --------------------------------[ jar ]---------------------------------
    10. [INFO]
    11. [INFO] --- jmeter:3.6.0:configure (default-cli) @ GritJmeter ---
    12. [INFO]
    13. [INFO] -------------------------------------------------------
    14. [INFO] C O N F I G U R I N G J M E T E R
    15. [INFO] -------------------------------------------------------
    16. [INFO]
    17. [INFO] Creating test configuration for execution ID: default-cli
    18. [INFO] Building JMeter directory structure...
    19. [INFO] Generating JSON Test config...
    20. [INFO] Configuring JMeter artifacts...
    21. [INFO] Populating JMeter directory...
    22. [INFO] Copying extensions to D:\Program Files\GitData\GritJmeter\target\caab0619-1b6c-4fcb-b55c-13626e70662f\jmeter\lib\ext
    23. Downloading dependencies: true
    24. [INFO] Copying junit libraries to D:\Program Files\GitData\GritJmeter\target\caab0619-1b6c-4fcb-b55c-13626e70662f\jmeter\lib\junit
    25. Downloading dependencies: true
    26. [INFO] Copying test plan libraries to D:\Program Files\GitData\GritJmeter\target\caab0619-1b6c-4fcb-b55c-13626e70662f\jmeter\lib
    27. Downloading dependencies: true
    28. [INFO] Configuring JMeter properties...
    29. [INFO]
    30. [INFO] --- jmeter:3.6.0:gui (default-cli) @ GritJmeter ---
    31. [INFO]
    32. [INFO] -------------------------------------------------------
    33. [INFO] S T A R T I N G J M E T E R G U I
    34. [INFO] -------------------------------------------------------
    35. [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]
    36. [INFO]

    这个方式执行速度比较慢

    查看报告

    执行完成后会存在一个targer目录

    通过Jenkins执行
    安装Jenkins插件

    HTML Publisher plugin

    点击左边available plugins,查询HTML,选中HTML Publisher plugin,安装

    同理,安装Performance PluginMaven 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接口自动化环境搭建(一) | 柯南的博客记录

  • 相关阅读:
    Idea2023 Springboot web项目正常启动,页面展示404解决办法
    创建多个 conda 环境和 jupyter 切换使用环境
    阿里、字节等神创,必须是全网最全的Netty核心原理手册
    ESP8266-Arduino编程实例-MPL3115A2压力传感器驱动
    Chromium GPU资源共享
    mysql的主从创建及mycat的安装
    linux(ubuntu)安装并使用scrcpy
    axios
    SpringBoot启动流程源码分析
    List集合的Stream流式操作实现数据类型转换
  • 原文地址:https://blog.csdn.net/Migu4423/article/details/133856708