How do I pass a variable from one Thread Group to another in JMeter

JavaTestingJmeterBeanshell

Java Problem Overview


I have a JMeter test with 2 Thread Groups - the first is a single thread (which creates some inventory) and the second has multiple threads (which purchase all the inventory). I use BeanShell Assertions and XPath Extractors to parse the returned value (which is XML) and store variables (such as the ids of the items to be purchased).

But, values that are created in the first Thread Group, whether extracted into standard ${jmeter} type variables, or ${__BeanShell(vars.get("jmeter"))} type vars, are not available in the second Thread Group. Is there anyway to create a variable in the first Thread Group and make it visible to the second?

Java Solutions


Solution 1 - Java

I was not able to do this with variables (since those are local to individual threads). However, I was able to solve this problem with properties!

Again, my first ThreadGroup does all of the set up, and I need some information from that work to be available to each of the threads in the second ThreadGroup. I have a BeanShell Assertion in the first ThreadGroup with the following:

${__setProperty(storeid, ${storeid})};

The ${storeid} was extracted with an XPath Extractor. The BeanShell Assertion does other stuff, like checking that storeid was returned from the previous call, etc.

Anyway, in the second ThreadGroup, I can use the value of the "storeid" property in Samplers with the following:

${__property(storeid)}

Works like a charm!

Solution 2 - Java

According to JMeter documentation:

> 16.12 Sharing variables between threads and thread groups
> > Variables are local to a thread a variable set in one thread cannot be read in > another. This is by design. For variables that can be determined > before a test starts, see Parameterising Tests (above). If the value > is not known until the test starts, there are various options: > > > > 1. Store the variable as a property - properties are global to the > JMeter instance > 2. Write variables to a file and re-read them. > 3. Use the bsh.shared namespace - see 16.8.2 Sharing Variables > 4. Write your own Java classes

Another way to pass variable between the threads is to use jmeter-plugins as mentioned by Andrey Botalov below.

But I found that it is a bit confusing to use it first time but it gives full control of variable during passing from thread to thread. Follow my example with BeanShell usage and you see how easy it is:

Project stucture Next referring to sections in picture bellow:

(1.1) Here I created custom variable in User Defined Variables (or you can do it with BSF Proccessor - disabled in this example (1.2))

(2.1)(2.4)I successfully used variable in first thread - nothing special :)

(2.2)Added BeanShell PostProcessor and customized my variable

(2.3)Added it to queue

(3.1) In second thread - variable is taken from queue - with any name you want. But be careful, use wisely Timeout, because this thread will wait til previous finish so it can get modified variable (experiment with some long response)

(3.2)(3.3)(3,4)That repeated steps of using and modifying variable

(3.5) Variable is sent once again in new queue - so provide new name to it

(4.1)(4.2)(4.3) Grabbed modified variable from new queue works like charm

Warning

  1. If you add more threads then add some Counter to Thread Group with variable and add this variable name to queue name - do the same in Thread Group where you try to catch queue so queue will have unique name for each thread (write a comment if you need some clearer explenation)

  2. If you have more than one http Request in one Thread Group then add thread communication pre processor as a child of last (or other if you want to achieve some custom thing) http Request

Play, modify, customize to get best result :) Adding more threads can result in unwanted behavior so you need to be watchful.

Information about project structure

Solution 3 - Java

I found which I believe is the most simple way to get this done.

Use

Bean Shell PostProcessor

Set in one Thread Group

