How to get Thread Group number - jmeter

I am trying to get the Thread Group number of the thread group executing a request so I can correlate it with a property (or maybe a CSV line), but I am having a hard time getting the number alone.
I am able to get the full thread name (including the number) with thread.getThreadName(), but I would need to isolate the number from the name and I would like to skip that part if possible.
Methods that I have used:
ctx.getThreadGroup().getName() returns Thread Group
ctx.getThreadGroup().getThreadName() returns null for some reason
ctx.getThreadNum() returns 0 as it is the first thread to run inside the Thread Group
ctx.getThread().getThreadName() returns Thread Group 1-1
ctx.getThread().getThreadNum() returns 0
I know the number has to be stored somewhere as the log prints this line:
2022-07-10 13:31:10,313 INFO o.a.j.e.StandardJMeterEngine: Starting ThreadGroup: 1 : Thread Group
Where can I find this information?
Thank you.
Regards.

I think you're looking for ctx.getThreadGroup().getNumThreads() function
More information: Top 8 JMeter Java Classes You Should Be Using with Groovy

Related

In jmeter, how can we restrict each thread to read on csv line only?

Objective:
I have csv files with unique data e.g say 200 lines. I want to run endurance test, which executed for duration e.g 10min and avail a unique data from csv.
Setup:
Conisdering this, i did stepping thread setup as below with "shared mode: All threads". And i was expecting each min/60sec, 20 new threads will add and so on till 10min (expecting 200 threads at the end). With Stop thread EOF- True ( want to process data once no dup) and Recycle of EOF= False.
However,API executed in one min only and csv data read by few threads < 20 and done (EOF encounter), though jmeter honoring next 9min without executing my API's.
Expectation : 200 threads will read 200 lines i.e each thread 1 line and occurance of thread will be 20 threads in 60sec. Same like diagram, without doing Stop thread EOF=false. In nutshell, each thread execute once and then stop like kind of stuff.
Any sugeestion, how could i acheive this? Thanks!!
Given your setup each thread will read next line from the CSV file on each iteration
For example:
Thread 1 starts
Thread 1 reads line 1 from CSV
Thread 1 executes 1st sampler, response time is 1 second
Thread 1 executes 2nd sampler, response time is 2 seconds
Thread 1 starts 2nd iteration
Thread 1 reads line 2 from CSV
etc.
So you either need to switch to the Thread Group implementation which has Threads iterations limit setting like Concurrency Thread Group or Arrivals Thread Group or Free-Form Arrivals Thread Group
If you want to continue with your Stepping Thread Group - you need to slow down your threads using Timers

Loop threw Thread Group with Jmeter

I got a thread group of 10 users. I want to to connect these 10 users to an URL path mysite.com/1.
Then I want to loop a second time on these thread group and connect them to mysite.com/2.
And I need to repeat this process 10 times (i.e until mysite.com/10)...
I there a simple way of achieving that? I've already tried to use counter but what it does is connecting user1 to /1, user2 to /2 and so on...
Add While Controller to your Thread Group and put the following expression to the "Condition" area:
${__javaScript(${counter} < 11,)}
Add Counter test element as a child of the While Controller and configure it as follows:
In your HTTP Request sampler refer the incrementing value as ${counter} where required.
See How to Use a Counter in a JMeter Test to learn how to properly generate incrementing values in JMeter tests.

Get number of threads in a thread group from the setup thread group (jmeter)

