Jenkins中集成jmeter-maven插件

第一步.先在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文件加入

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

如果出现找不到构件(artifact)的情况,考虑切换一下Maven Repository,最直接的方法是在POM中加入类似如下代码。

	<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,确定。

笔者在运行过程曾遇到异常

[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中对应的代码:

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插件帮助:https://github.com/Ronnie76er/jmeter-maven-plugin/wiki


第二步.在Jenkins中集成使用jmeter-maven插件

0.环境

    CentOS Linux 6.0 x64
    SUN JDK 1.7 64bit
    Jenkins ver. 1.529

1.在安装Jenkins中安装Performance Plugin

2.在需要集成jmeter-maven插件的工程配置页面,构建后操作加入一个Publish Performance test result report,如图所示Jenkins中集成jmeter-maven插件_第1张图片

说明:Performance report是指展现的报告种类,目前支持JMeter和JUnit
              如果指定为JMeter需要指出JMeter测试结果集目录,**代表pom.xml所在目录
        Performance threshold是指标记本次构建为Unstable和Failed时所需的最低测试错误百分比

3.在MAVEN Build的Goals中填入verify。

Jenkins中集成jmeter-maven插件_第2张图片

注意事项:如果JMeter性能测试比较耗时,就不能把它放在这里执行,因为它会导致Jenkins构建时间过长。对此,可提前手工使用命令执行,例如:

$ jmeter -n -p user.properties -t my_test_plan.jmx -l my_results.jtl

也可把它做成Linux Crontab脚本。还有一种方式是,利用Jenkins的周期性构建功能,勾选Build periodically复选框,设定一个定时执行间隔。

4.执行Jenkins构建,观察结果。

Jenkins中集成jmeter-maven插件_第3张图片





你可能感兴趣的:(Jenkins,jmeter-maven插件)