I have a scenario as below,
Post a webpage which have a search bar, where i have to search for 5 different strings.
Hence i created thread group as below,
No of threads : 3
No of iterations : 2
Inside the script i included a while loop to search for 5 strings using a CSV config(added 5 string in a csv file).
CSV Config:
Recycle on EOF : False
Stop thread on EOF : True
Now the jmx is making only 1 iterations which makes actual output as below,
Actual output:
URL Launch : 3 users x 1 iteration = 3 hits
Search hits : 3 users x 1 iteration x 5 strings = 15 hits
EXPECTED OUTPUT:
URL Launch : 3x2 = 6 hits
Search hits : 3x2x5 = 30 hits
You won't be able to achieve this using CSV Data Set Config and While Controller, consider amending your test plan as follows:
Change While Controller to Loop Controller and use the following __groovy() function in the "Loop Count" section:
${__groovy(new File('test.csv').readLines().size() ,)}
Instead of referencing the variable from CSV use __StringFromFile() function like:
${__StringFromFile(test.csv,,,)}
Just replace test.csv with the full or relative path to your own CSV file and you should be good to go:
More information on using JMeter Functions: Apache JMeter Functions - An Introduction
Related
I have a few values in CSV files. I need to pass this values in JSR223 sampler. if suppose, in CSV files it has 4 variables are present in first row then i need to write two line below.
Example 1 in JSR223 sampler :
('usr', '${Var1}', '${Var2}')
('usr', '${Var3}', '${Var4}')
If 6 variables are present in second row of CSV files, they my script should have three lines.
Example 2 in JSR223 sampler :
('usr', '${Var1}', '${Var2}')
('usr', '${Var3}', '${Var4}')
('usr', '${Var5}', '${Var6}')
My scenario here is, each row will have different count of values would be present. So, how can i create my JSR223 sampler request count based on the values counts that is present in CSV files. May i know how to create this scenario in JMeter.
Something like:
def lines = new File('test.csv').readLines()
lines.each { line ->
log.info('Line from CSV: ' + line)
def request = new StringBuilder()
def values = line.split(',')
def counter = 0
1.upto(values.size() / 2, { it ->
request.append("('usr',").append(values[counter]).append(", '").append(values[counter + 1]).append(" ')")
if (it != values.size() / 2) {
request.append(System.getProperty('line.separator'))
}
counter = counter + 2
})
log.info('Generated request: ' + System.getProperty('line.separator') + request.toString())
}
should do the trick for you.
Demo:
More information:
Reading a File in Groovy
The Groovy Templates Cheat Sheet for JMeter
I've got test plan :
Thread groups ( users 3, loop 2)
Random Variable
HTTP Request
I want variable to be changed only per loop, so under each iteration all three threads should send same value.
So I want something like this :
request where random var = X
request where random var = X
request where random var = X
request where random var = Y
request where random var = Y
request where random var = Y
I tried a lot of workounds but can't find proper solution.
P.S. I don't want to read variables from file. I need to generate them
No matter whatever you "want" the best option would be pre-generating random values somewhere in setUp Thread Group and writing it to the file and then using CSV Data Set Config in the "main" Thread Group to read the values.
However if this is still not something you "want" here is yet another "workaround", hopefully it's "proper" enough for you:
Add JSR223 PreProcessor as a child of the request which you "want" to parameterize with the random variable
Put the following code into "Script" area:
if (props.get('foo_' + vars.getIteration()) != null {
props.put('foo_' + vars.getIteration(), org.apache.commons.lang3.RandomUtils.nextInt(0, 100))
}
Refer the "generated" random value using the following __groovy() function where required:
${__groovy(props.get('foo_' + vars.getIteration()),)}
Demo:
I would like to change the number of threads values in JMeter according to the Loop Count value.
Number of Threads values are 1 when Loop Count values are 1,
Number of Threads values when Loop Count value is 2,
When the Loop Count value is n, I would like to change the Number of Threads value to n.
How do we do that?
Now JMeter is using 5.3.
You can use same (or different) property for threads and loop count, e.g.
On Thread Group settings put in number of threads and loop count get property method
${__P(count)}
And send the property in command line:
jmeter -n -Jcount=4 -t your.jmx
For count 4 flow will execute 16 times (4 threads * 4 loops)
The nature of your request is not very clear, given you know the number of loops you can use the same JMeter Variable or JMeter Property as the number of Threads
Whatever. You can change the number of threads for every Thread Group in your Test Plan to be equal to the number of loops of that Thread Group by:
Adding setUp Thread Group to your Test Plan
Adding JSR223 Sampler to the setUp Thread Group
Putting the following code into "Script" area
SampleResult.setIgnore()
def engine = engine = ctx.getEngine()
def testPlan = engine.test
def threadGroupSearch = new org.apache.jorphan.collections.SearchByClass<>(org.apache.jmeter.threads.ThreadGroup.class)
testPlan.traverse(threadGroupSearch)
threadGroupSearch.getSearchResults().each { threadGroup ->
def loops = threadGroup.getSamplerController().getPropertyAsInt('LoopController.loops')
threadGroup.setNumThreads(loops)
}
I'm using loop controller inside the jmeter script, and I'm not able to fetch previous variable value in pre-processor beanshell.
Example:
var temp = 1; log.info("before : "+temp.toString()); temp++; prev.put("t",temp.toString());
Thanks in advance
To save values b/w iterations:
Following is one of the ways to store and retrieve the values b/w iterations:
log.info("temp prev value " + vars.get("temp")); // first iteration returns null
vars.put("temp","something"); // store or override the value, so it will be available in next iterations.
To know iteration number:
If your need is to know the iteration number, then use Counter:
In beanshell preprocessor, access using reference name (counter) as shown below:
I am working on Jmeter load test. I want to maintain the incremented counter value for the next parallel requests [each request defined under throughput controller], however, the counter value resets to 0 again for the subsequent requests.
Here is how my test plan looks like -
+Thread group
+counter (starts from 0 and increments by 1 ; ref name : index)
+Throughput controller [total execution : 1]
http request1 - uses index with value 0 in the request which is fine.
+Throughput controller [total execution : 2]
http request2 - should use index with value 1 and 2 , however the counter again resets to 0 and uses value 0 and 1 for 2 executions.
+Throughput controller [ total execution : 3]
http request3 - should use index with value 3 , 4, 5 ; instead uses value 0,1,2 as counter resets for this as well.
How do i maintain the counter value so that i can run these requests with the desired index values.
Thanks for your help here.
There is no direct functionality in JMeter Counter to handle your problem. Either you can use Beanshell sampler OR you can handle your scenario through a workaround with Counter as explained below:
Control the number of Throughput controllers execution via variables.
For example create 3 variables:
Controller1_ExecutionTimes: 1
Controller2_ExecutionTimes: 2
Controller3_ExecutionTimes: 3
Now you can use these variables to set starting point of your counters.
Set start point of counter1 as 0
Set start point of counter2 as ${Controller1_ExecutionTimes}
Set start point of counter3 as ${Controller1_ExecutionTimes} + ${Controller2_ExecutionTimes}
and so on.
Thanks Arif,
I used the beanshell sampler, extracted the counter value and applied logic to solve my problem.