Jmeter + Maven Integration – Some facts

JMeter is the most widely used open source tool for analysing the performance of different applications. Here I will be discussing not only how we can “integrate” JMeter with Maven but also discuss what common “hurdles” we usually encounters with the integration.

Maven basics are required to be known, to understand following discussion.

So lets kick off this interesting discussion in the form of incremental steps.

Step 1 — Create a Maven Project.

Lets start with creating an empty maven project using  maven ‘archetype‘ plugin with the goal ‘generate‘ ie. mvn archetype:generate.

Now maven project is in place with a default POM file generated in root folder.

Step 2 — Create a Jmeter Test Plan(Jmeter GUI).

Create a Jmeter test plan to have performance benchmarking of your application.

And place it in your maven directory — src/test/jmeter/{YourTestPlan}.jmx

Step 3 — Use ‘jmeter-maven-plugin’.

Maven plugin ‘jmeter-maven-plugin’ has the capability to interact with Jmeter plan files or project(.jmx). Add this plugin to the POM file.

<em>"&lt;groupId&gt;com.lazerycode.jmeter&lt;/groupId&gt;</em>

<em> &lt;artifactId&gt;jmeter-maven-plugin&lt;/artifactId&gt;</em>

<em> &lt;version&gt;1.10.1&lt;/version&gt;"</em>

And just attach the goal "jmeter" to a phase "verify" as

<em>"&lt;phase&gt;verify&lt;/phase&gt;</em>

<em> &lt;goals&gt;</em>

<em> &lt;goal&gt;jmeter&lt;/goal&gt;</em>

<em> &lt;/goals&gt;"</em>

Step 4 — Avoid “Out Of Memory” errors during load test.

When we executes the load test with large number of concurrent users, then usually JVM goes Out Of Memory due to less Java heap space. So to avoid it, we need to increase the java heap space. We can pass xms and xmx to 1024 with ‘jMeterProcessJVMSettings’ parameter.

<strong><em>"&lt;jMeterProcessJVMSettings&gt;</em></strong>

<strong><em> &lt;xms&gt;1024&lt;/xms&gt;</em></strong>

<strong><em> &lt;xmx&gt;1024&lt;/xmx&gt;</em></strong>

<strong><em> &lt;arguments&gt;</em></strong>

<strong><em> &lt;argument&gt;-Xprof&lt;/argument&gt;</em></strong>

<strong><em> &lt;argument&gt;-Xfuture&lt;/argument&gt;</em></strong>

<strong><em> &lt;/arguments&gt;</em></strong>

<strong><em> &lt;/jMeterProcessJVMSettings&gt;"</em></strong>

Step 5 — Pass parameters to Jmeter Test Plan through maven.

We can not live with all the parameters in load tests to be hard coded or static in test
plan, we need to have a mechanism where tests would execute for different sets of inputs.
So same we want when executing through maven.

Inside a configuration tag of ‘jmeter-maven-plugin’, use ‘propertiesUser‘ tag
where parameters can be defined which will flow inside your JMeter tests.

<em>"&lt;propertiesUser&gt; </em>

<em> &lt;threadcount&gt;${concurrency}&lt;/threadcount&gt;</em>

<em> &lt;loop&gt;${loopCount}&lt;/loop&gt;</em>

<em> &lt;/propertiesUser&gt;"</em>

Now look carefully how inputs will be communicated to JMeter, in JMeter test script,
‘threads’ in ThreadGroup are mentioned as ${__P(threadcount,)} which means threads are
not static but based on user input. To pass ‘threadcount’
from maven to test script we added <threadcount>${concurrency}</threadcount>.

That means when test is triggered through maven, then “threadcount” is passed and will be used in your Jmeter script.

And, ${concurrency} means that user will give concurrency parameter while executing maven command as “-Dconcurrency=500”.

Step 6 — What Tests to execute.

As per Step 2, we have placed our test plans in directory (src/test/jmeter).
This is the default directory which ‘jmeter-maven-plugin’ looks for.
Otherwise we can instruct the plugin to look for tests in our custom directory through ‘testFilesDirectory’ tag.

eg. <testFilesDirectory>${project.base.directory}/src/test/mydirectory/</testFilesDirectory>

By default ‘jmeter-maven-plugin’ executes all tests present in testFilesDirectory.
But, we can also configure to specify the test as well, by

Specifying <testFilesIncluded> as

  “<testFilesIncluded>

     <jMeterTestFile>test1.jmx</jMeterTestFile>

     <jMeterTestFile>test2.jmx</jMeterTestFile>

    <testFilesIncluded>”

Specifying <testFilesIncluded> Using Regex as

   “<testFilesIncluded>

       <jMeterTestFile>foo*.jmx</jMeterTestFile>

    </testFilesIncluded>”

Specifying <testFilesExcluded> as

   “<testFilesExcluded>

      <excludeJMeterTestFile>test3.jmx</excludeJMeterTestFile>

      <excludeJMeterTestFile>test4.jmx</excludeJMeterTestFile>

    </testFilesExcluded>”

Specifying <testFilesExcluded> Using Regex as

“<testFilesExcluded>

<excludeJMeterTestFile>*bar.jmx</excludeJMeterTestFile>

</testFilesExcluded>”

To even make this parameterized, use <testFilesIncluded> as follows –

<testFilesIncluded> ${TestNameToExecute}.jmx </testFilesIncluded>

And in maven command user can give the test name to execute as –“DTestNameToExecute=test1”

I suppose till this point everybody is with me. Don’t worry if there is any confusion in above steps,
as this will be clear when I integrate all the components we discussed.

Now, lets integrate what we discussed.

‘jmeter-maven-plugin’ looks like as follows in POM (go through each line carefully) —

Lets say in /src/test/jmeter I have 3 Tests (Test1.jmx,Test2.jmx,Test3.jmx).

To execute Test 1 —

“mvn verify -Dconcurrency=500 -Drampup=2 -Dloop=10 -Dapplication.base.url=www.linkedin.com -DTestName=Test1”

Isn’t it really cool!

Hope this is worth sharing 🙂

Comments