JMeter - reusing random variables in another thread group - jmeter

I currently have a test plan where I use a random variable to submit a post request to a given URL (/app/${app_id}).
I want to also re-use the random variable app_id to poll the status of that app (/app/${app_id}/status). Note there would be multiple requests to the status URL.
My current idea is to:
have one thread group that submits the posts
save a list of the randomly generated app_ids
in another thread group, read the list of app_ids and for each app_id, loop the status request
Is this a sensible approach? If so, how can I go about saving the randomly generated app_ids and then reading them?
Also, if there is a better approach to this situation, I'm all ears :)

You can pass the values from one thread group to another one using __setProperty() function like:
${__setProperty(appid-${__counter(FALSE)},${your_variable_holding_appid},)}
Each time the function will be called it will generate a JMeter Property in form of
appid-1=foo
appid-2=bar
appid-3=baz
etc.
Where numbers like 1, 2, etc. are coming from __counter() function
In another Thread Group you will be able to access the generated values using __P() function like:
${__P(appid-${__counter(FALSE)},)}
Demo:
See Apache JMeter Functions - An Introduction for more information on JMeter Functions concept.
There is more "advanced" way of passing JMeter variables between threads and even thread groups, moreover you will be able to synchronise your threads, i.e. don't start thread in 2nd thread group until variable is not set using Inter-Thread Communication plugin.

Solution with Thread Groups is feasible, although you also can do all that in one thread group, by configuring one of the threads to be "monitor", while remaining threads submit posts. Something like this:
Thread Group
If [${__threadNum} == 1]
Samplers to check status
If [${__threadNum} != 1]
Samplers to submit posts
One reason to use 1 thread group for both types of users is if you need any sort of synchronization between writing and reading app_id list - that is easier to achieve in one thread group. Or if you already have many thread groups.
As for providing app_id to various threads/thread groups, you can use one of the approaches:
To pass IDs from one thread group to the other without a file, you can use one of the methods described here (using properties is the most popular way).
In your case, probably the easiest is to save them in one property with some delimiter, e.g. ID1,ID2,.... The "status" thread then can get this property, split it using either script or __split function to convert property into serialized variables: app_id_1, app_id_2, etc. Those variables are a natural choice for ForEach Controller.
Also first thread group could be saving app_ids into file, while the other reads from the same file with CSV Data Set Config. A bit of caution here though, if they are doing it at the same time.
If app-ids can be pre-generated, a more economical approach would be to have SetUp Thread, which generates them and saves them to CSV file (you will only need one script, e.g. BeanShell to do that, see example here). And then both thread groups read from that file using CSV Data Set Config

Related

Jmeter - How to pass multiple tokens from http login request to another http request

I am currently designing a test case for multiple users testing using jmeter and I kind of need some help.
Here's the scenario:
Suppose I have 2 different login portals with 500 different users on each portal. I want to do login, access getExamSchedule, startExam, getListQuestion, getOneQuestion, submitOneQuestion and finishExam so I create one new thread group for each request. Each thread has 2 samplers (1 for portal A, 2 for portal B).
My question is how am I supposed to pass the multiple generated tokens from login thread so that I can access the rest of the thread with different tokens. And also each http request has some response I need to extract to be used on another thread. I tried to extract the tokens and the responses to csv file but somehow it didn't work. Let me know if there's any best practice for this scenario. Thank you in advance!
so I create one new thread group for each request - normally you should create separate/different Thread Groups only for new logical groups of virtual users, for your use case I think everything could be in one Thread Group.
As per JMeter Documentation:
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.
So if you really need to pass values across different Thread Groups - you can take a look at following solutions:
__setProperty(), __threadNum() and __P() functions combination like:
To set the property:
${__setProperty(token_${__threadNum},${jmeter-variable-holding-the-token},)}
To read the property: ${__P(token_${__threadNum},)}
Use Inter-Thread Communication Plugin

JMETER Write and Read from the same CSV file between threadgroups