I have a JMeter test plan which performs a simple action once.
The problem I am having is that my test setup needs to know how many threads the thread group will have. To make things clearer, here is a simple representation of the test plan:
setUp Thread Group
needs to know the number of threads in the below thread group
Thread Group
The number of threads for this thread group is determined via the "Number of Threads (users)" Thread Property.
I can dynamically get the number of threads from within the thread group, but I can't find any way of getting this number from within the setup thread group instead.
Any ideas?
Ideally I was hoping to find something like ${__groovy(ctx.getTestPlan().getThreadGroupByIndex(1).getNumThreads())}, but I can't find anything of the sort.
To be clear, I'm looking for the number of threads as assigned directly in JMeter in the Thread Group properties. This question has absolutely nothing to do with BlazeMeter and is therefore not a duplicate of Get number of threads (defined via BlazeMeter) in a thread group from the setup thread group (jmeter)
You can try defining a User Defined Variable in the Test Plan, let's say "Users_test" and assign it the number of virtual users you want to run the test.
Then simply use that variable in the Thread Group "Number of Threads (users): ${Users_test}, and you can do the same in the setUp.
The fast that you don't know the number of threads indicates that your test is badly designed.
The fact that you have already been given the answer for the same question 6 hours before indicates that you are unwilling to learn and prefer the community to solve problems for you.
Just in case I will repeat using simpler words:
if you need to overcome a JMeter limitation you have to go for scripting
the recommended approach is using JSR223 Test Elements and Groovy language
JMeter API reference lives at https://jmeter.apache.org/api/
Check out Groovy Is the New Black article for examples of using Groovy with JMeter API
Example code to get the number of threads for all thread groups:
import org.apache.jmeter.engine.StandardJMeterEngine
import org.apache.jmeter.threads.ThreadGroup
import org.apache.jorphan.collections.HashTree
import org.apache.jorphan.collections.SearchByClass
import java.lang.reflect.Field
StandardJMeterEngine engine = ctx.getEngine()
Field test = engine.getClass().getDeclaredField("test")
test.setAccessible(true)
HashTree testPlanTree = (HashTree) test.get(engine)
SearchByClass<ThreadGroup> threadGroupSearch = new SearchByClass<>(ThreadGroup.class)
testPlanTree.traverse(threadGroupSearch)
Collection<ThreadGroup> threadGroups = threadGroupSearch.getSearchResults()
threadGroups.each {
log.info("Thread Group: " + it.getName() + "; Number of threads: " + it.getNumThreads())
}
Demo:

Jmeter: Is it possible to run a particular thread group after all the other thread group gets completed

In JMeter, I have a requirement where I want to run a particular thread group after all the other thread groups complete their run, I know the tearDown thread group has the similar behavior but unfortunately, the logic has to be part of my regular thread.
Let's say there are 4 thread groups A,B,C & D in my test plan and I want the thread group D only to be executed after A, B & C will complete their run.
Can we achieve this without using "setup, teardown & Run groups one at a time" ??
Problem ScreenShot:
I can suggest 2 options:
Use Inter-Thread Communication Plugin. See example test plan for details.
If for some reason you are not in position to use JMeter Plugins you can achieve the same using JMeter Properties like:
When Thread Group A finishes set a JMeter Property, i.e. ThreadGroupADone=true using __setProperty() function like
${__setProperty(ThreadGroupADone,true,)}
In Thread Group D:
Add While Controller at the beginning of the Thread Group and use the following condition:
${__javaScript("${__P(ThreadGroupADone,)}"=="false",)}
Add Test Action sampler as a child of the While Controller and configure it to pause for a reasonable amount of seconds, i.e. 5 so each 5 seconds While Controller will check ThreadGroupADone property value and if it is still false - sleep for another 5 seconds. When property value will become true - Thread Group D will proceed.

Jmeter: Parallelly running Thread Group execution gets delayed

I have 3 Thread Groups: Setup Thread Group, Thread Group 2(which has the http request which does the download of the file), Thread Group 3 under a single Test Plan. The thread count=3 for each. The checkbox 'Run consecutively' is deselected in the Test plan.
The Setup thread group has all the pre-requisite requests which are needed to be executed prior to Thread Group 2 and 3. The Setup Thread Group(having multiple https requests) writes some authentication tokens to 2 different csv files which will then be utilized by the other 2 thread groups respectively when they get executed in parallel at later point of time. So, csv file1 will be used by Thread Group 2 and csv file2 will be used by Thread Group 3.
First, the Setup Thread Group gets executed.Then the Thread Group 2 and Thread Group 3 get executed in parallel. Everything seems fine when all of the threads of Setup Thread Group get the successful response:200.
But when there is any erroneous response like 500 in one of the Setup Thread Group thread Response, the csv files get less number of entries(which is fine); and the Thread Group 2 and Thread Group 3 do not run in parallel. The Thread group 3 runs first. Then after some time gap, the Thread group 2 runs. What's the cause and resolution for this problem?
1) Set Up Thread: Extract the response code or any response message of the Last sampler of the Set Up Thread.
2) Before proceeding Thread group 2 or 3 add some constant time and a BSF pre-processor. validate the condition of the extracted value(either response code/response message).
If the anticipated value is found, proceed with Thread group 2 and 3.
If not Add some constant time within the BSF(define a condition).
3) do the simlar approach, to wait for Thread Group-3 to ensure Thread Group-2 has ran succesfully.

Resources