DEV Community

adamleszko
adamleszko

Posted on

Guide to Handling Variables in JMeter

Introduction

Load testing is an integral part of software testing, focusing on assessing application performance under different load conditions. Apache JMeter, an open-source tool developed by the Apache Software Foundation, has gained popularity as a versatile load testing tool. It supports various protocols and provides extensive features for creating and executing complex load testing scenarios.
One crucial aspect of load testing is handling variables effectively within JMeter, which allows testers to create dynamic and realistic test scenarios.
This article explores different approaches to variable handling in JMeter, providing practical insights and examples to optimize your performance testing endeavors.

Variable handling

In load testing, variable handling plays a critical role in creating dynamic and realistic test scenarios. Variables enable testers to parameterize their tests by substituting dynamic data during test execution. They allow the inclusion of dynamic data, such as user credentials, session IDs, or random data, into requests, leading to more accurate test results.
Additionally, variables facilitate data-driven testing, where external data sources drive test scenarios. Testers can read data from files, databases, or CSV files and use variables to pass that data into requests, making tests more flexible and reusable.

Hardcoded Values in Samplers/HTTP Requests

In JMeter, the simplest way to manage variables is to directly insert the values into samplers or HTTP requests. While this method is limited in terms of versatility and scalability, it can be beneficial for short tests or when variables do not change often. It does, however, necessitate revisions to test scripts, which can be time-consuming as test cases grow.

Thread Group:
Image description

HTTP Request (Sampler):

Image description

In this example variables like number of threads, duration, protocol, URL and any parameters sent in the request are all hardcoded. To enhance the script, it's recommended to replace the hardcoded value with variables that can be easily managed in one place and updated through the whole JMX script.

PROS: simple, straightforward, suitable for tests with fixed parameters
CONS: not flexible and scalable, time-consuming to modify values, requires updates to test scripts

User Defined Variables Config Element

JMeter provides the User Defined Variables config element, allowing testers to define variables within the JMX script and reference them in samplers or other elements. This approach centralizes variable management within JMeter's UI, making it easier to manage and update variables across multiple samplers. It supports data-driven scenarios and reduces redundancy in test scripts.

New User Defined Variables config element:
Image description

Updated Thread Group and HTTP Request:

Image description

Image description

Where variable names, as described in the User described Variables element, are used instead of hardcoded values, you can now declare and define variables that can be used throughout the test plan.

PROS: central management in JMeter’s UI, support data-driven scenarios
CONS: limited to JMeter UI, cumbersome for considerable number of variables

Variables Defined in CLI Script (Passed via JMeter CLI)

JMeter allows passing variables from a CLI script to the JMX script using the JMeter CLI. This approach enhances script reusability and maintainability by defining variables externally. Testers can run tests with different sets of variables without changing the JMX script. However, it requires scripting knowledge, introduces complexity, and lacks control via JMeter's UI.
Only the User Defined Variables element must be updated (Thread Group and Samplers remain unchanged because they use the same variable names):

Image description

Instead of explicitly defining the value of the variable in User Defined Variables element, you need to further pass it to be defined in CLI script calling the JMX script. To define the values in a PowerShell script, you need to use:

-Jext_protocol = “https”
-Jext_serachEngineUrl = “www.google.com” 
-Jext_textToSearch = “whatever”
-Jext_duration = “10”
-Jext_numberOfUsers = ”10”
Enter fullscreen mode Exit fullscreen mode

PROS: easier management via scripts, variables changed programmatically, integration with external systems
CONS: requires scripting knowledge, introduces complexity, lack of control via JMeter’s UI (need to introduce statistics via scripting)

Variables Defined in Azure Test Load Resource (Passed via BeanShell)

JMeter supports passing variables from external resources like Azure Load Test using the BeanShell scripting element. Testers can write code to fetch variables from the external resource and assign them to JMeter variables. This method is beneficial when dynamic variables need to be retrieved from an external source during test execution. However, it requires Azure knowledge and may not be compatible with other environments.
Again, only the User Defined Variables element needs to be updated:

Image description

When creating Azure Test Load resource and Tests, in Parameters section, you need to define Name and Value for each exposed variable (Name should match the one defined in quotes from ${__BeanShell(System.getenv("protocol")) statement in Value column of JMeter’s User Defined Variables element)

PROS: integration with Azure, easily configurable via Azure Test Load parameters
CONS: requires Azure knowledge, may not be compatible with other environments, lacks advanced variable handling

Conclusion

Efficient variable handling is crucial for flexible and scalable performance tests in JMeter. By adopting proper variable handling approaches, testers can create dynamic and realistic test scenarios. The methods discussed, including hardcoded values, User Defined Variables via JMeter UI, CLI scripts, and Azure Load Test parameters, offer flexibility and scalability to your performance testing efforts. Choose the approach that best suits your testing requirements and environment. By implementing these techniques, you can enhance the efficiency and maintainability of your JMeter scripts, enabling comprehensive performance testing.

Useful links

JMeter’s User Guide
Azure Test Load documentation

Top comments (1)

Collapse
 
aluwani_rambuda_09952b972 profile image
Aluwani Rambuda

Great article, really puts all the pieces together.

I found that the Variables Defined in Azure Test Load Resource (Passed via BeanShell) can also set as environment variables on your local terminal/cmd session or at the system/user level.

The following sets the protocol for the current on Terminal before launching JMeter

export protocol=https
jmeter
Enter fullscreen mode Exit fullscreen mode

This is useful for those that want to run the same test locally and via Azure Load Test.