JMeter use of variables (CSV Data Set) - jmeter

I have a question on the visibility scope of variables. I have two thread groups, each with CSV file and controller containing http request steps, testing the application.
Thread Group1
CSV file1
Controller1
Test Steps1
Thread Group2
CSV file2
Controller2
Test Steps2
Both Controllers use the values in each Thread Group's CSV file just fine. However, I then want to make a Thread Group Combined containing both Controller1 and Controller2
Thread Group Combined
CSV file1
CSV file2
Controller1
Test Steps1
Controller2
Test Steps2
The problem is that the CSV files both use the same variable name, therefore I guess Controller1 and Controller2 don't know which CSV file to use. I still want Controller1 to use CSV file1 and Controller2 to use CSV2. Will a setup like this work?
Thread Group Combined
Controller1
CSV file1
Test Steps1
Controller2
CSV file2
Test Steps2
Thanks for any help!

If you use a 'Config Element' - It does not matter where you place it - they are like Global variables (Test Plan Scope). They get initialized before your test starts .
Lets assume i have a set up like this.
Thread Group 1
User Defined Variable (A=100)
Thread Group 2
User Defined Variable (A=150)
Now If i run this test - Under Thread Group 1 - If i try to print the value of A, it would be 150. This is because - Scope of these variables are not within Thread Groups - they are global variables.
For this setup,
Thread Group 1
Thread Group 2
User Defined Variable (A=150)
Now If i run this test - Under Thread Group 1 - If i try to print the value of A, it would still be 150 even if i do not have User Defined Variables under Thread Group 1.
However - variables you create with Beanshell etc have 'Thread scope'.
So, you might want to use unique variables accordingly.

Related

Parallel executions of dynamic URL's coming from for each controller

I have a requirement to load the dynamic assets(Images) parallelly which got extracted from the for each loop as there is only one sampler with variable passed. I cant use the parallel controller here, also I don't know the exact number of assets loaded to pass in parallel sampler.
As suggested by you in one of similar issues I have used that approach(add pre sampler) below script and try to execute it in, It is able to pick the values but the child element is always only one(Parallel requests are not happening) however I see the URL's passed main request has multiple passed.
Pre-Processor script used:
String var2 = vars.get("tokens");
String var3 = vars.get("Token");
vars.put("var3", "https://stagingassets.ovid.com"+"/"+var2+"/t/width-150-png?"+var3);
urlsList = vars.get("var3").tokenize(",")
for (int i = 0; i < urlsList.size(); i++) {
def row = new org.apache.jmeter.testelement.property.CollectionProperty()
row.addItem(urlsList[i])
sampler.data.addItem(row)
log.info("ROWS ${row}")
}
variables are coming from CSV where values are saved into a file from each loop using a Flexible File Writer.
please suggest , My requirement is to execute the images parallelly as it happens in the browser.
[JMeter script image][1]
Use Parallel Sampler, the URLs can be added dynamically via JSR223 PreProcessor using the following code snippet:
sampler.addURL('your-first-url')
sampler.addURL('your-second-url')
If you want to mimic browser-like behaviour you need to put the Parallel Sampler under the Loop Controller and set the number of loops to the number of your URLs / 6

JMeter update user defined variable at run time

