June 10, 2019

Using JMeter Variables With Multiple Thread Groups

Open Source Automation

Do you know how to pass JMeter variables between different thread groups? Is it even possible? Find out!

In this blog, we break down the basics of JMeter variables in relation to thread groups. Learn how to add variables to your test plan and use them to run your load test more quickly and easily, while passing on values between thread groups.

Back to top

What Are JMeter Variables?

JMeter variables are special placeholders that can be resolved to respective values. This means they are values that can be used inside the test and populated when the test runs.

Unlike properties, variables are local to a thread and not common to all threads in the test. You can access JMeter variables by going into thread groups and you can use them in any test element, except the test plan. 

đź“• Related Resource: Learn more about How to Use the SetUp Thread Group in JMeter

Get more out of your testing with BlazeMeter — now featuring the Open Model Thread Group plugin. Get started FREE today!

Start Testing Now

Back to top

How to Use JMeter Variables With Multiple Thread Groups

In the figure below we have a test plan containing two Thread Groups. In this example, these thread groups are named “First Thread Group” and “Second Thread Group.”

Thread Group

Before executing the test plan do not forget to set the check box, “Run Thread Groups consecutively.” Setting this check box is a must for executing Thread Groups one by one. Otherwise, you might be running a spike test.

Test Plan

Let’s go to Regular Expression Extractor in the first thread group. The result of the Regular Expression will be stored in the variable “second_url,” as shown in the following image.  

Regex Extractor

Next, let's look at the JSR223 Assertion in the first thread group.

JSR223 Assertion

Note the interesting construction of props.put('second_url', vars.get('second_url')):

  • __setProperty(). This is a built-in JMeter function that sets the value of a JMeter property. (A detailed description can be found here).
  • second_url. This refers to the name of the property that will be set.
  • ${second_url}. This is the variable that was stored in the Regular Expression Extractor.
  • vars. This is shorthand for the JMeterVariables class instance, it provides read/write access to all JMeter Variables in the current thread’s scope
  • props. This is shorthand for the Properties class. holding JMeter Properties. The properties are global and accessible by all threads in any Thread Group.

This line of code is responsible for transferring the value between the threads in the test. From here on out, the value of ${second_url}  can be accessed from any other thread group. This value makes testing both simpler and more scalable.

To access this value, let us look at the second request in the Second Thread Group. To increase the property value, we use another JMeter function: __property().

It takes only one argument and the name of the property returns its value. JMeter's documentation also offers more details about its various functions and variables

Got it? Good. Now let's execute the scenario!

Server name

Note that the second_url property value was set as “Server Name”. Great! You are now ready to use JMeter variables yourself.

Alternatively, you can use the Inter-Thread Communication plugin, a popular JMeter plugin that helps pass variables between threads and thread groups.

Back to top

Go Beyond JMeter Variables With BlazeMeter

Go beyond JMeter variables and start testing with BlazeMeter today. Open-source JMeter is a popular community tool used by thousands of developers BlazeMeter helps teams run JMeter load testing at scale, while also generating test data and accessing advanced reports.

BlazeMeter's continuous testing platform lets you add your JMeter test, easily calibrates it, and executes the script whenever you need from across the world. Try JMeter testing with BlazeMeter today. 

START TESTING NOW

 

This blog was originally published on June 10, 2012 and has since been updated for accuracy and relevance.

Related Resources

Back to top