转自:http://my.oschina.net/u/1377774/blog/168969
目录[-]
第一步.先在maven工程中单独使用jmeter-maven插件
0.环境
Windows7 64bit
SUN JDK 1.7 64bit eclipse v3.7 m2e v1.0.100.20110804-1717 eclipse中外挂maven 3.02注意: 由于jmeter-maven插件运行时自动会引入jmeter所需的类库,所以机器上不需要单独安装jmeter。
1.在POM.xml文件加入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | < build > ... < plugins > < plugin > < groupId >com.lazerycode.jmeter</ groupId > < artifactId >jmeter-maven-plugin</ artifactId > < version >1.8.2-SNAPSHOT</ version > < configuration > < resultsFileFormat >xml</ resultsFileFormat > < ignoreResultFailures >true</ ignoreResultFailures > < testResultsTimestamp >false</ testResultsTimestamp > </ configuration > < executions > < execution > < id >jmeter-tests</ id > < phase >verify</ phase > < goals > < goal >jmeter</ goal > </ goals > </ execution > </ executions > < plugins > < span ></ span >... < build > |
如果出现找不到构件(artifact)的情况,考虑切换一下Maven Repository,最直接的方法是在POM中加入类似如下代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | < pluginRepositories > < pluginRepository > < id >Codehaus repository</ id > < url >http://repository.codehaus.org/</ url > < releases > < enabled >true</ enabled > < updatePolicy >always</ updatePolicy > </ releases > < snapshots > < enabled >true</ enabled > < updatePolicy >always</ updatePolicy > </ snapshots > </ pluginRepository > </ pluginRepositories > |
2.新建scr/test/jmeter目录(这是jmeter-maven插件默认找jmeter的jmx文件的位置,若要更改在POM中作配置),把已经测试好的jmeter文件如test1.jmx拷贝进去。
在Eclipse中Run Configurations -> Maven Build -> new中Goals输入框填入 verify,确定。
笔者在运行过程曾遇到异常
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 | [ERROR] Failed to execute goal com.lazerycode.jmeter:jmeter-maven-plugin: 1.8 . 1 :jmeter (jmeter-tests) on project mytodo: Execution jmeter-tests of goal com.lazerycode.jmeter:jmeter-maven-plugin: 1.8 . 1 :jmeter failed. NullPointerException -> [Help 1 ] org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal com.lazerycode.jmeter:jmeter-maven-plugin: 1.8 . 1 :jmeter (jmeter-tests) on project mytodo: Execution jmeter-tests of goal com.lazerycode.jmeter:jmeter-maven-plugin: 1.8 . 1 :jmeter failed. at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java: 225 ) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java: 153 ) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java: 145 ) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java: 84 ) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java: 59 ) at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java: 183 ) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java: 161 ) at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java: 319 ) at org.apache.maven.DefaultMaven.execute(DefaultMaven.java: 156 ) at org.apache.maven.cli.MavenCli.execute(MavenCli.java: 534 ) at org.apache.maven.cli.MavenCli.doMain(MavenCli.java: 196 ) at org.apache.maven.cli.MavenCli.main(MavenCli.java: 141 ) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java: 57 ) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java: 43 ) at java.lang.reflect.Method.invoke(Method.java: 601 ) at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java: 290 ) at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java: 230 ) at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java: 409 ) at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java: 352 ) Caused by: org.apache.maven.plugin.PluginExecutionException: Execution jmeter-tests of goal com.lazerycode.jmeter:jmeter-maven-plugin: 1.8 . 1 :jmeter failed. at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java: 116 ) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java: 209 ) ... 19 more Caused by: java.lang.NullPointerException at com.lazerycode.jmeter.JMeterAbstractMojo.setJMeterResultFileFormat(JMeterAbstractMojo.java: 382 ) at com.lazerycode.jmeter.JMeterMojo.execute(JMeterMojo.java: 43 ) at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java: 107 ) ... 20 more |
查找资料未解决后,找到com.lazerycode.jmeter.JMeterAbstractMojo类第382行中,setJMeterResultFileFormat中对应的代码:
1 2 3 4 5 6 7 8 9 10 | protected void setJMeterResultFileFormat() { if (resultsFileFormat.toLowerCase().equals( "csv" )) { propertiesJMeter.put( "jmeter.save.saveservice.output_format" , "csv" ); resultsOutputIsCSVFormat = true ; } else { propertiesJMeter.put( "jmeter.save.saveservice.output_format" , "xml" ); resultsOutputIsCSVFormat = false ; } } |
断定是propertiesJMeter为null所致,为什么这个对象没初始化,答案目前不得而知。于是去github把jmeter-maven插件最新代码clone到本地,使用new初始化propertiesJMeter对象,在eclipse中重新打包,得到jmeter-maven-plugin-1.8.2-SNAPSHOT.jar,再把POM.xml中jmeter-maven-plugin插件版本从1.8.1更换为1.8.2-SNAPSHOT,再次运行mvn verify ,就看到打印出Build Success字样。
更多jmeter-maven插件帮助:
第二步.在Jenkins中集成使用jmeter-maven插件
0.环境
CentOS Linux 6.0 x64 SUN JDK 1.7 64bit Jenkins ver. 1.5291.在安装Jenkins中安装Performance Plugin
2.在需要集成jmeter-maven插件的工程配置页面,构建后操作加入一个Publish Performance test result report,如图所示
说明:Performance report是指展现的报告种类,目前支持JMeter和JUnit 如果指定为JMeter需要指出JMeter测试结果集目录,**代表pom.xml所在目录 Performance threshold是指标记本次构建为Unstable和Failed时所需的最低测试错误百分比
3.在MAVEN Build的Goals中填入verify。
注意事项:如果JMeter性能测试比较耗时,就不能把它放在这里执行,因为它会导致Jenkins构建时间过长。对此,可提前手工使用命令执行,例如:
$ jmeter -n -p user.properties -t my_test_plan.jmx -l my_results.jtl
也可把它做成Linux Crontab脚本。还有一种方式是,利用Jenkins的周期性构建功能,勾选Build periodically复选框,设定一个定时执行间隔。
4.执行Jenkins构建,观察结果。
maven-jmeter-plugin使用 2011-06-17 10:42:28
步骤1:下载maven-jmeter-plugin的jar包和依赖pom
download:http://jmeter-maven-plugin.googlecode.com/files/maven-jmeter-plugin-1.0.zip
步骤2:安装到本地仓库或远端仓库
【注意】需要先修改jmeter的位置,因为很可能公司仓库中的jmeter和maven中央仓库中的有变化。调整jmeter依赖为:
- <dependency>
- <groupId>com.alibaba.external</groupId>
- <artifactId>apache.jmeter</artifactId>
- <version>2.3</version>
- </dependency>
安装语法:
- mvn deploy:deploy-file -DgroupId=org.apache.jmeter -DartifactId=maven-jmeter-plugin -Dversion=1.0 -Dpackaging=jar -Dfile=maven-jmeter-plugin-1.0.jar -DpomFile=maven-jmeter-plugin-1.0.pom -Durl=file://<repo dir>
deploy: mvn deploy:deploy-file -DgroupId=org.apache.jmeter -DartifactId=maven-jmeter-plugin -Dversion=1.0 -Dpackaging=jar -Dfile=maven-jmeter-plugin-1.0.jar -DpomFile=maven-jmeter-plugin-1.0.pom -Durl=file://D:\maven.repository
或-Durl=file://"C:\Documents and Settings\username\.m2\repository"
步骤3:在本地工程中引用jmeter plugin
-
- <plugin>
- <groupId>org.apache.jmeter</groupId>
- <artifactId>maven-jmeter-plugin</artifactId>
- <version>1.0</version>
- <executions>
- <execution>
- <id>jmeter-tests</id>
- <phase>verify</phase>
- <goals>
- <goal>jmeter</goal>
- </goals>
- </execution>
- </executions>
- <configuration>
- <includes>
- <!--
- <include>druid-delete.jmx</include>
- -->
- <include>druid-insert.jmx</include><!--
- <include>druid-select.jmx</include>
- --></includes>
- <reportDir>target/jmeter-reports</reportDir>
- <jmeterUserProperties> for user properites </jmeterUserProperties>
- </configuration>
- </plugin>