In JMeter, I have a thread group which creates order, I capature the relavant details of the order and write to a csv file.
I have another thread group which searches these orders, the data for which I am currently creating as part of the above thread group as a prerequsite for this step.
Rather I wanted to check if there is an option to write and read from the same csv file, just to avoid the data preparation ahead. Any suggestions on how I can achieve this? Any constraints forseen on running with multiple user writing and reading from the same file. Thanks
With 1 thread - yes.
With > 1 thread - still yes, but it will be dangerous due to race condition, writing into the same file by multiple threads will result in data corruption.
If you need to pass values between Thread Groups there are following options:
Inter-Thread Communication Plugin
__setProperty() function in 1st Thread Group and __P() function in the 2nd Thread Group if you don't want or cannot use the plugin
You can use Inter Thread Communication plugin to share data between thread groups.
You can put a string value into a queue from one thread, and then get that value from another thread, even in another Thread Group. This allows you having separate Thread Groups with some synchronization and parameter passing between them. There may be any number of FIFOs and they differ by "queue name".
Use the Inter-thread communication Post-processor to write the order information
User the Inter-thread communication Pre-processor to read the order information from another thread group
Sample Test Plan 1 Test Plan 2

Passing property between threads in Jmeter

In J meter, I am unable pass property from thread 1 to thread 2 if there is JDBC request in thread 1. If there is no JDBC request, property is getting passed from thread 1 to thread 2. Any specific reason for this, Any changes i need to do? kindly help me on this.
Passing values between Thread Groups is not something you should be normally doing, I would recommend reconsidering the design of your JMeter script so it would not be required
There are several ways of passing values between different thread groups starting from using __setProperty() and __P() functions combination, using interim CSV files, and ending up with Inter-Thread Communication Plugin, they all should work irrespective of presence/absence of the JDBC test elements in this or that Thread Group
When anything goes wrong in the absolute majority of cases JMeter will print something to jmeter.log file, first of all check it for any warnings/errors and if they are not there - check the variables/properties values using Debug Sampler

Jmeter wont read csv file with line feed properly

I have a thread which writes data to a csv file and data looks like this in that file, as you see below each data is separated by a , then line feed.
ApiKey
20a145260241463194bf84f43952da9c:dut8ghdt+iQrsmYEgKzHzF1It79aWRIjb/d1wM8U3WE=,
93,
d0e29bb7-476d-4a52-9527-e5d9bb0ac34a
In another thread I am trying to read the data so I have done the following. Added a "CSV Data Set Config" with correct filename variables X,Y,Z and my Delimiter I have tried all these A=,n or B=n or C=n, I am not able to get Y and Z properly. What am I doing wrong?
Synchronization of data between thread groups using CSV files is not the best idea as you will have to implement a form or read/write lock to ensure that only one thread is writing data to the CSV file at a time and that will cause throughput degradation.
So I would recommend going for one of the following solutions:
Use __setProperty() function to convert the data from JMeter Variables into JMeter Properties. JMeter Properties are global for the whole JVM therefore they can be accessed from another Thread Group(s). See Knit One Pearl Two: How to Use Variables in Different Thread Groups article for more information.
Use Inter-Thread Communication plugin which implements FIFO queue so you can block threads in 2nd thread group unless data from 1st thread group is available. You can install Inter-Thread Communication Plugin using JMeter Plugins Manager
Both approaches are in-memory therefore you will not need to store data in interim files.

Dependency among multiple thread groups in JMeter

I have set up a load test plan with multiple thread group, i.e. -
Registration (50% of the threads)
Place Order (10% of the threads)
Some more operations (remaining threads)
Herein if Registration thread does not succeeds than I don't want to execute remaining thread groups. In case of a single thread group I can use if controller and discard samples if one sample fails but how do I achieve it when I am using multiple thread groups.
JMeter Variables scope is limited to current Thread Group only, if you want to use If Controller basing on the condition which is set in another Thread Group - you should be using JMeter Properties instead (JMeter Properties scope is global for the whole JVM). See How to Use Variables in Different Thread Groups article for details on converting JMeter Variables into JMeter Properties.
You may also find InterThread Communication plugins useful when it comes to passing data between thread groups and setting up dependencies.
However, given your scenario you either need to pass the whole thread context (cookies, cache, whatever) which might be tricky so it would be much better putting all the samplers under the same Thread Group and use Throughput Controller, Switch Controller or Weighted Switch Controller, whatever matches your scenario the closest way. See Running JMeter Samplers with Defined Percentage Probability guide for more information.

Resources