I am trying to update the user defined variable set at a test plan level from a thread group. This is my scenario:
Test plan> user defined variable (variable name/value: fBurst=0)
Test plan> Threadgroup1 > Once only controller> JSR223 test plan (inside the test plan I have the following code)
log.info("fBurst user defined value : " + ${fBurst})
vars.put("fBurst", Integer.toString(111))
log.info("fBurst user defined value vars.put' : " + ${fBurst})
props.put("fBurst", 222)
log.info("fBurst user defined value props.put' : " + ${fBurst})
What I am trying to do here is to update the fBurst user defined value from inside the once only controller sampler and so far not been able to do it using the vars.put or props.put. Is there another way to do this?
Take a look at JSR223 Sampler documentation:
The JSR223 test elements have a feature (compilation) that can significantly increase performance. To benefit from this feature:
Use Script files instead of inlining them. This will make JMeter compile them if this feature is available on ScriptEngine and cache them.
Or Use Script Text and check Cache compiled script if available property.
When using this feature, ensure your script code does not use JMeter variables or JMeter function calls directly in script code as caching would only cache first replacement. Instead use script parameters.
So replace ${fBurst} with vars.get('fBurst") or props.get('fBurst') correspondingly and it should start working as expected
More information: Top 8 JMeter Java Classes You Should Be Using with Groovy

How to parametrize number of threads in JMeter 5.2.1

I have a JMeter 5.2.1 project where in the SetUp thread I generate properties with names like ThreadGroupName1-NumberOfThreads, ThreadGroupName2-NumberOfThreads, etc. with values representing integers.
Now, I wish to access these properties in thread groups named ThreadGroupName1, ThreadGroupName2, etc. to parametrize the number of threads. I tried something like ${__jexl3(props.get(threadName + "-NumberOfThreads"))} but it fails as threadName evaluates to standardjmeterengine.
Also, I tried to use ctx but ctx.getThread() and ctx.getThreadGroup() but they evaluate to null.
So far what 'works' for me is ${__jexl3(props.get("ThreadGroupName1-NumberOfThreads"))} but I want it be parametrized by the name of the thread group.
Is it possible to do this?
Is this threadName returning standardjmeterengine a bug?
Update: In fact, the easiest 'solution' that provides the parametrized number of threads there is ${__P(ThreadGroupName1-NumberOfThreads)} and what I want is to generate this key ThreadGroupName1-NumberOfThreads to be something like ${MyCurrentGroupName}-NumberOfThreads, effectively providing a way to have an abstract method like
int GetNumberOfThreads(string threadGroupName)
{
return properties.get(threadGroupName + "-NumberOfThreads";
}
Similarly, I wish to use this patter in Constant Throughput Timer as well with another prefix like -Rpm.
I don't think you can use any JMeter Function in the "Number of Threads" field of the Thread Group so this is not something you can do via UI. If you believe this is something everyone needs you can consider raising an enhancement request
As a workaround you can
Set the number of threads to 1
Add If Controller to the Thread Group and use the following __groovy() function as the condition:
${__groovy(ctx.getThreadNum() == 0 && vars.getIteration() == 1,)}
Add JSR223 Sampler as a child of the If Controller and put the following code into "Script" area:
SampleResult.setIgnore()
2.upto(props.get(ctx.getThreadGroup().getName() + '-NumberOfThreads') as int, { ctx.getThreadGroup().addNewThread(0, ctx.getEngine()) })
This way each thread Group will normally start with 1 thread, however this thread will read the property you defined earlier and add as many threads as needed.
You can use JMeterContext's getThreadNum to get thread number (increment, because it starts with 0)
${__jexl3(props.get("ThreadGroupName"))}${__jexl3((ctx.getThreadNum()+1) + "-NumberOfThreads" )}

How to read JMeter Test Cmd Line Parameters from JSR223/Beanshell

I have wrote a JMeter test and I want to run it in Command Line with some parameters, let's say ThreadNumber.
How do I read it in JSR223/BeanShell?
Send property in command line using -J which adds new property
-JthreadNum=100
Inside Thread Group use the value using __P function in Number of Users(threads) field
${__P(threadNum)}
simplified property function which is intended for use with properties defined on the command line.
Use props to get properry in JSR223/BeanShell
props.get("threadNum")
Note you may set ramp up same as thread number property
Start with Ramp-up = number of threads and adjust up or down as needed.

Apache Jmeter read SQL variables from csv file

Need to load test a Oracle database the requirement is to fire sql queries concurrently to the database reading the varaibles from a CSV file is this feasible ?
Have a CSV file with values like
Name Email
Justin justin#beiber.com
George george#washington.com
...
Micheal micheal#jackson.com
And then have 10,20,30 users fire of queries like
select name,phone,city
from address
where name = <<feild1-from-csv>>
and email = <<feild2-from-csv>>
...
I'd suggest splitting your test logic into 2 separate pieces:
Load information from CSV and store it as JMeter variables
Execute SQL code against Oracle using variables from point 1
In regards to implementation I'd suggest to use 2 separate Thread Groups, the first one will be loading stuff from CSV, the second one will be doing actual testing.
Important: don't forget to check "Run Thread Groups Consecutively" box at TestPlan level to assure that second thread group runs after first one.
Example configuration of 1st thread group:
Counter
Start - 1
Increment - 1
Reference name - counter
CSV Data Set Config
Filename - path to your csv file
Variable names - name, email
Delimiter - if you're using TAB - "\t", if comma - "," without quotes
Allow quoted data - False
Recycle on EOF - False
Stop thread on EOF - True
Sharing mode - All Threads
Beanshell Sampler (this one is optional, JMeter 2.10 is smart enough to store variables for you but I prefer to control everything myself)
Code for Beanshell sampler shoud look as follows:
props.put("name" + vars.get("counter"), vars.get("name"));
props.put("email" + vars.get("counter"), vars.get("email"));
It fetches current "name" variable and stores it as name + counter property.
So given 3 lines is CSV file you'll have following properties:
name1=Justin
email1=justin#beiber.com
name2=George
email2=george#washington.com
name3=Micheal
email3=micheal#jackson.com
You can use Debug Sampler to see JMeter Variables and Properties values
After that in second thread group you can refer stored properties as:
${__P(name1,)}
or
${__property(name1,,)}
in your JDBC Request Sampler.
Both should work.

Resources