Running Jmeter scripts with Jenkins

How to run a Jmeter script with Jenkins and generate emailable reports

Jmeter is a great open-source tool for performance and functional testing. How we use jmeter for testing and sharing reports with the team is a key to improving product quality. We wanted to make the tests written in jmeter scripts available to everyone in the team. This meant that everyone had easy access to run tests, had minimal need for any setup and have options to automatically get reports on email. Also important was the ability to configure tests at run time by giving run-time parameters for environment, number of users, duration of test, etc

With these requirements in mind we looked for a continuous integration tool like Jenkins to run the configurable jmeter tests and send reports. Here are the steps needed to achieve the same

1. Record a Jmeter script.

2. Make it parameterized.

3. Create a mavenized-directory structure with pom.xml.

4. Setup Jenkins and create job in jenkins with right .

5. Push jmx in git and run jmeter test in Jenkins with dynamic parameters.

Record a Jmeter script

image02

There are many ways of recording a script in a jmx format. Most popular ones are Badboy, Blazemeter plugin for chrome. I used the blazemeter plugin for recording, and then exported it into a jmx format.

For using blazemeter first open the website that you have to record and then click the record button in the plugin, after you have done recording click on the .jmx button, automatically a file will be downloaded.

Now open that file in Jmeter and perform necessary changes and parameterization.

How to make the Jmeter script parameterized

In the following example, we have 3 variables whose values could be supplied at run-time. The variables are host-name, number of threads (users) and the test duration. Follow the below steps

To have a parameterized variable in Jmeter, specify the variable name in bracket and default value after that

${__P(variable,default-value)}

Go to the request and change the Server Name or IP: to

${__P(host,default_host)}

also in Thread Group change Number of Threads: to

${__P(thread,2)}

and in Duration (seconds):

${__P(duration,100)}

image01

The same can be done for host name as mentioned inside a http sampler.

Mavenised-directory structure with pom.xml

image00

To run these script from Jenkins we need to setup Maven and create a pom.xml as follows:

pom.xml(Project Object Model) contain the information about the

project and configuration details used by Maven.

The pom.xml has the jmeter-maven-plugin info that is used to execute Jmeter tests in jenkins.

Directory Structure

Directory structure should be as follows

        Main folder → src → test → jmeter → file.jmx

                     → pom.xml

This directory structure is mandatory. We will be running this Jmeter script from a github repository. So we need to push the folder (having the pom and the test script) to github, Follow the below steps to push the script into github

1. Create a repository in your github

2. Go to directory from command prompt

3. do the following

        → $ git init

        → $ git add .

        → $ git commit -m ‘First-commit’

4. Copy the link from your repository

        → $ git remote add origin remote-repo-link

        → $ git remote -v

        → $ git push origin master

Now you can check your repository, you will find you script in the same folder structure as described. We will be using this repository in Jenkins so we need to setup Git in Jenkins.

Setting up Jenkins with git and maven

Prerequisite to running Jenkins is Apache tomcat. If you already have Jenkins setup either locally or remotely skip this section. If not, then follow this link for installing Apache tomcat in your machine Installation.

To set up Jenkins, follow the setup.

Installing plugins

Now open Jenkins from your browser localhost:8080/Jenkins.

Go to Manage Jenkins → Manage Plugins → Available and add the following plugins:

1. Maven Integration plugin

2. Git plugin

3. GitHub plugin

4. Performance plugin

5. Email Extension Plugin

Also, set path in environment variables in your system. Download Maven and Java-jdk. Now got to environment variables and add path of maven and jdk in the path section.

Go to my-computer → properties → Advanced system settings → Environment variables → path → edit.

Now add path at the end with a semi-colon(;).

Now mention the path information in Jenkins. Go to Manage Jenkins and click on configure system. We will be setting java_home and maven_home.

image07 image06 image03

Now we will be setting up email so that the reports will be mailed after Jenkins has successfully build the Jmeter. Make sure you are using a gmail account for SMTP if not then you have to make changes accordingly.

image13

If you are done with changes then now apply changes and click on New Item. Create a free style project and do the following so that we can run a Jmeter script.

1. Check Build with parameterized option → Add parameters → text parameters

image11

Repeat for other parameters [duration and host name, in this example].

2. Now specify the git repository path in the source-code section

image10

Add credentials in case of private repository

3. Add build step: Select Add build step option and select Invoke top-level maven targets

Set (Default) in Maven Version

Set “verify” in Goals [refer pom.xml]

image12

4. For Jmeter report:

Add post-build step→ Publish-performance report → Add new report → Jmeter

        Now write **/*.jtl

        This will create a .jtl report file for the Jmeter script you have build

Add one more report Add new report → Jmeter

        Now write **/*.jtl-report.html

        This will create an HTML report file for the Jmeter script you have build

image08

5. Sending reports on mail

Add a Post Build action→ Editable Email → Attachments

add the file you want to mail,

here you need to write **/*.jtl,**/*.jtl-report.html

In Project Recipient List add the mail id you want to mail it to and click APPLY. Enter relevant subject & email content based on the nature of the test. You may also include the value of the dynamic parameters in the email content as

“Environment on which the test was run: ${host}”

6. Add post build action → Archive the artifacts → Files to archive →                                                                          **/*.jtl , **/*.jtl-report.html

Now go to “Build with parameters”you will find the three variables that you have added in your Jenkins project.

Specify the values and click Build

image05

After few Builds (at least 2) go to Performance Report and you can see graph according to your page performance

image04

Or by clicking on the Last Successful Artifacts → yourFileName.jtl-report.html,
you will get output like this. 

image09

That’s it !!! Now you have a parameterized Jmeter script which will mail you the reports in HTML and jtl format. There are many other options in jenkins which can be used based on the need of the application being tested like auto-scheduled, run with dynamic parameters etc.

Advertisements

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