Printing loop controller iteration number in beanshell scripting - jmeter

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);

Related

Use same random variable in all threads

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:

How to call Jmeter Counter Reference Name variable incrementing one

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.

"for var = 1, 2" in Lua

What does this kind of loop do in Lua?
for count = 1, 2 do
-- do stuff
end
The variable count isn't used in the body of the loop.
It executes the body of the loop twice.
There's no need to refer to count inside the loop unless you need to know its current value.
for count = 1,5 do
print("Hello")
end
prints
Hello
Hello
Hello
Hello
Hello
In this case count is "dummy variable" - "dummy" in that a variable is used fulfill a certain construct even though the variable is not used. (Another common name for such a usage is _, although count arguably adds a little more semantic intent.)
Such a dummy variable is used because LUA loops require a variable / assignment in the grammar construct. However, there is no requirement that the variable is used - hence a "dummy".
.. A numeric for [loop] has the following syntax:
for var=exp1,exp2,exp3 do
something
end
That loop will execute something for each value of var from exp1 to exp2, using exp3 as the step to increment var. This third expression [exp3] is optional; when absent, Lua assumes one [1] as the step value.

how to use variable in jmeter like counter

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:

Reset Counter Jmeter in loop

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

Resources