分享

jmeter

 daydayupjuan 2016-11-18

jmeter-maven-plugin使用

在github上提供了一个jmeter的maven插件,能够使用maven来执行jmeter测试。 
官网:http://jmeter./ 
GitHub:https://github.com/jmeter-maven-plugin/jmeter-maven-plugin 
wiki:https://github.com/jmeter-maven-plugin/jmeter-maven-plugin/wiki

1.Basic Usage

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>

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.选择执行的测试脚本

默认执行所有的脚本

执行所有脚本,只需要运行写到pom中的执行阶段即可,该处使用的是mvn verify

<plugin>
   [...]
        <executions>
            <execution>
                <id>jmeter-tests</id>
                <phase>verify</phase>
                <goals>
                    <goal>jmeter</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

使用<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的日志,可以通过关掉。

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

5.控制JVM参数

JMeter Maven plugin 使用独立的JVM来运行,并且可以使用-Xms,-Xmx以及其他的命令行参数来控制该JVM。 
1024 1024 -Xprof -Xfuture

maven插件与jenkins集成

在jenkins上安装Performance Plugin,然后在report files中添加输出结果的ftl文件路径,即可将jmeter的运行结果展示到jenkins上。

jenkins插件地址:https://wiki./display/JENKINS/Performance+Plugin

配置见插件地址链接


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约