Introduction to Load Testing with JMeter

Introduction to Load Testing with JMeter

JMeter is a load testing tool to measure the performance of web applications. It is open-source and written in Java.

Key concepts in JMeter

JMeter test script is developed by adding components in the Test Plan pane on the left side. To get a general idea about the components and what can be achieved, let’s go through the various categories of components.

CategoryDescription
Samplerdoes the actual work in JMeter by sending requests to the server
Thread Groupdefines the number of simulated users (also called threads)

Usually, samplers are child elements of the Thread Group.
Logic Controllerdefines the logic of the test script like conditions and loops
Listenersused to view, save, summarize and visualize test results.
Configuration Elementsdefines defaults and variables for later use by samplers
Assertionsperforms additional checks on the sampler results like size, duration, pattern matching etc.
Timersintroduce a delay between requests (e.g. simulate user think time)
Pre Processorsmodify the samplers before they are executed
Post Processorsprocess the results of the samplers.

Often used to extract data from the response and store it in variables.

Samplers

The samplers do the actual work of sending requests to the server application under testing.

The most common sampler is HTTP Request.

There are a number of samplers for various protocols like FTP, SMTP, TCP, Mail Reader, LDAP, JMS, BOLT etc.

JDBC Request allows us to send a request to a database (an SQL query).

There are also script samplers where we can write our own code to generate a request or process previous results (more about scripts here).

Logic Controllers

Logic Controllers are used to define the conditional logic, loops and randomize the order of requests etc.

Following is a short description of all the controllers:

ControllerDescription
Loop Controllerexecutes child components a given number of times in a loop

If Controllerruns its children only if the given condition is true
While Controllerkeeps running its children in a loop while the given condition is true
Switch Controllerruns one of its children based on a given ‘Switch Value’.

We can use variables to dynamically calculate the ‘Switch Value’.
ForEach Controllerloops through the values of a set of related variables.



For the above configuration, the ForEach controller will look for inputVar_1 in the first iteration. If the variable is found, the children are executed with the value of inputVar_1 stored in the returnVar variable. Next iteration, the controller will look for inputVar_2 and so on.

Typically, these variables are set earlier by a script after extracting them from a response.
Transaction Controllergenerates an additional sample that measures the overall time taken to perform the nested test elements.
Simple Controllerhas no logic, it is just used to group components together. You can think of it as a filesystem folder.
Only Once Controllerexecuted only once i.e. during the first iteration of any looping parent controller.

If the parent looping controller restarts, Only Once Controller will also be reset.
Interleave Controllerruns one of the child components alternately during each loop iteration
Random Controlleracts similarly to Interleave Controller, except that it picks one of the child components at random for each pass instead of going in order.
Random Order Controllerexecutes each child element once but in random order.

You can think of it as a Simple Controller with the random ordering of child elements.
Throughput Controllercontrols how often child elements should be executed in terms of a number of times or percentage.

It is badly named, doesn’t control throughput.
Runtime Controllercontrols how longs its children will run. Keeps on running its children until a given duration.
Module Controllerallows reusing a Test fragment
Include Controllerallows including an external JMX file
Critical Section Controllerensures that children are executed by only one thread at a time.

It takes lock only within one JVM, so won’t work with distributed testing.

Listeners

Listeners allow us to view, save, summarize and visualize results.

‘View Results Tree’ is the most common way of viewing the results, particularly during test script development. It lists all the requests made along with complete request and response data.

Another useful visualizer is ‘View Results in Table’. It creates a row for every request (sampler). The output can be exported to CSV file which is much easier to analyze and summarize than the results of some of the other listeners.

Following is the output of Graph Listener.

You can find more about listeners here.

Configuration Elements

Configuration Elements are used to set up defaults and variables for later use by samplers.

ElementDescription
CSV Data Set Configreads a row from CSV file every iteration and split it into variables.

Often used for separate user logins for each Thread Group user.

Read more about it here.
HTTP Request Defaultsallows us to set default values for HTTP Request.

It lets us change common HTTP Request config values from a central place and also makes it easier to create new HTTP Request as most of the config values can be inherited from the defaults.

We have similar configuration elements for sampler also like FTP Request Defaults.
HTTP Cookie Managerstores and sends cookies like a web browser.

Without HTTP Cookie Manager, cookies will not be sent back with requests.

Each user thread has its own separate cookie store.

It also lets us manually add a cookie to be sent with requests.
HTTP Cache Manageradds caching functionality to HTTP requests to simulate browser cache feature.
HTTP Header Manageradds or overrides HTTP request headers
User Defined Variableslets us define variables.

There is also a ‘Random Variable’ element that will have a random value within a given range.
Counterdefines a variable that increments with each loop iteration

Variables

Variables are an important and useful concept in JMeter. They can be defined in scripts (like JSR223 Sampler), CSV Data Set Config etc.

A variable can be defined and given a value in JSR223 script as follows:

vars.put("myVar", 1);

Variables can be used almost anywhere in JMeter e.g. Sampler description, request header or body etc.

The syntax for using a variable is ${variable}

For more information on variables and scripting in general, see JMeter Function Reference.

Assertions

Assertions let us perform additional checks on sampler results.

There are a number of Assertions that perform pattern matching or validate the content type of response. These include Response, HTML, XPath, XML, Compare, JSON and MD5Hex Assertions.

JSR223 and BeanShell Assertions allow us to write custom scripts to perform additional checks.

Duration Assertion lets us set a maximum amount of response time for the sampler to be considered successful.

Size Assertion allows us to perform checks on the size of the response.

Timers

Following are the descriptions of some of the key timers:

ElementDescription
Constant Timerintroduces delay for the given number of milliseconds
Uniform Random Timerintroduces delay for a random number of milliseconds between a given range

Gaussian Random Timer and Poisson Random Timer are other similar timers.
Constant Throughput Timerintroduces a delay to execute the requests certain number of times in a given duration like 5 times per minute

Precise Throughput Timer is similar with more configuration options.
Synchronizing Timerpauses the threads till a given number of them are paused, then start them at the same time

Helps simulate a number of users making a request at the same time
JSR223 Timerintroduces delay based on the code written in one of the supported scripting languages

Beanshell and BSF are other similar timers.

Further Reading

2 Replies to “Introduction to Load Testing with JMeter”

Leave a Reply

Your email address will not be published.