How to run JMeter tests with Maven

I just finished an effort setting up automated load tests against a GWT-RPC server interface.  It took me some time to pull together the pieces from different sources and figure out what was possible and how I wanted to organize the whole thing.  But once I was there the result is pretty simple.  So I’m going to write it down here and hopefully save someone else the effort of figuring it out like I did. So this post is the first part of the puzzle – how to create a project that will run JMeter tests with Maven.  I got a lot of help from some other people’s posts on this subject.  Jacobo Matute’s post Performance Tests with Jmeter, Maven and Hudson was a big help, but fortunately slightly out of date. My goal with this project was to have something that works the maven way.  All I should have to do is:

  • Checkout the project.
  • Run a maven goal.

I don’t want to have to install JMeter first.  I don’t want to have to go get any dependencies. That’s what Maven’s for.  This used to be a problem because there were no JMeter artifacts available, but that’s fortunately not true anymore.

1) Create a simple maven project

I use the quickstart archetype:

mvn archetype:generate -DgroupId=com.example -DartifactId=jmeter-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

This creates a basic java project named jmeter-demo, with and example App class and an AppTest test class.  We don’t need either of these, so we can delete them.  Make sure you keep src/main/java and src/test/java, but you can delete the com directory in each of these. Now you should be able to

cd jmeter-demo

and

mvn install

and get a build success, but nothing was built.

2) Add the JMeter Maven plugin

All of the heavy lifting will be done for us by the JMeter Maven plugin.  Edit the pom.xml file in the jmeter-demo directory so it looks like this:

<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.example</groupId>
	<artifactId>jmeter-demo</artifactId>
	<packaging>jar</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>jmeter-demo</name>
	<url>http://maven.apache.org</url>

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

So you’re removing the junit dependency and adding a build plugin that runs jmeter tests when you do the verify phase of the lifecycle. If you run mvn install now you will get a failure because you don’t have the jmeter test directory created.

3) Create a JMeter test

The JMeter plugin will run any test that it finds under the src/test/jmeter directory.  A JMeter  test is just xml, but it’s easiest to create this xml with the JMeter GUI.  Currently you have to install JMeter in order to run the GUI, but hopefully very soon it will be possible to run the GUI directly from the JMeter plugin. Using the GUI I created a simple test with a Thread Group that runs 10 times, an HTTP Request Sampler that requests the Google Home Page at http://www.google.com, and a View Results in Table that shows the timing results.  Then I ran it in the GUI.  I saved the test in a src/test/jmeter directory that I created.

4) Run it in Maven

Now I run

mvn install

in my project directory.  Here’s a piece of the output:

[INFO] --- jmeter-maven-plugin:1.4.1:jmeter (jmeter-tests) @ jmeter-demo ---
[INFO]
[INFO] -------------------------------------------------------
[INFO]  P E R F O R M A N C E    T E S T S
[INFO] -------------------------------------------------------
[INFO]
[WARNING] Unable to parse the 'jmeter.exit.check.pause' entry in jmeter.properties!  Falling back to a default value of '2500'.
[INFO]
[INFO] Proxy server is not being used.

[info]
[debug] JMeter is called with the following command line arguments: -n -t C:\projects\jmeter-demo\jmeter-demo\src\test\jmeter\Demo Test.jmx -l C:\projects\jmeter-demo\jmeter-demo\target\jmeter\report\Demo Test-120416.jtl -d C:\projects\jmeter-demo\jmeter-demo\target\jmeter
[info] Executing test: Demo Test.jmx
INFO    2012-04-16 14:51:59.725 [jmeter.e] (): Listeners will be started after enabling running version
INFO    2012-04-16 14:51:59.726 [jmeter.e] (): To revert to the earlier behaviour, define jmeterengine.startlistenerslater=false
[info] Completed Test: Demo Test.jmx
[info]
[info] Building JMeter Report(s)...
[info]
[info] Raw results: C:\projects\jmeter-demo\jmeter-demo\target\jmeter\report\Demo Test-120416.jtl
[info] Test report: C:\projects\jmeter-demo\jmeter-demo\target\jmeter\report\Demo Test-120416.jtl-report.html
[INFO]
[INFO] Test Results:
[INFO]
[INFO] Tests Run: 1, Failures: 0, Errors: 0
[INFO]
[INFO]