to set the variable (http://jmeter.apache.org/usermanual/best-practices.html#bsh_variables)

import org.apache.jmeter.util.JMeterUtils;
JMeterUtils.setProperty("PC_CREATED_PROMO_CODE", "value");

OR if you want to set it to a value contained in another variable

import org.apache.jmeter.util.JMeterUtils;
JMeterUtils.setProperty("PC_CREATED_PROMO_CODE", vars.get("Extracted_PC_CREATED_PROMO_CODE"));

Get in the other Thread Group

And then from the other thread group, read it via (http://jmeter.apache.org/usermanual/functions.html#__property)

${__property(PC_CREATED_PROMO_CODE)}

Solution 4 - Java

JMeter Plugins has Inter-Thread Communication for this purpose.

There are 2 methods to use it:

  • PostProcessor/PreProcessor
  • Functions __fifoPut and __fifoPop

In my opinion PostProcessor/PreProcessor is easier to use.

Solution 5 - Java

JMeter Bean Shell Assertion

Just add a bean shell assertion use a property function to assign the value to a variable (like a global variable) which will hold the value even after it goes to other thread.

> thread group >> Add >> Assertions >> Bean Shell Assertion

${__setProperty(Global_variable_Name,${Variable_name_whose_Value_to_be_Passed})}

and then in the other thread you can to call this global variable and can use it

below is the function you need to use to call the stored value:

${__property(global_variable_name)}

https://medium.com/@priyank.it/jmeter-passing-variables-between-threads-a4dc09903b59

Solution 6 - Java

This is not possible in JMeter, because it is not normal client behavior (sharing parameters between Threads). Instead of this use one Thread-Group with Controllers:

Thread Group
+ Create inventory
+ + XPath
+ Loop
+ + Purchase inventory

Solution 7 - Java

Well this is one way to do it; follow these steps and it will work, later you can adjust it to your needs! Variables are not shared among threads (JMeter calls this a feature probably :) ). But properties are! So set your variable as a propery like so:

  1. Click your testplan and enable 'Run Thread Groups consecutively' -> this makes the thread groups run ordered and not randomly. (you can later adjust it, but for now to get it to work..)

  2. create a threadgroup called 'setup' for instance; in that thread group add a BeanShell Sampler with the following code:

    import org.apache.jmeter.util.JMeterUtils; JMeterUtils.setProperty("theNameOfYourNewProperty", "theValueOfYourPropery");

So now the property has been set! If the value you want to store as a propery is a variable allready (User definded variable or reqex variable for instance) you can do:

JMeterUtils.setProperty("theNameOfYourNewProperty", vars.get("theNameOfYourVariable"));

3) add a testgroup 'actual test' for instance with a number of threads higher than 1; add a test and to that test add a BeanShell Preprocessor with the following code:

import org.apache.jmeter.util.JMeterUtils;
vars.put("theNameOfYourNewProperty", JMeterUtils.getProperty("theNameOfYourNewProperty"));

So now you've created a variable in that thread called theNameOfYourNewProperty which has the value of your system property theNameOfYourNewProperty. In your test you can now access it like:

${theNameOfYourNewProperty}

And it will work for each thread, not only just the first thread..

Solution 8 - Java

Let' give a topic a second life :) One more way to transfer variables between threads is to write/read to file. Passing variables between threads

Solution 9 - Java

Another solution is to use the Simple Table Server to manage the dataset. This feature was add the 1.2 JMeter Plugins.

"The main idea is to use a tiny http server in JMeter Plugins to manage the dataset files with simple commands to get / add rows of data in files"

Look at the documentation : http://jmeter-plugins.org/wiki/HttpSimpleTableServer/

Regards.

Solution 10 - Java

I used set property function and teardown teardowngroup getting values from setupthreadgroup but teardown threadgroup getting 401 error .

I checked all the headers everything look like fine .not sure y teardown failed . Any idea plz advice me

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTodd RView Question on Stackoverflow
Solution 1 - JavaTodd RView Answer on Stackoverflow
Solution 2 - JavapbaranskiView Answer on Stackoverflow
Solution 3 - JavaJoviano DiasView Answer on Stackoverflow
Solution 4 - JavaAndrei BotalovView Answer on Stackoverflow
Solution 5 - JavaVajjala Vinay RaoView Answer on Stackoverflow
Solution 6 - JavaMork0075View Answer on Stackoverflow
Solution 7 - JavaFicoView Answer on Stackoverflow
Solution 8 - JavaolyvView Answer on Stackoverflow
Solution 9 - JavaVincent DaburonView Answer on Stackoverflow
Solution 10 - Javayazhdevi subramaniView Answer on Stackoverflow