Run JMeter Tests with Maven

In this article, I will be focusing on configuring JMeter with Maven but lets first understand some basics of JMeter and Maven.

The Apache JMeter™ application is open source software, a 100% pure Java application designed to load test functional behavior and measure performance. These days, performance testing is very very important especially when the applications are targeting large number of users. There are many tools available in market, some are paid, some are free. Apache JMeter is one such free and open source software.

Though JMeter’s was initially developed for load testing web applications, it is now far more advanced. The biggest advantage of the JMeter is that it can do many things like performance and functional testing for web services, databases, FTPs or Web Servers, LDAP, JMS, trigger emails/notifications. Most of these features are implemented with plugins.

JMeter is powerful, easy to install and use and FREE! It is a Java desktop application with simple user interface. You can always download stable version of JMeter here. You can download .tgz or .zip files to your desktop and extract it. You need appropriate Java version to run the software.

JMeter comes with a reporting module which shows how effective was the test. It can even generate simple but useful graphs.

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information. It is a build automation tool used primarily for Java projects. There are three built-in build lifecycles: default, clean and site. The default lifecycle handles your project deployment, the clean lifecycle handles project cleaning, while the site lifecycle handles the creation of your project’s site documentation. Each of these build lifecycles is defined by a different list of build phases, wherein a build phase represents a stage in the lifecycle. For example, the default lifecycle comprises of the following phases:

  1. validate – validate the project is correct and all necessary information is available
  2. compile – compile the source code of the project
  3. test – test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  4. package – take the compiled code and package it in its distributable format, such as a JAR.
  5. verify – run any checks on results of integration tests to ensure quality criteria are met
  6. install – install the package into the local repository, for use as a dependency in other projects locally
  7. deploy – done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.

Lets assume we already have a JMeter (.jmx) file. Now, create a maven project within Eclipse. Next, create a new source folder src/test/jmeter. You can now add your .jmx file in this source folder. Now, we will be updating pom.xml file to achieve various scenarios.

Dynamic JMeter properties based on environment

Most of the times, we will see minumum DEV, STAGE and PROD environments. So, if we need to configure different properties for different environments dynamically, then we can create profiles to achieve this. Here is the example:

Above example has sample stage profile. Lets say if we need to run JMeter tests against Stage environment, then we can use following command. Likewise, we can create multiple profiles and run JMeter tests against those profiles.

If you notice, we had username and password in plaintext format. To make it secured, we can encrypt the values and decrypt them at runtime. In order to achieve this, you need to configure encrypted properties in POM file and use following maven plugin to decrypt the values at runtime.

Note that, you should define this plugin in each profile so that you can decrypt environment specific values.

Now its time for reporting. Reporting is one of the most important thing after performance test. Apache JMeter has built-in reporting module. You can generate useful reports with some simple configurations. First, we will modify our sample stage profile as below.

Then we will configure one more plugin in our POM file which will generate report and we are done. You just need to provide correct JMeter file name in the configuration. Here is the example.

Finally, here is complete pom.xml file. Again, you need to provide correct JMeter file name in the configuration.

You can run various JMeter tests by passing appropriate profile and password for decryption with following command:

Note: You can create multiple profiles like Dev and Prod (similar to stage in this example) and run JMeter tests by passing appropriate profile value.

Leave a Reply

Your email address will not be published. Required fields are marked *