We can see that the test ran successfully, the raw results are located at target/jmeter/report/Demo Test-120416.jtl and an html report is located at target/jmeter/report/Demo Test-120416.jtl-report.html. You can grab the sample project from the Stage_1_JMeter_and_Maven branch of my jmeter-demo project. Check out my next step in the automated load tests effort:

How to automate JMeter tests with Maven and Jenkins (Hudson)

About these ads

12 thoughts on “How to run JMeter tests with Maven

  1. Pingback: How to automate JMeter tests with Maven and Jenkins (Hudson) | i4igeeks

  2. Hi Friend,
    thanks for your efforts. I was trying to execute the same but was getting below errors

    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

    [INFO]
    [INFO] — maven-jar-plugin:2.3.2:jar (default-jar) @ jmeter-demo —
    [INFO] Building jar: D:\jmeter-demo\target\jmeter-demo-1.0-SNAPSHOT.jar
    [INFO]
    [INFO] — jmeter-maven-plugin:1.4.1:jmeter (jmeter-tests) @ jmeter-demo —
    [INFO]
    [INFO] ——————————————————-
    [INFO] P E R F O R M A N C E T E S T S
    [INFO] ——————————————————-
    [INFO]
    [WARNING] Unable to parse the ‘jmeter.exit.check.pause’ entry in jmeter.properties! Falling back to a default value of ’2500′.
    [INFO]
    [INFO] Proxy server is not being used.

    [info]
    [debug] JMeter is called with the following command line arguments: -n -t D:\jmeter-demo\src\test\jmeter\Demo Test.jmx -l D:\jmeter-demo\t
    get\jmeter
    [info] Executing test: Demo Test.jmx
    INFO 2013-07-24 15:51:50.524 [jmeter.e] (): Listeners will be started after enabling running version
    INFO 2013-07-24 15:51:50.525 [jmeter.e] (): To revert to the earlier behaviour, define jmeterengine.startlistenerslater=false
    [info] Completed Test: Demo Test.jmx
    [info]
    [info] Building JMeter Report(s)…
    [info]
    [info] Raw results: D:\jmeter-demo\target\jmeter\report\Demo Test-130724.jtl
    [info] Test report: D:\jmeter-demo\target\jmeter\report\Demo Test-130724.jtl-report.html
    [INFO]
    [INFO] Test Results:
    [INFO]
    [INFO] Tests Run: 1, Failures: 1, Errors: 0
    [INFO]
    [INFO] ————————————————————————
    [INFO] BUILD FAILURE
    [INFO] ————————————————————————
    [INFO] Total time: 35.875s
    [INFO] Finished at: Wed Jul 24 15:52:20 IST 2013
    [INFO] Final Memory: 13M/155M
    [INFO] ————————————————————————
    [ERROR] Failed to execute goal com.lazerycode.jmeter:jmeter-maven-plugin:1.4.1:jmeter (jmeter-tests) on project jmeter-demo: There were te
    [ERROR]
    [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR]
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

    Could you please guide me in resolving this.

    Thanks in advance

  3. Pingback: How to write JMeter tests for your GWT-RPC servlet | Ribble's Code

  4. Hi… I got Build Success But Tests run: 0 Failures: 0, Errors: 0, Skipped: 0

    Tests run should be display 1 instead of 0
    C:\jmeter-demo>mvn install
    [INFO] Scanning for projects…
    [INFO]
    [INFO] ————————————————————————
    [INFO] Building jmeter-demo 1.0-SNAPSHOT
    [INFO] ————————————————————————
    [INFO]
    [INFO] — maven-resources-plugin:2.5:resources (default-resources) @ jmeter-dem
    o —
    [debug] execute contextualize
    [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
    i.e. build is platform dependent!
    [INFO] skip non existing resourceDirectory C:\jmeter-demo\src\main\resources
    [INFO]
    [INFO] — maven-compiler-plugin:2.3.2:compile (default-compile) @ jmeter-demo -

    [INFO] No sources to compile
    [INFO]
    [INFO] — maven-resources-plugin:2.5:testResources (default-testResources) @ jm
    eter-demo —
    [debug] execute contextualize
    [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
    i.e. build is platform dependent!
    [INFO] skip non existing resourceDirectory C:\jmeter-demo\src\test\resources
    [INFO]
    [INFO] — maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ jmete
    r-demo —
    [INFO] No sources to compile
    [INFO]
    [INFO] — maven-surefire-plugin:2.10:test (default-test) @ jmeter-demo —
    [INFO] Surefire report directory: C:\jmeter-demo\target\surefire-reports

    ——————————————————-
    T E S T S
    ——————————————————-
    org.apache.maven.surefire.util.SurefireReflectionException: java.lang.reflect.In
    vocationTargetException; nested exception is java.lang.reflect.InvocationTargetE
    xception: null
    java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
    java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
    sorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(
    ReflectionUtils.java:164)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke
    (ProviderFactory.java:110)
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(Suref
    ireStarter.java:175)
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWh
    enForked(SurefireStarter.java:107)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:
    68)
    Caused by: java.lang.NoClassDefFoundError: junit/framework/TestCase
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14
    2)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    at org.apache.maven.surefire.util.DefaultDirectoryScanner.loadClass(Defa
    ultDirectoryScanner.java:98)
    at org.apache.maven.surefire.util.DefaultDirectoryScanner.locateTestClas
    ses(DefaultDirectoryScanner.java:78)
    at org.apache.maven.surefire.junit.JUnit3Provider.scanClassPath(JUnit3Pr
    ovider.java:124)
    at org.apache.maven.surefire.junit.JUnit3Provider.invoke(JUnit3Provider.
    java:75)
    … 9 more
    Caused by: java.lang.ClassNotFoundException: junit.framework.TestCase
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    … 25 more

    Results :

    Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

    [INFO]
    [INFO] — maven-jar-plugin:2.3.2:jar (default-jar) @ jmeter-demo —
    [INFO]
    [INFO] — maven-install-plugin:2.3.1:install (default-install) @ jmeter-demo –
    -
    [INFO] Installing C:\jmeter-demo\target\jmeter-demo-1.0-SNAPSHOT.jar to C:\Users
    \Krishna\.m2\repository\com\example\jmeter-demo\1.0-SNAPSHOT\jmeter-demo-1.0-SNA
    PSHOT.jar
    [INFO] Installing C:\jmeter-demo\pom.xml to C:\Users\Krishna\.m2\repository\com\
    example\jmeter-demo\1.0-SNAPSHOT\jmeter-demo-1.0-SNAPSHOT.pom
    [INFO] ————————————————————————
    [INFO] BUILD SUCCESS
    [INFO] ————————————————————————
    [INFO] Total time: 3.147s
    [INFO] Finished at: Tue Jul 30 16:38:14 IST 2013
    [INFO] Final Memory: 5M/15M
    [INFO] ————————————————————————

  5. I have a doubt and I am struggling on that… What in case if I dont want the date in the test result filename… What changes do I need to make to generate .jtl and .html file without date…. Please help me…

  6. Using above method can we Invoke Junit tests from maven project and handle them on non Gui mode of Jmeter? I am currently struggling in parallel running of Junit test case invoked from Jmeter with parameter passed from Jmeter to Junit sampler. Everything is good except parameter passing as jmeter throws internal error for passed parameter..Can I handle it and overcome this in Maven project..anything you can help me with..

  7. Hi thanks for the post
    I am using my own jmeter test and it seems to work fine.
    I get the .jtl report file but I dont see any .html file, is there some configuration for this?
    I am using latest version 1.9.1, I went to wiki and I dont see any config for html out.

    thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s