你笑的时候全世界陪你一起笑,你哭的时候只有你一个人哭

jmeter-maven-plugin实现Jenkins自动化发起HTTP GET性能测试

上一篇 / 下一篇  2017-08-02 13:23:45 / 个人分类:性能测试

一、     测试目的

目前公司性能需求还不大,没有专职的性能测试人员,仅偶尔有些网站或APP的性能优化需要简单执行一下,而且请求多以HTTP GET为主,为了简化性能的流程,减少工作量,因此计划将JMeter的发压放到Jenkins上实现,让项目组内所有同事都可以自己设定发压的url地址、并发数量、执行时长并发起压力。

二、     jmeter-maven-plugin简介

jmeter-maven-plugin是一个开源的maven插件,配置在pom.xml中可以让maven项目在verify阶段自动调起JMeter执行发压,配合Jenkins可以方便实现性能测试的自动化执行和简单的结果收集、对比。目前jmeter-maven-plugin最新版是2.2.0,支持到JMeter 3.2版本,新增支持设置reportgenerator.properties,测试后可以生成JMeter Dashboard

jmeter-maven-plugin 2.2.0要求最低JDK 1.7,但JMeter 3.2要求JDK 1.8,因此Jenkins里执行jmeter-maven-plugin 2.2.0job时需要配置运行在JDK 1.8的环境下。jmeter-maven-plugin的配置项很多,本文仅介绍目前项目中使用到的参数配置。

三、     JMeter性能项目编写

新建一个JMeter的测试计划,在Test Plan下新建一个User Defined Variablesurl=${__P(url,www.maimaiche.com)},从Jenkins读取初始的url值(可包含多个url,以竖线|分割):

Thread Group下添加并发参数${threads}${duration},注意勾选Loop CountForever,勾选Scheduler

