In JMeter it is possible for me to specify the number of threads for a test in the command line. I just do:
jmeter -n -t mytest.jmx -l output.jtl -j jmeterlog.log -Jthreads=5
Is it possible to do something similar for number of iterations?
Sure, you just use _P in the loop count.
EXAMPLE
Define the property value:
jmeter -Jgroup1.threads=7 -Jhostname1=www.realhost.edu
Fetch the values:
${__P(group1.threads)} - return the value of group1.threads
${__P(group1.loops)} - return the value of group1.loops
${__P(hostname,www.dummy.org)} - return value of property hostname or www.dummy.org if not defined
Related
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 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
I have two different array with values as below:
Code = [8,9,10]
Value = [4,5,6]
I need to get the values from each array (above mentioned) randomly and assign it to different variable like below:
Code 1 = 9 , Code2=10
Value1 = 4 , Value2=6
Or is there any way in Jmeter to Pass that array to another sampler thereby assigning it to different variables.
How can we achieve it on Jmeter ? Any help / Suggestions is welcome!
Your values look utterly like JSON Arrays so my expectation is that you could handle it more easy using JSON Extractor
Just in case I'm wrong you can get random code and/or value using the following Groovy code in any of JSR223 Test Elements
import org.apache.commons.lang3.RandomUtils
def codes = vars.get('Code').findAll(/\d+/ )*.toInteger()
def values = vars.get('Value').findAll(/\d+/ )*.toInteger()
def randomCode = codes.get(RandomUtils.nextInt(0,codes.size()))
def randomValue = values.get(RandomUtils.nextInt(0,values.size()))
log.info('Random code: ' + randomCode)
log.info('Random value: ' + randomValue)
Demo:
You can use "Config Element" > "Random Variable" where you can give a range and ask for a random number within that given range.
Hope it helps.
In my testplan, I'm using Counter config element and have configured start = 1, increment = 1, maximum = 4 and the ReferenceName = loopCount.
And in a sampler, I have used Json Extractor as well where I want to set the
Match No: ${loopCount} + 1
since in the Json file I'm extracting always the first match is null.
Unfortunately I think I can't give it like
${loopCount} + 1.
Is there any work around for this.. please help.
You can add a BeanShell Sampler after the Counter config element with the below code in the code area:
int Counter = Integer.parseInt(vars.get("loopCount"));
vars.put("Increment", String.valueOf(Counter+1));
And now you can use the variable ${Increment} as the Match No.
OR
You can use ${__intSum(${loopCount},1)} which will add 1 to the value of loopCount variable directly.
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: