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.
Related
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 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.
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.
I have a scenario.
ThreadGroup (loop value set to 1),
loop controller (value set to 5),
HTTP request,
Beanshell preprocessor
In Beanshell preprocessor i need to print loop controller iteration value like 1,2,3,4,5.
I use log.info(vars.getIteration()); but it prints (Threadgroup loop iteration number)
Will anybody guide me how to do this.
Depending on what you're trying to achieve:
Either use Counter configuration element and print the "Reference Name" variable via vars.get("foo");
or put __counter() function somewhere, i.e. in "Parameters" input like ${__counter(,)} and refer it as log.info("Current iteration: " + Parameters);
or define a variable and increment it as required like:
int iteration = vars.getObject("iteration"));
iteration++;
vars.putObject("iteration", iteration);