JMeter - calculate variable using another variable - jmeter

I have 5 thread groups and I want the number of threads in each group to be derived from a total users number.
So I wouldn't need to change the numbers in each thread group every time I change the number of users.
So I defined 6 variables on my test plan:
1. TOTAL_NUMBER_OF_USERS = ${__P(num_of_users,100)}
2. WEIGHT_OF_GROUP_1 = ${__P(weight1,0.2)}
3. ...
4. ...
5. ...
6. WEIGHT_OF_GROUP_6 = ${__P(weight6,0.2)}
and than in the thread group Number of threads(users) field I put - ${WEIGHT_OF_GROUP_1}*${TOTAL_NUMBER_OF_USERS}.
When I start the test plan I can see in the log that the number of threads was interpreted as 0.
I tried using evalVar or doing the calculation at the place where I define the variables, but it didn't work.
Any idea how to do this?

Use __BeanShell function if you need computations:

Related

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 use different variables for different threads depending on a percentage?

I have one thread group of which I run 100 threads. Each thread does almost the same. Difference: x percent of the threads shall use variable A, the rest variable B as value for a Request.
I don't want to use a switch or throughput because all steps are the same and I don't want to have redundant code.
Thank you in advance.
You can use ${__threadNum} function in Jmeter to get the Thread number and based on that you can write Beanshell code like below to assign the variable.
Assuming that percentage is 70
if 100 threads are started then 70 threads will have variable as A and 30 with have variable as B
if(${__threadNum}<=70){
vars.put("result","A");
}
else{
vars.put("result","B");
}

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

Alternatives to using CSV in JMeter (for generating usernames)

I have a JMeter Test Plan with following structure
Test Plan
**ThreadGroup1**
--CSV Data Config-001
----SimpleController
--------------LoginRequest
--------------Action-abc-Request
**ThreadGroup2**
--CSV Data Config-002
----SimpleController
--------------LoginRequest
--------------Action-xyz-Request
I have two CSV files which contain list of users like this..
**CSV-001**
Username1
Username2
.. ..
Username50
**CSV-002**
Username51
Username52
.. ..
Username100
In my scenario, I need to run a load test with say 100 users. 50 users login from ThreadGroup1 and other 50 users login from ThreadGroup2. Users from both threadgroups login simultaneously.
Currently, I have to go through process of manually creating/editing these CSV files whenever I change the number of total users.
Please suggest if there are any alternative time-saving & performance-efficient approaches through I which can fulfill my scenario requirements (without using CSV files).
I will appreciate, if you can explain the alternative solution with some details as I am quite new to JMeter stuff. Thanks.
Another idea is to use
Username${__threadNum}
for the first thread group and
Username${__BeanShell(ctx.getThreadNum()+Z+1)}
for the second, where Z equals the total number of threads in thread group 1. You also need to add 1 since ctx.getThreadNum() returns a thread number using a 0 based index, whereas the __threadNum function is 1 based.
You can use a counter in each thread. The start value for the counter in the first thread would be 1, in the second 51. Be sure the 'Track counter independently for each user' check box is unchecked.
If you set the reference names to thread1Count and thread2Count respectively, you can use
Username${thread1Count}
for the first thread and
Username${thread2Count}
for the second.

How to evenly increase number of users in 3 Thread Groups saving proportion of requests between them?

I have 3 Thread Groups in my test plan.
These Thread Groups must be with fixed request weights (for example 1st-50%, 2nd-40%, 3rd-10%).
If I need to emulate constant number of users, the solution in this post which was given by #PMD UBIK-INGENIERIE works good if I set Loop Count as number (1, 10, 100 ect.). But the problem is when I set the check-box "forever" for Loop Counts, the proportion between groups is broken.
Now I need to increase the amount of users, saving proportion of requests between my 3 groups, to perform the capasity assessment.
All 3 groups must send requests evenly, in this constant proportion 50-40-10.
If I use Stepping Thread Group, I can evenly increase number of users in each of them, but I can't set the proportion of request for them.
Could you please tell me any idea, how to do this?
UPD1.
My screenshots with settings:
and
What is wrong?
UPD2. The last variant (I have put numbers manually for each group) is (Thread Group1 - 50%):
The idea is the same as in PMD UBIK-INGENIERI's answer.
Set USERS variable in User Defined Variables or test plan variables (or you can pass it through command line).
Also set variables:
PERCENT1 to 0.5
PERCENT2 to 0.4
PERCENT3 to 0.1
Add 3 stepping thread groups with:
"This group will start" set to
${__BeanShell(Math.round(${PERCENT1}*${USERS})}
${__BeanShell(Math.round(${PERCENT2}*${USERS})}
${__BeanShell(Math.round(${PERCENT3}*${USERS})}
"Then start" set to:
${__BeanShell(Math.round(${PERCENT1}*${USERS}/10)}
${__BeanShell(Math.round(${PERCENT2}*${USERS}/10)}
${__BeanShell(Math.round(${PERCENT3}*${USERS}/10)}
It will work if value of USERS variable can be divided by 100

Resources