[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>

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>

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

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>

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>

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

			<configuration>
					<generateReports>true</generateReports>
                    <testFilesExcluded>
                        <excludeJMeterTestFile>Demo.jmx</excludeJMeterTestFile>
                        <ignoreResultFailures>true</ignoreResultFailures>
                    </testFilesExcluded>
              </configuration>

7、指定jmeter版本
plugins下配置

			<configuration>
					<jmeterVersion>5.4.3</jmeterVersion>
              </configuration>

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>

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

			<!-- JVM 设置-->
            <jMeterProcessJVMSettings>
                <xms>2048</xms>
                <xmx>2048</xmx>
            </jMeterProcessJVMSettings>

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>

完整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>

执行命令

mvn clean verify

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

mvn jmeter:configure jmeter:gui

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

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值