• [JMeter]maven利用jmeter-maven-plugin插件管理jmx脚本


    前置条件:已支持maven项目运行
    主要运行命令
    1、mvn clean verify

    2、唤起Jmeter界面 mvn jmeter:configure jmeter:gui

    一、项目下添加pom.xml文件

    在这里插入图片描述

    二、在pom.xml新增

    1、jmeter-maven-plugin插件,指定版本号
    git地址:项目git地址

    	   <groupId>com.lazerycode.jmeter</groupId>
           <artifactId>jmeter-maven-plugin</artifactId>
           <version>3.6.0</version>
    
    • 1
    • 2
    • 3

    2、新增properties属性
    java版本号需要和运行环境保持一致

    	<properties>
            <!--指定Maven用什么编码来读取源码及文档 -->
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <java.version>1.8</java.version>
            <!-- Maven默认执行Jmeter报告结果生成的路径 -->
            <jmeter.result.jtl.dir>${project.build.directory}\jmeter\results</jmeter.result.jtl.dir>
            <jmeter.result.html.dir>${project.build.directory}\jmeter\html</jmeter.result.html.dir>
            <!-- Jenkins构建生成报告所在路径 -->
            <!--
            <jmeter.result.jtl.dir>${env.WORKSPACE}/Report/${env.BUILD_ID}/jtl</jmeter.result.jtl.dir>
            <jmeter.result.html.dir>${env.WORKSPACE}/Report/${env.BUILD_ID}/html</jmeter.result.html.dir>
            -->
        </properties>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    运行结果文件如图
    在这里插入图片描述

    3、需要执行的jmx、csv和jmeter.properties文件放在src/test/jmeter 文件夹下

    src/test/jmeter需要自行新建
    在这里插入图片描述

    4、依赖版本配置

    <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    5、通用配置
    plugins下配置

    				<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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    6、屏蔽不执行的jmx脚本
    plugins下配置

    			<configuration>
    					<generateReports>true</generateReports>
                        <testFilesExcluded>
                            <excludeJMeterTestFile>Demo.jmx</excludeJMeterTestFile>
                            <ignoreResultFailures>true</ignoreResultFailures>
                        </testFilesExcluded>
                  </configuration>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    7、指定jmeter版本
    plugins下配置

    			<configuration>
    					<jmeterVersion>5.4.3</jmeterVersion>
                  </configuration>
    
    • 1
    • 2
    • 3

    8、配置依赖libs
    plugins下配置,压测监控组件,插件管理组件

    			<configuration>
    					<!-- 将任何其他 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>```
    			</configuration>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    9、线程资源配置
    plugins下配置

    			<!-- JVM 设置-->
                <jMeterProcessJVMSettings>
                    <xms>2048</xms>
                    <xmx>2048</xmx>
                </jMeterProcessJVMSettings>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    10、结果文件配置
    plugins下配置

    			<configuration>
    					<!-- 设置jmeter生成结果文件格式-->
                        <resultsFileFormat>xml</resultsFileFormat>
                        <!--这里是否生成result report需要设置为false,否则运行时会出现“前言中不允许有内容”的报错。
                        因为若该项为true,无论你在.properties配置文件或者此pom文件中将结果文件的格式显示设置为xml,最后都只会生成csv的结果文件。
                        而csv的格式无法转换成我们最后要的html结果文件,就会报上述的错。-->
                        <generateReports>true</generateReports>
                        <!--报告文件尾部输出时间戳 -->
                        <appendResultsTimestamp>true</appendResultsTimestamp>
                        <!--指定日志级别-->
                        <overrideRootLogLevel>error</overrideRootLogLevel>
               	<configuration>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    完整POM文件内容如下

    <?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>com.yangbh</groupId>
        <artifactId>JmeterPerformance</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\cud_business</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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106

    执行命令

    mvn clean verify
    
    • 1

    结果如下
    在这里插入图片描述
    执行命令

    mvn jmeter:configure jmeter:gui
    
    
    • 1
    • 2

    打开jmeter的GUI界面窗口
    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    多项式
    部分背包问题【贪心算法】
    求三维坐标绕坐标轴旋转后的坐标值
    代码随想录第33天 | ● 509. 斐波那契数 ● 70. 爬楼梯 ● 746. 使用最小花费爬楼梯
    Go语言类库-reflect(反射)
    Apache Kafka 基于 S3 的数据导出、导入、备份、还原、迁移方案
    .net----结构和枚举
    计算机组成原理第二章数据的表示和运算(数制与编码)
    【Python】中文字符写入json文件
    基于 SPI 的增强式插件框架设计
  • 原文地址:https://blog.csdn.net/mengwuyoulin/article/details/125907685