Jmeter property get/put issue - jmeter

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.

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

In Jmeter vars vs props for while controller

Before test, I added x variables to TestPlans,
After I run test as 10 thread 10 seconds ramp up
In while controller for condition that
${__javaScript("${x}" != "",)}
In beanshell:
vars.put("x","");
Normally, each thread occured 1 second, First thread changed x variable as "", therefore another threads don't meet if condition. why do threads get looped though ?
Note: Other threads did not start testing when using property instead of variable.
As per JMeter documentation:
Properties are not the same as variables. Variables are local to a thread; properties are common to all threads
so when you do vars.put("x",""); you change x variable value only for the current thread, other threads have their own values of the x variable.
If you want to amend the value so it would be visible for all threads - you will have to:
Change vars.put("x",""); to props.put("x","");
In the While Controller ${__javaScript("${__P(x,)}" != "",)}
Also: since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting so it worth considering migrating (the same applies to __javaScript() function in the While Controller), more information: Beanshell vs. JSR223 vs. Java For JMeter: Complete Showdown

Incremental variable in each iteration/loop in Jmeter Tests

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

How to save 10 different id from Jmeter response and use it in next 10 requests

I am currently running 10 different threads like this:
each of the thread response will provide a different id in the response and i want to save them and use it as a request in the next test case(10 threads) so there will be 10 ids and 10 threads and each thread will have a unique id as a request. This is what I am doing:
Here is the first request
This is how I am extracting the values
This is how i am using the final request however i am not able to get the desired results
UPDATE:
I tried Dmitri Answer but still no luck i am extracting the id by using this
I used __threadNum() function as the prefix or postfix for the property name like:
${__setProperty(loginassistant_${__threadNum},${loginassistant_},)}
and read it similarly:
${__P(loginassistant_${__threadNum},)}
but it is not working and it is setting the value as a static string(see screenshot below):
This is how and where i am defining my loginassistant using the simple controller:
You're using properties which are global, if you have more than one thread the next thread will overwrite the property defined by the previous thread so it could be mismatch.
If you don't need to pass values across thread groups - go for JMeter Variables instead, variables are local to the thread (virtual user) and the properties are global for the whole JMeter/JVM instance
If you do need to pass values across thread groups - either use __threadNum() function as the prefix or postfix for the property name like:
${__setProperty(loginassistant_${__threadNum},${loginassistant},)}
and read it similarly:
${__P(loginassistant_${__threadNum},)}
or go for Inter-Thread Communication Plugin

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

Resources