ThreadNumber in Jmeter - jmeter

I am using the function to get threadNum but, i see that it always gets me 1 though i have setup 5 threads. This is how i have my threads setup. I was expecting it to give me 1,2,3,4,5. Is my understanding right?
and this is what i see in log.
This is with one loop. I also tried with multiple loops and i see the same result.

Not knowing the details on how you're "using" it it's hard to say what's wrong, I cannot reproduce it using the following __groovy() function:
${__groovy(ctx.getThreadNum(),)}
Demo:
And thread numbers are zero-based so it will be 0, 1, 2, 3, 4
In the above example ctx stands for JMeterContext class instance, see JavaDoc and Top 8 JMeter Java Classes You Should Be Using with Groovy for more information

__threadNum is a function not a property, so use it directly
${__threadNum}
The thread number function simply returns the number of the thread currently being executed. These numbers are only locally unique with respect to their ThreadGroup

Related

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/Groovy: getting iteration number without reference to Beanshell

Basically this in a slightly (but meaningfully!) different context. From within a Groovy script being run in jMeter (those are all the details I have at the moment), I need functionality to tell the script what iteration of the test it is currently on.
There are 2 options:
Use vars.getIteration() function
Since JMeter 4.0 you can also get current Thread Group iteration as ${__jm__Thread Group__idx}. From the Groovy script it would be vars.get('__jm__Thread Group__idx)`

Jmeter: Is it possible to increment the value of variable for every thread in Jmeter?

Example:
Thread: 50,
Ramp-Period: 0,
Loop: 1.
I have to send different value with each thread. Is it possible to do it without changing the loop count?.
If you need to add something to an existing variable it may be done via __intSum() function
__counter() function generates incremented number each time it's being called either per-thread or global
Another candidate is __threadNum() function which is basically the number of current thread.
See How to Use JMeter Functions posts series for advanced information on above and other useful functions.
Your question looks like this - jmeter unique id per thread for http request
You can also use Beanshell to create an unique ID for every thread.
You can achieve this by adding a counter under the thread group.
In the counter it's possible to set the start, increase and maximum number you want to reach.
Then add reference name in counter, which will be used in your request as ${reference_name}

Using JMeter PreProcessor and User Variables logic

I am new to JMeter and it maybe a stupid question but I still find it hard to understand the concept here.
I have a simple test.
Thread Group with a Single thread with loop count of 2
PreProcessor that place two
variables on the vars map
A loop that execute a request twice based
on the PreProcessor parameters
I expected that the preprocessor will initialize the parameter and it will use the same values twice in the request.
It looks like it’s executing the PreProcessor once pair call.
When I switch the PreProcessor with similar User Defined Variables it reuses the same value on every call.
Can anyone explain the logic here?
I am using JMeter 2.11
A PreProcessor is executed each time the HTTP Request is executed so if you have a total of 2 iterations, you should see log twice, you have it 4 times so maybe number of iteration is different than what your write or you have 2 threads.
When you use User Defined Variables, the value is computed once and then reused. Value will be different per thread.
After reading the documentations and with #UBIK LOAD PACK help I used User Variables and it worked
User Variables - are specific to individual threads.
Pre-Processor is attached to a Sampler element (e.g. http request in our case), then it will execute just prior to that sampler element running
So 4 request for different parameters because it runs before every request
User Defined Variables - It is processed at the start of a test, no matter where it is placed. For simplicity, it is suggested that the element is placed only at the start of a Thread Group. This is why I get the same value all the time

Jmeter random variable in several threads

I have a Jmeter thread group that uses the variable uuid several times throughout.
uuid is defined with 12345678-1234-4444-a123-${__Random(111111111111,999999999999)}
In other words, it starts with a fixed series 12345678-1234-4444-a123- and then randomizes the last twelve characters.
I want to run several threads at the same time, this gives the following problem.
When I define uuid as a user defined variable inside the thread group, it randomizes once and then uses that value for all my threads.
If I set it globaly, the same thing happens.
I will be running thousands of threads at the same time when I'm done, so I can't do manual solutions or read/write to disk.
Does anyone out there have experience with this?
I have been through the documentation and Google for quite a while, but can't seem to find a solution.
In short: I need to randomize a variable, use that variable throughout the thread group, and run this thread group in several simultaneous threads. The variable should have different randomized values in each different thread.
Suppose you can simply use Random Variable configuration element instead:
Variable Name: uuid
Output Format: 12345678-1234-4444-a123-000000000000
Minimum Value: 111111111111
Maximum Value: 999999999999
Per Thread (User): True
Generated value
can be accessed as ${uuid};
unique for each thread;
preserved between different samplers calls flow of each thread (not regenerated during each reference);
generated during each iteration of Thread Group.
Test Plan
Thread Group
Random Variable
...
Sampler 1
Sampler 2
...
e.g.
iteration: 1
thread: 1
sampler 1: VALUE_1-1
sampler 2: VALUE_1-1
...
thread: 2
sampler 1: VALUE_2-1
sampler 2: VALUE_2-1
...
...
iteration: 2
thread: 1
sampler 1: VALUE_1-2
sampler 2: VALUE_1-2
...
thread: 2
sampler 1: VALUE_2-2
sampler 2: VALUE_2-2
...
...
Sample script implemented for schema given above: rnd-var.jmx
As per Random Seed field description of Random Variable:
Default is the current time in milliseconds. If you use the same seed
value with Per Thread set to true, you will get the same value for
earch Thread as per Random class.
If two instances of Random are created with the same seed, and the
same sequence of method calls is made for each, they will generate and
return identical sequences of numbers.
Keep it in mind on implementing scenarios with high concurrency (as mentioned below in comments).
To overcome this issue you can use randomize seed with e.g. ${__Random(MIN,MAX)} as value of Seed for Random function field.
Just put
12345678-1234-4444-a123-${__Random(111111111111,999999999999)}
inline where you need.
If you put this in your UDV component, value is assigned once only, before threads are even started.
The behaviour is OK according to jmeter documentation. Please read it carefully.

Resources