How to invoke first thread group if another thread group failed - multithreading

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

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

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

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.

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.

Properties don't take effect until second execution

I'm running scripts that require a different thread for each user account I pull from a database. So the script starts by running a JDBC processor to get all the accounts and store them (using the "Variable Names" field) in "accounts". Then I run a BeanShell PreProcessor to convert the variable "accounts_#" to a property:
props.put("p_accounts_#",vars.get("accounts_#"));
Then, I have a thread group start. Under "Number of Threads (users)", I have
${__P(p_accounts_#)}
The FIRST time I run this script (after launching jMeter), I only get a SINGLE thread. Every subsequent time I run it, it runs for all accounts.
It seems like for some reason, the property is not being saved until the end of the first execution. This is a very big problem as when jMeter is launched without the UI, it only does a single thread every time.
Am I setting the property incorrectly? I also tried it with a Beanshell Assertion with the same result.
Just as a test, I created a new test with the bare minimum I needed to reproduce this. Here's the script (images): http://imgur.com/a/WB5J2
It's a Beanshell PreProcessor with "props.put("accounts","12");"
Then a Thread group using "${__P(accounts)}" as the Number of Threads
Then inside that thread group is a Debug Sampler outputting the JMeter properties.
At the end is a View Results Tree.
When I run it the first time, there's only one output: "Thread 1 Running".
When I run it again, there's 12 outputs, "Tread 1 Running", "Thread 2 running", etc.
I can see that for both Debug Samplers (for the first run and second run), the "Accounts" property is set to 12. But the Thread Group needed to execute TWICE before it would work.
Any ideas?
This can be solved by adding another ThreadGroup called a 'setUp ThreadGroup' to contain the setup portion. If you put all of your staging steps into this type of threadgroup, it will run prior to any other threadgroups. You can then have your preprocessor, or move the logic to a beanshell sampler if you'd like, and set the property from there.

Is it possible to run JMeter samplers concurrently?

So to explain my situation:
I have a JMeter test plan that runs some test groups constantly in a loop. In addition to this I need to have multiple sampler requests go through together each minute (to simulate spiked usages). I can't set a constant timer to delay each of these because some may finish up quicker than others and they won't be in sync.
Is there a way to make multiple test groups send a request every minute the test is running?
OR
Is there a way to put all these samplers in 1 thread group and make them all run concurrently?
As far as I'm able to understand your use case, you need 2 Thread Groups.
First Thread Group which is SOAP Sampler A
Second Thread Group which is SOAP Sampler B
Then you need to set different variables for both thread groups to make them behave according to your use case and implement spikes you need.
Important: make sure that "Run Thread Groups consecutively" under your test plan is UNCHECKED elsewise you'll be having SOAP Sampler B running after SOAP Sampler A, not in the same time.
Lets consider your scenario is,
5 Users hitting 5 URLs(samplers) simultaneously.
So what you need to do is, in your Test Plan, add 5 Thread Groups. In each Thread Group configure the number of Threads to 5 and Ramp Up to 0.
Now, add one HTTP Request sampler in each Thread Group. Configure each sampler according to the URL you want to test.
Add Listener(s) to your Test Plan. Save the Test Plan and Run your test.
Make sure you haven't selected the "Run Thread Groups consecutively" in the Test Plan.

Resources