Is there a way to reset the counter after i edit the maximum counter with a variable ? For example see the image http://i.stack.imgur.com/21067.png
Beanshell Sampler vars.put("Loop","5");
Loop 1 Loopcount 5
Loop 2 Loopcount ${Loop}
Counter with ${Loop} as maximum
Beanshell Sampler with log.info(vars.get("Counter").toString());
Beanshell Sampler with vars.put("Loop","2");
When i try this the ${Loop} in the counter module won't edit.
Im adding a user defined variable Counter with 0. In the loop i add this piece of code:
long number = Long.parseLong(vars.get("Counter"));
number = number + 1;
vars.put("Counter", String.valueOf(number));
Add the end of the loop i reset the counter with
vars.put("Counter","0");
This works for me
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)
}
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:
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);