Incremental variable in each iteration/loop in Jmeter Tests - jmeter

I have below setup for my test plan:
In POST http request, I am sending a variable myCount set to value 0.
As per above configuration,
this test will run 100 times but everytime the value of myCount is sending 0.
I want it in incremental
for example: for loop 1 value set to 1, for loop 2 value should set 2.
Please let me know how can I achieve this.
Also, I would like to know in Taurus as well.

Thread Group has a pre-defined variable which returns the current iteration number, it's ${__jm__Thread Group__idx}
if you want the counting to start from 1 - go for __intSum() function
There is __counter() function which generates an incremented number each time it's being called
There is Counter configuration element which does the same but you have additional possibility to control the number format, i.e. if you need 0001 instead of just 1 or you plan to re-use the generated variable value later on within the bounds of same request or iteration. More information: How to Use a Counter in a JMeter Test
With regards to Taurus - in case of JMeter executor it supports all the approaches mentioned above

Related

JMeter - Setting the value of a Counter config element programatically in a setup thread

Im using Jmeter 5.5. Im trying to find a way to set the starting value of a counter programmatically. Essentially what i need to do is
Start test
Read an int value from a file <-- setUpThreadGroup
Set the start value of the Counter element to this value.<-- setUpThreadGroup
Iterate through the test/threads, incrementing this as a shared variable. <--Threadgroup
Write the new value to the file. <-- Teardown
I've tried using props.put(),(__P,__setProperty), vars.put, System.setProperty, all with no success.
Is it possible to set the starting value of a counter via code? It always seems to start with 0.
If this isnt possible, is it possible to create a shared variable that can be used across threads that increments safely to ensure no duplicate variable values will ever be used?
I don't think you even need set up and teardown thread groups, you can do something like:
Read the counter from the file via __FileToString() function like:
${__FileToString(counter.txt,,)}
Once all iterations are complete you can write the new value into the file using __StringToFile() function
${__StringToFile(counter.txt,${counter},false,)}
Demo:
More information: How to Use a Counter in a JMeter Test

how to increment the number in jmeter body whenever we run jmeter run

i want to increment the batch number in my jmeter body whenever i run the jmeter run
say for example im running 1 request it should be like bleow
'batch_number_1_2022-09-27_1'
also if i run 2 request it would be , batch number remains the same for that run only counter changes fo as many request we trigger
'batch_number_1_2022-09-27_1'
'batch_number_1_2022-09-27_2'
now when i run the jmeter 2nd time again it should be like below(batch number should be incremented)
'batch_number_2_2022-09-27_1'
and so on like below for everytime when i trigger jmeter run
'batch_number_3_2022-09-27_1'
|
|
|
|
'batch_number_n_2022-09-27_1'
Take a look at Counter configuration element or __counter() function, the first one generates an incremented number on each iteration, the latter one each time it's being called.
So if you amend your payload to look something like:
batch_number_1_2022-09-27_${__counter(,)}
you will get the incremented postfix
More information: How to Use a Counter in a JMeter Test

JSR223 Preprocessor generating the same value when the thread runs in loop

Context: Want to generate unique and different value when the thread runs in loop.
Currently it is generating the same value.
Script Inside JSR223 Preprocessor,
String subscribeSchemaNamePreProcesser="agent-Perf-${__Random(1,99999,)}"
log.info("Schema Name --------------->"+subscribeSchemaNamePreProcesser)
vars.put("subscribeSchemaNamePreProcesser", subscribeSchemaNamePreProcesser)
When run with number of threads more than 1(i.e. 2 thread 1 loop), it is generating different value
When run with number of loops more than 1 (i.e. 1 thread 2 loop), it is generating the same value (want to fix it)
anything I missed
Don't inline JMeter Functions or Variables into Groovy scripts, they will be cached and only first occurrence will be used for subsequent calls.
You need to change this line:
String subscribeSchemaNamePreProcesser="agent-Perf-${__Random(1,99999,)}"
to this one:
String subscribeSchemaNamePreProcesser = "agent-Perf-" + org.apache.commons.lang3.RandomUtils.nextInt(1, 99999)
More information:
JSR223 Sampler
Apache Groovy - Why and How You Should Use It

JMeter and random variable along with if controller

i want to control my sampler execution by using a random variable . I have a sequence of hits login,welcome,Bla,log out . i want the log out to be performed for 6/10 requests and let others do not login(so to speak 6 requests will perform the whole sequence including log out, 4 of them will perform will not perform log out ).How to achieve the same in JMETER
I have added a random variable rand and set it between 1-10 at the beginning of the thread group .Then just above Logout sampler i placed an IF controller were i check ${rand}>4 . How ever i always get all sequence executed . Please suggest what am i doing wrong
Your approach is a little bit weird, my expectation is that the problem is in the following areas:
Your IF Controller condition is wrong (check jmeter.log file for any suspicious entries)
Your random variable setting is wrong, i.e. it has the same value for all virtual users (threads) so they will either be executed all or none
So I would recommend using Throughput Controller or Switch Controller in order to set up this 60/40 distribution.
See Running JMeter Samplers with Defined Percentage Probability article for more details.
Random Variable in Jmeter is saved in long format be default so
${rand} > 4 won't work. You need to change
Condition to ${rand} > 4.0
or change Random Variable Output format to 00 (2 digits)
see Manual
This was accomplished by creating a combination of config element- random variable and an IF controller
1) a random variable was created with Minim and maxim value to meet above condition
2) and IF controller was able to check ${myrand}>4;
This had derived the desired result - thank you all

Jmeter property get/put issue

I have the following flow,
Thread Group 1
-JSR223 Sampler - props.put("x",10);
Thread Group 2
Number of threads(users) = ${__P("x")}
I am setting a property value which is not defined in any property file as 10. I am trying to use that value in the next thread group. But instead of ten threads, I get only 1 thread to start.
Documentation says ${__P()} would give 1 if the property is undefined. How can I set the THread Group 2's thread count to x value?
You need to pass that value from command line, when you run the test appended by -Jx.
So when you run your test, Pass value of X from command line
-JPropertyName=ProperyValue
This will pick up value as 10.
Setting property should be
props.put("x","10");
i.e. it wants value in String format, and function should be put, not set.
Thread Groups are being initialised during JMeter startup so you won't be able to set the number of threads in the Thread Group this way. You have 2 options:
Set the property value in user.properties file (lives in the "bin" folder of your JMeter installation) like
x=10
Pass the property value -J or -D command-line arguments like
jmeter -Jx=10 -n -t ....
jmeter -Dx=10 -n -t ...
The default value returned by the __P() function will never be 1 due to a bug in JMeter (you can report it via JMeter Issue Tracker if you want), its default value will be an empty string. If you want the __P() function to return 1 as default value you should be using it like ${__P(x,1)}
See Apache JMeter Properties Customization Guide guide for more information on working with JMeter Properties, ways of setting and overriding them, etc.
Although you cannot change the number of threads on the fly, you can control how often the samplers are executed via Constant Throughput Timer. This way you can use __P() function to set the desired throughput rate (in requests per minute) and you can change the associated property value on the fly even outside of the JMeter script.

Resources