How to use a value between two different threads in Jmeter

How to use a value between two different threads:
Let us see how we can use the extracted value in one thread can be used in a different thread:
Threads
As shown below we have Two threads (Thread Group – 1 and Thread Group -2). Under Thread Group -1 we have a HTTP Request where we extracted Request_Id and Session_Id. In our example we will use the same Ids in Thread Group -2
We have BeanShell PostProcessor to log the output of the variables in the output log.
The Lines in the both BeanShell is as below:
log.info("Request Id in Thread 1 = " + "${Request_Id}");
log.info("Session Id in Thread 1 = " + "${Session_Id}");

Now if we run the Script we get the below output:
Id_Output1
As we see the values of both the IDs are not passed to Thread Group-2.
Now we will add few lines in the BeanShell PostProcessor as below and run again:
${__setProperty(Request_Id,"${Request_Id}")};
${__setProperty(Session_Id,"${Session_Id}")};
log.info("Request Id in Thread 2 = " + ${__P(Request_Id)});
log.info("Session Id in Thread 2 = " + ${__P(Session_Id)});
Id_Output2

As we see the values are passed to Thread Group -2.
Few lines on : ${__setProperty(Request_Id,"${Request_Id}")};
__setProperty() – The setProperty function sets the value of a JMeter property and is a Jmeter built-in function. Detail here
Request_Id – name of the property that will be set.
${ Request _ Id} – Value of the property and is the variable that was referred to as in Regular Expression Extractor.
This is the line that is responsible for transferring the value between threads. Now we can access  the value of ${ Request _ Id} from any other thread group.
Note: Properties are not the same as variables. Variables are local to a thread; properties are common to all threads, and need to be referenced using the __P or __property function.
Point to note:
Sometimes it may happen that Thread Group-2 will run before Thread Group -1. Then we will not get the required result. Example is as shown in below screenshot:

Not_expected

Here Thread Group-2 ran before Thread Group-1, so the variables for ThreadGroup-2 are not set.
To avoid such situation we need to check the option “Run Thread Groups consecutively” option under Test Plan as shown below:

TestPlan

Comments

Post a Comment

Popular posts from this blog

Steps to Analyze AWR Report in Oracle

Correlation and checking in Results