Home > Software engineering >  Share Variables Within JMeter Thread Group
Share Variables Within JMeter Thread Group

Time:11-18

I have a thread group, which runs multiple threads concurrently. Each thread makes a request using an ID from a csv file. So different threads within the thread group can end up making a request with the same ID over time. I want to use the cookie that is returned, for the specific ID in the request, even though its made by a different thread.

At the moment I have a Regular Expression Extractor pulling the cookie value, which created a variable based on its ID, for example, where ID is 56789 and the cookie is 1234, the variable would be 56789_1234.

I then use ${__V(${id}_g1)} to pull the cookie, associated with a specific ID for another request.

(Essentially a bunch of variables are created, prefixed with the ID and the last returned cookie value, and each subsequent request can then use the ID for its request to pull out the correct cookie)

And then create the cookie as so:

import org.apache.jmeter.protocol.http.control.CookieManager;
import org.apache.jmeter.protocol.http.control.Cookie;
CookieManager manager = sampler.getCookieManager();

Cookie AWSALB = new Cookie("AWSALB","${cookieVal}","domain","path",false,Long.MAX_VALUE);
manager.add(AWSALB);

(I assign ${__V(${id}_g1)} to 'cookieVal' using jp@gc - Set Variables Action)

However, I still cant share the range of variables that are being created amongst all threads.

I've tried properties, but I believe it only works between Thread Groups, and if the groups run consecutively.

I'd like all threads within the group to be able to read all the variables extracted by other threads.

CodePudding user response:

  1. You can "stick" each thread (virtual user) to its own ID (or set of IDs from the CSV), i.e. use separate files for each user and __CSVRead() function or single file and __groovy() function to read the values
  2. If you still want to continue with your approach take a look at Inter-Thread Communication Plugin which provides a FIFO queue
  3. Another way is using JMeter Properties if form of name-value pairs of ID=cookie_value, if there is no cookie value for the ID - write the value into the property, if there is - read the value from the property instead of requesting a new cookie

CodePudding user response:

Although trying properties, I didn't seem to get it to work. Even though the properties were created (as identified in Debug Postprocessor), I still couldn't access them between threads for some reason.

However I solved this by using Beanshell Samplers before and after the request, writing out the single cookie value to an individual txt file per ID, with the txt files name being the ID. Each time any thread makes a request with this ID, it then updates the value in the corresponding txt file, and then before each request, reads the specific txt file for that ID to retrieve the last returned cookie.

  • Related