1.Basic 配置

1,在pom文件中增加如下插件信息:

<project> <build> <plugins> <plugin> <groupId>com.lazerycode.jmeter</groupId> <artifactId>jmeter-maven-plugin</artifactId> <version>1.9.0</version> <executions> <execution> <id>jmeter-tests</id> <phase>verify</phase> <goals> <goal>jmeter</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>

这里的作用是制定了插件版本 ,并将插件目标绑定到生命周期的verify阶段

注: mvn生命周期 阶段描述

validate 验证项目是否正确,以及所有为了完整构建必要的信息是否可用 

verify 执行所有检查,验证包是有效的,符合质量规范 

install 安装包至本地仓库,以备本地的其它项目作为依赖使

 

2,将需要执行的测试用例放到${basedir}/src/test/jmeter目录下面

3,将需要的配置文件放到${basedir}/src/test/jmeter目录下面。

jmeter.properties saveservice.properties upgrade.properties system.properties user.properties global.properties

4,执行mvn verify命令。

2.选择执行的测试脚本

默认执行所有的脚本

使用<jMeterTestFiles>选择执行用例

如果需要选择执行tests,可以使用 tag:

<plugin> [...] <configuration> <testFilesIncluded> <jMeterTestFile>test1.jmx</jMeterTestFile> <jMeterTestFile>test2.jmx</jMeterTestFile> </testFilesIncluded> </configuration> [...] </plugin>

更复杂的情况,可以使用正则表达式选择需要执行的.jmx文件。

<configuration> <testFilesIncluded> <jMeterTestFile>foo*.jmx</jMeterTestFile> </testFilesIncluded> </configuration>

使用<testFilesExcluded>标签排除用例

如果需要从测试用例中去掉不执行的tests,可以使用 tag,同样可以使用正则表达式:

<configuration> <testFilesExcluded> <excludeJMeterTestFile>test3.jmx</excludeJMeterTestFile> <excludeJMeterTestFile>*bar.jmx</excludeJMeterTestFile> </testFilesExcluded> </configuration>

使用<testFilesDirectory>指定测试用例的路径

<configuration> <testFilesDirectory>/scratch/testfiles/</testFilesDirectory> </configuration>

3.测试结果

使用<testResultsTimestamp>去掉结果中的时间戳

jmeter-maven-plugin插件默认在每个jtl结果文件的文件名中添加时间戳。如果你不想要这个时间戳,可以通过设置标签来禁用。

<configuration> <testResultsTimestamp>false</testResultsTimestamp> </configuration>

使用<appendResultsTimestamp>将时间戳放到文件末尾

testResultsTimestamp标签默认为true,并且在文件开头添加时间戳。可以通过设置标签,来让时间戳出现在文件末尾。

<configuration> <appendResultsTimestamp>true</appendResultsTimestamp> </configuration>

使用<resultsFileNameDateFormat>设置时间戳格式

jtl结果文件中的时间戳默认是ISO_8601格式的日期(YYYYMMDD)。可以通过设置标签来修改。

<configuration> <resultsFileNameDateFormat>yyyy-MM-dd-HH-mm-ss</resultsFileNameDateFormat> </configuration>

使用<resultsFileFormat>设置结果文件的格式

<configuration> <resultsFileFormat>csv</resultsFileFormat> </configuration>

使用<resultsDirectory>指定结果路径

<configuration> <resultsDirectory>/tmp/jmeter</resultsDirectory> </configuration>

使用<ignoreResultFailures>忽略错误

默认情况下,如果发生请求失败,那么插件就会停止运行(因为该插件现在不支持读取csv格式的结果,所以如果如果结果保存是csv格式,任何错误都会被忽略)。如果我们想忽略错误,让插件继续执行,可以通过来配置。

<configuration> <ignoreResultFailures>true</ignoreResultFailures> </configuration>

使用<suppressJMeterOutput>设置控制台输出

默认情况下,该插件不会将运行日志都在控制台打印出来。如果想打印jmeter的日志,可以通过false打开。

<configuration> <suppressJMeterOutput>false</suppressJMeterOutput> </configuration>

使用<skipTests>忽略jmeter测试

如果我们不想执行性能测试,可以设置忽略。

<execution> <id>jmeter-tests</id> <phase>verify</phase> <goals> <goal>jmeter</goal> </goals> <configuration> <skipTests>${skipTests}</skipTests> </configuration> </execution>

执行命令:mvn verify –DskipTests=true

4.远程控制

<startServersBeforeTests>标签,可以将–runremote命令发送到jmeter.properties中配置的节点机器上,来开启远程服务。

<stopServersAfterTests>标签,可以将–remoteexit命令发送到jmeter.properties中配置的节点机器上,来关闭远程服务。

两个标签可以分开使用,因此我们可以通过其他进程来开启和关闭jmeter的远程服务。

<plugin> <groupId>com.lazerycode.jmeter</groupId> <artifactId>jmeter-maven-plugin</artifactId> <version>1.9.0</version> <executions> <execution> <id>jmeter-tests</id> <phase>verify</phase> <goals> <goal>jmeter</goal> </goals> <configuration> <remoteConfig> <startServersBeforeTests>true</startServersBeforeTests> <stopServersAfterTests>true</stopServersAfterTests> </remoteConfig> </configuration> </execution> </executions> </plugin>

如果我们想在执行每个jmeter任务时,就重启一下远程服务,这个标签可以满足我们的需求,且这个标签的优先级比<startServersBeforeTests>和<stopServersAfterTests>的高,如果同时设置了这两种标签,后两者会被忽略。

<configuration> <remoteConfig> <startAndStopServersForEachTest>false</startAndStopServersForEachTest> </remoteConfig> </configuration>

默认情况下,jmeter-maven-plugin插件将会启动jmeter.properties中定义的所以机器节点,但是我们可以通过标签来手动控制启动那些节点。

<execution> <id>jmeter-tests</id> <phase>verify</phase> <goals> <goal>jmeter</goal> </goals> <configuration> <remoteConfig> <startServersBeforeTests>true</startServersBeforeTests> <serverList>server1,server2</serverList> <stopServersAfterTests>true</stopServersAfterTests> </remoteConfig> </configuration> </execution>

宝剑锋从磨砺出; 梅花香自苦寒来。