JMeter: How to utilize token generated at runtime on multiple thread groups - multithreading

Scenario is we are generating token during execution which would further be used in other threadgroup.
Like:
In first thread group, tokens will be generated for 100 users.
In second thread group, 50 users will utilize the tokens.
In third thread group, next 50 users will utilize the tokens.
Query is:
1. How do we save tokens that is generated during run time?
2. How to use first 50 tokens on second thread group and rest other 50 on third thread group?

If you need to use a Token generated by a request in following requests then use 1 Thread Group and not many.
Threads in different ThreadGroup have absolutely no relation and are considered as different users, so although you may use elements like InterThreadCommunication to do that, just DON'T.
If you use same thread group then it's just a matter of using the correct extractor to generate variables and then use them using ${varName}.

You can transfer a value between Thread Groups by converting it into a JMeter Property. According to the 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 need to transfer something from one thread group into another:
Convert the value into a JMeter Property using __setProperty() function like:
${__setProperty(foo,bar,)}
Once done the value can be accessed usine __P() function like
${__P(foo,)}
Demo:
If the value is different for different threads - you can combine the above approach with __threadNum() or __counter() functions in order to use current virtual user number or next incremented number as a prefix or a postfix for the function.

Related

Jmeter thread-groups are not running independent for different users

I have load testing scenario where test-plan has multiple thread-group and each thread-group has different type of HTTP request and this group is designed to executed in sequence .
Below is scenario I'm testing -
Test-Plan
+---Thread-Group(Register-Request)
+---Thread-Group(Container-Request)
+---Thread-Group(Subscription-Request)
+---Thread-Group(Data-Request)
+---Thread-Group(Deregister-Request)
Load testing has to follow the defined sequence. Each user-thread reads thread specific values from CSV file and during testing, JMeter output shows that:
User-threads don't move from Thread-Group(Register-Request) to Thread-Group(Container-Request) until all user threads have completed execution which looks odd to me.
Any idea what could the reason of this behaviour ?
User threads don't "move" from one Thread Group to another Thread Group, each Thread Group has its own pool of virtual users and they're not connected by any means.
So if you want each user to execute some actions (Register-Request, Container-Request, etc.) sequentially - you need to put the relevant Samplers under the same Thread Group.
If your workload model is more complex and i.e. you need to run different scenarios with different throughputs and maintain user session across Thread Groups at the same time - you can take a look at i.e. Using JMeter Variables With Multiple Thread Groups article Inter-Thread Communication Plugin or

Create unique props variable for each thread in Jmeter

I want to create properties variable for each Thread and this is unique and i can use it in another input. So does anyone can suggest me the way to do it? Is __threadNUm is the easiest way to do it?
In order to set the property you can use __setProperty() and __threadNum() functions combination like:
${__setProperty(PROPERTY_PREFIX_${__threadNum},PROPERTY_VALUE,)}
Replace PROPERTY_PREFIX and PROPERTY_VALUE with your own values
To read the property value per thread you can use __P() and __threadNum() functions combination like:
${__P(PROPERTY_PREFIX_${__threadNum},)}
Demo:
Another solution is using Inter-Thread Communication Plugin which is handy for sharing values across different threads (even if they are in different thread groups). The exact instructions will differ depending on what you're trying to achieve, you can see SynchronizationExample.jmx test plan for reference.
You can install Inter-Thread Communication Plugin using JMeter Plugins Manager

How to invoke first thread group if another thread group failed

I am new to Jmeter, and I am trying to achieve the following test plan in Jmeter 4.0.
I have 2 thread group.
First thread group will generate access token using Oauth 2.0 and the
value of access token is set to a variable.
Second Thread group will use the access token generated from first thread group and process the HTTP request.
Token expiration time is 2 hours.
I want to schedule a 8 hours test in Jmeter. How to invoke first thread group to generate token for every 2 hours and to continue thread group 2.
Any help will be appreciated !!
Put If Controller after a sampler which could fail
Use the following expression as the condition:
${__groovy(vars.get('JMeterThread.last_sample_ok').equals('false'),)}
where:
JMeterThread.last_sample_ok is a pre-defined variable indicating whether previous sampler was successful or not
__groovy() function allows execution of arbitrary Groovy code, in above example it checks if the previous sampler is not successful.
Put Module Controller as a child of the If Controller and point it to the 1st Thread Group
More information: Easily Write a GOTO Statement in JMeter
Update: use Test Fragments like:
Add Test Fragment to your Test Plan and move the logic from Thread Group 1 there
Add Module Controller to Thread Group 1 and reference the Test Fragment
In the Module Controller from Thread Group 2 reference the same Test Fragment

How Can I use Multiple cookies from Different user type Logins (Login from CSV file in a single thread) in other thread groups randomly?

I have 4 different action types and I want to run 2 action with USER 1 and another 2 Action with USER 2.
I have set up two differet threads for 2 Logins and then used these cookies in 4 different actions randomly using Beanshell post processor and Beanshell preprocessor methods.
But now I want to use single thread for Login request and allow users login with CSV data. In this case how can I get the different cookie for each Login s that I can pass them randomly in 4 actions ?
Please help.
Given you have cookies defined like:
cookie_1=Cookie #1
cookie_2=Cookie #2
cookie_3=Cookie #3
cookie_4=Cookie #4
You can use __P() and __Random() functions combination in order to get a random cookie value like:
${__P(cookie_${__Random(1,4,)},)}
Demo:
See Apache JMeter Functions - An Introduction to get familiarized with JMeter Functions concept.

Jmeter Current Thread Number?

I am using a Thread Group with Number Of Threads = 5 with an HTTP request.
In the request I want to include a parameter with the value of the thread number, e.g.,
"pageno": ${threadno}
I want to get the thread number like ${threadno}.
How can I do that?
The thread number is available as:
${__threadNum}
See: functions reference
While the above-mentioned ${__threadNum} will work in many places within jMeter, you'll need to use something else where it is not allowed, e.g., script elements within Pre/Post-Processors.
This answer explains how to get thread number or count within such a script in jMeter.
To get the number of the current thread (out of 5 in your case) use ctx.getThreadNum() which will get the number of the thread.
To get the total number of threads being used by jMeter you can use ctx.getThreadGroup().getNumThreads() or ctx.getThreadGroup().getNumberOfThreads() for total active threads.
https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterContext.html#getThreadNum()
https://jmeter.apache.org/api/org/apache/jmeter/threads/AbstractThreadGroup.html
For those looking for the number of active threads in the entire test plan, not just a particular ThreadGroup.
${__groovy(org.apache.jmeter.threads.JMeterContextService.getNumberOfThreads())}
${__threadNum} does not work well alone.
You will need use ${__eval(${__threadNum})}.
Try to use this:
int threadNum=ctx.getThreadGroup().getNumThreads();
ctx is from JmeterContext

Resources