添加Config Element User Defined Variables,添加3个参数:urlformat=${__split(${url},urlformat,|)threads=${__P(threads,1)}duration=${__P(duration,10)}

urlformat会将初始的url地址按竖线分割成不同的url地址,threadsduration将从Jenkins中读取参数值:

添加Logic Controller ForEach ControllerInput variable prefix里输入urlformatOutput variable name里输入urlfinal,这时ForEach控制器会按顺序执行格式化分开的各个url

 

ForEach Controller下面新建HTTP Request SamplerName改为HTTP Request ${urlfinal}方便事后统计不同url的请求,Server Name or IP里输入${urlfinal},请求具体的url地址:

测试计划中可以添加一些Listener,方便在GUI模式下验证参数设置的是否正确。

四、     jmeter-maven-plugin性能项目编写

1.项目结构

jmeter-maven-pluginmaven编译过程中默认从<basedir>\src\test\jmeter下搜索jmx测试计划,并在verify阶段执行测试计划,最后把执行的日志结果保存在<project.build.directory>\jmeter\results下。

由于我们配置的是JMeter最基本的请求,因此不用编写任何代码,只需要将JMeter测试计划放到jmeter目录下即可,目录结构如下:

 

 

2.POM文件

项目的pom.xml文件如下:

<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.ucar.perf</groupId>

     <artifactId>jmeter-web-jenkins</artifactId>

     <version>0.0.1-SNAPSHOT</version>

     <build>

         <plugins>

              <plugin>

                   <groupId>org.apache.maven.plugins</groupId>

                   <artifactId>maven-compiler-plugin</artifactId>

                   <configuration>

                       <source>1.7</source>

                       <target>1.7</target>

                   </configuration>

              </plugin>

              <plugin>

                   <groupId>com.lazerycode.jmeter</groupId>

                   <artifactId>jmeter-maven-plugin</artifactId>

                   <version>2.2.0</version>

                   <executions>

                       <execution>

                            <id>jmeter-tests</id>

                            <phase>verify</phase>

                            <goals>

                                 <goal>jmeter</goal>

                            </goals>

                       </execution>

                   </executions>

                   <configuration>

                       <downloadExtensionDependencies>false</downloadExtensionDependencies>

                       <testFilesIncluded>

                            <jMeterTestFile>web.jmx</jMeterTestFile>

                       </testFilesIncluded>

                       <propertiesUser>

                            <threads>${threads}</threads>

                            <duration>${duration}</duration>

                            <url>${url}</url>

                            <jmeter.save.saveservice.output_format>csv</jmeter.save.saveservice.output_format>

                            <jmeter.save.saveservice.bytes>true</jmeter.save.saveservice.bytes>

                            <jmeter.save.saveservice.label>true</jmeter.save.saveservice.label>

                            <jmeter.save.saveservice.latency>true</jmeter.save.saveservice.latency>

                            <jmeter.save.saveservice.response_code>true</jmeter.save.saveservice.response_code>

                            <jmeter.save.saveservice.response_message>true</jmeter.save.saveservice.response_message>

                            <jmeter.save.saveservice.successful>true</jmeter.save.saveservice.successful>

                            <jmeter.save.saveservice.thread_counts>true</jmeter.save.saveservice.thread_counts>

                            <jmeter.save.saveservice.thread_name>true</jmeter.save.saveservice.thread_name>

                            <jmeter.save.saveservice.time>true</jmeter.save.saveservice.time>

                            <jmeter.save.saveservice.connect_time>true</jmeter.save.saveservice.connect_time>

                            <jmeter.save.saveservice.timestamp_format>yyyy/MM/dd HH:mm:ss.SSS</jmeter.save.saveservice.timestamp_format>

                            <jmeter.reportgenerator.date_format>yyyy/MM/dd HH:mm:ss.SSS</jmeter.reportgenerator.date_format>

                       </propertiesUser>

                       <resultsFileNameDateFormat>yyyy-MM-dd</resultsFileNameDateFormat>

                       <!-- <resultsFileFormat>csv</resultsFileFormat> -->

                       <jMeterProcessJVMSettings>

                            <xms>128</xms>

                            <xmx>128</xmx>

                       </jMeterProcessJVMSettings>

                   </configuration>

              </plugin>

         </plugins>

     </build>

</project>

threadsdurationurlJenkins里取值并设置到JMeteruser.properties文件中实现参数化传递到JMeter测试用例中。

五、     Jenkins项目设置

本文以Jenkins 1.651为例,下载并安装Subversion Plug-inPerformance Plugin

Jenkins里新建一个maven项目,用来编译JMeter HTTP Request的项目。

l 参数化构建过程中添加2String Parameterthreadsduration,添加一个Text Parameter url

l SVN路径指向JMeter HTTP Request的项目地址。

l maven goals and options”clean verify

l 添加一个JDK 1.8的路径,在Job中使用这个JDK

l Post Steps”下添加“Execute Shell”/usr/java/jdk1.8.0_144/bin/java -jar $WORKSPACE/target/jmeter/bin/ApacheJMeter-3.2.jar -g $WORKSPACE/target/jmeter/results/*.csv -o $WORKSPACE/target/jmeter/results/report”。执行后自动根据结果生成JMeter Dashboard

l 构建后操作下添加“Publish

TAG:

dionysus的个人空间 引用 删除 dionysus   /   2017-08-02 13:43:22
帖子字数超了,后面一些文字写在这里:
        “构建后操作”下添加“Publish Performance test result report”,“Source data files”指向**/target/jmeter/results/*.jtl,可以用星号代替名字:



保存后点击Build with Parameters即可输入参数并开始构建:

查看Jenkins编译日志可以看到jmeter-maven-plugin已经调起了JMeter执行测试计划:

执行成功后Performance Plugin会分析测试结果并与之前编译成功的构建结果进行简单对比:

由于本次试验的Jenkins上HTML Publisher Plugin不好用,因此执行后没有添加JMeter Dashboard直接输出展示,但可以通过Job执行后工作区的link访问JMeter Dashborad:
 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

Open Toolbar