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.
Category | Description |
Sampler | does the actual work in JMeter by sending requests to the server |
Thread Group | defines the number of simulated users (also called threads) Usually, samplers are child elements of the Thread Group. |
Logic Controller | defines the logic of the test script like conditions and loops |
Listeners | used to view, save, summarize and visualize test results. |
Configuration Elements | defines defaults and variables for later use by samplers |
Assertions | performs additional checks on the sampler results like size, duration, pattern matching etc. |
Timers | introduce a delay between requests (e.g. simulate user think time) |
Pre Processors | modify the samplers before they are executed |
Post Processors | process 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:
Controller | Description |
Loop Controller | executes child components a given number of times in a loop |
If Controller | runs its children only if the given condition is true |
While Controller | keeps running its children in a loop while the given condition is true |
Switch Controller | runs one of its children based on a given ‘Switch Value’. We can use variables to dynamically calculate the ‘Switch Value’. |
ForEach Controller | loops 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 Controller | generates an additional sample that measures the overall time taken to perform the nested test elements. |
Simple Controller | has no logic, it is just used to group components together. You can think of it as a filesystem folder. |
Only Once Controller | executed 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 Controller | runs one of the child components alternately during each loop iteration |
Random Controller | acts 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 Controller | executes 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 Controller | controls 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 Controller | controls how longs its children will run. Keeps on running its children until a given duration. |
Module Controller | allows reusing a Test fragment |
Include Controller | allows including an external JMX file |
Critical Section Controller | ensures 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.
Element | Description |
CSV Data Set Config | reads 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 Defaults | allows 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 Manager | stores 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 Manager | adds caching functionality to HTTP requests to simulate browser cache feature. |
HTTP Header Manager | adds or overrides HTTP request headers |
User Defined Variables | lets us define variables. There is also a ‘Random Variable’ element that will have a random value within a given range. |
Counter | defines 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:
Element | Description |
Constant Timer | introduces delay for the given number of milliseconds |
Uniform Random Timer | introduces delay for a random number of milliseconds between a given range Gaussian Random Timer and Poisson Random Timer are other similar timers. |
Constant Throughput Timer | introduces 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 Timer | pauses 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 Timer | introduces delay based on the code written in one of the supported scripting languages Beanshell and BSF are other similar timers. |
2 Comments