If controller stop loop instead of creating new iteration - Jmeter Groovy - loops

I have an issue that I am trying to solve,
I have a loop controller and inside it I created if controller.
I want something to be done if the "if controller" is true.
the condition is that:
def loop_counter2 = ${__counter(TRUE)};
and the if controller condition is "${loop_counter2}" == "1".
the loop is set to run 3 times, so the expected results is that the condition will be correct only once.
I have 2 problems
1. the condition never set true for some reason
2. the loop is performed only once and not as set to 3 (I print the loop counter to Jmeter consul and only 1 displayed).
Can someone please advise why this is happening?
if I remove the If controller the loop performed 3 times
Check Arr - the loop controller that I want to run 3 times regardless what in the if controller.
test2 - put in variable (loop_counter2) the number of the loop iteration
if controller - the if controller that should be true only once, when we in iteration 1
JSR223 SAMPLER - Print to consul if entered to if controller
P.S all the purpose of my test is to run 3 times and if condition is set to connect DB.
in Java for example I can use for loop and put if condition inside.
for (i = 0;i<3;i++)
{
if (i = 1)
print happy;
else nothing;
}
how can I perform it in Jmeter?
how can I perform this in Jmeter?

Here is how to do it:
Use a Counter element configured this way:
And If Controller will be (I use __jexl3 function and check "Interpret Condition as Variable expression" for performances reason):

Related

Sending muliple occurrences of id to a request in jmeter

From a soap XML response I need to get all the occurrences of an id (This part already done using Match No being set to -1 in Regex extractor).
And then I need to send these ids to a subsequent request. That is, the request needs to be fired as many times as of the total number of occurrences of the id, we can call it as ID_matchNr. (This is the total occurrences I got from the Regex extractor).
I used a while loop and a counter. The while loop works until the ID_matchNr, but it doesn't stop there, that while loop is infinite. How to fix this?
My counter's reference name is count and the field name in my request is ${__V(ID_${count})}. And while loop's condition is ${count}<=${ID_matchNr}. I also tried using a beanshell to save the ID_matchNr to another variable and then use it in while condition instead of directly using ID_matchNr. Still running infinitely.
Have you considered using ForEach Controller?
Given you have JMeter Variables in form of:
ID_1=foo
ID_2=bar
ID_3=baz
ID_matchNr=3
Add ForEach Controller to your Test Plan and configure it like:
And put the "subsequent" sampler as a child of the ForEach Controller and refer the generated subsequent ID value as ${CURRENT_ID} where required
More information on the concept with the real-life example: Using Regular Expressions in JMeter

Start next Loop Controller iteration

I would like to configure something similar to Action to be taken after a Sampler error in Thread Group but at Loop Controller level.
For example, if one Sampler fails, instead of Start Next Thread Loop, I would like to Start Next Loop Controller Loop, I assume this would be achievable using groovy + JMeter properties, but I cannot find the right way to do it.
Try Until Controller from this guy -- http://www.testautomationguru.com/jmeter-until-controller/.
This should help.
You can do this By using While Loop instead of Loop Controller Use While Controller
Set Action to be taken on sampler error to continue on thread group.
Initialize a counter and set its value to 1 using a beanshell sampler , Keep this sampler before while loop begins
Add the following code
vars.put("counter","1");
Add A while Loop and keep the set of samplers you want to run in loop.
Add the following condition to while loop
${__javaScript(parseInt(${counter})<=15,)}
here 15 represents the number of loops (Loop Count)
Add A beanshell post processor to your first sampler in while loop
and add the following code (To increment the counter value)
int counter = Integer.parseInt(vars.get("counter")) +1;
vars.put("counter",Integer.toString(counter));
Next samplers in While loop embed with an if condition
${JMeterThread.last_sample_ok}
As shown below You have to do this for each sampler as we may not know where the error occurs.
In My case i wanted to run the loop 15 times irrespective of errors , You can see its executed 15 times even after getting errors
For more information on While controller Follow this link..
Please let me know if it helps ..
The easiest way is using If Controller.
Add If Controller after sampler on failure of which you want to restart the loop
Put all requests which are "below" the If Controller to be children of the If Controller
Use ${JMeterThread.last_sample_ok} as the If Controller condition
That's it, now If Controller's children will be executed only if previous sampler is successful, otherwise it will be next Loop Controller iteration.
As you can see from the above demo Some Other Sampler was executed only when Some Sampler was successful.

While loop stops thread before EOF

I have 1 million records (only 1 column without header) in a CSV/txt file which I am iterating through CSV Data Set Config and stores it a variable. I also have added while controller to check for end of the file to exit the loop and it's also retrying for 5 times before failing any transaction. So I have added the counter of 5.
The problem is, this logic is working but only for 50305 entries in CSV/txt file, it is not continuing till EOF, not sure what is wrong but every time it is exciting in the same way and it goes till 5035.
Could you please help me to loop until end of the file ? Is it happening because I have used groovy for reset the counter?
Added the screenshot.
I would suggest slightly amend your scenario and have 2 While Controllers, example outline:
While Controller, condition: ${__javaScript("${myVar}" != "<EOF>",)}
CSV Data Set Config, Variable Names - myVar Recycle on EOF - false, Stop Thread on EOF - false
If Controller, condition: "${myVar}" != "<EOF>"
Your Sampler
If Controller, condition ${JMeterThread.last_sample_ok}"=="false"
While Controller, condition ${__javaScript(${counter} < 4 && "${JMeterThread.last_sample_ok}"=="false",)}
Counter: Start 1, Increment 1, Reference Name: counter
Your Sampler
Any other samplers which need to be executed after main 1 million "while" loop
References:
Pre-defined Variables
Using the While Controller in JMeter

JMeter - loop controller with variable loop count

I am using JMeter and I want to define one loop controller. In this loop I want to define the loop count with a Jmeter variable. Is this posible?
Thanks a lot
I know is very late to help you, but it can help the others.
One good way to do this is to define a counter inside the loop controller.
Example, if you want a loop with 20 iterations, you put 20 in the loop controller and in the counter you put from 0 - 19. In the counter you can also define an increment. The output variable is named in the Reference Name field.
Yes, it's possible.
Define your user variable using the User Defined Variables component, or use the CSV component.
Then, in the Loop Controller, define loop count to: ${nameOfVar}
Since version 4.0 of JMeter, there is an easier way:
https://jmeter.apache.org/usermanual/component_reference.html#Loop_Controller
JMeter will expose the looping index as a variable named
__jm__Name of your element__idx
So for example, if your Loop Controller is named LC, then you can access the looping index through ${__jm__LC__idx}. Index starts at 0
If you're looking to learn jmeter correctly, this book will help you.
You can use the following inside the "Loop Count" field:
${myVar}
e.g. for a random count:
${__groovy(java.util.concurrent.ThreadLocalRandom.current().nextLong(128L),)}
You can access the current loop index (starts at 0) via ${__jm__Loop Controller__idx} where Loop Controller is the name of your loop.

Jmeter how to loop through a list of different properties in a single thread group or controller?

How to do a basic loop through different properties for a fixed set of controllers? Loop controller runs a set group a certain number of times, does not use properties though.
I can do modules, and set the values to properties for multi thread group usage, but how to pass the next iteration of the property, and run the loop again?
property x
do module (points to controllers)
next property
Say I have a list of 44 characters, and I want to loop through those characters in a ${name} while I'm doing a test. I'd very much not like to build 44 sets of controllers for one character change.
Please Note I cannot add extra files to my computer. It has to work via the stock available controllers. I'm using Jmeter 2.4 r961953
Thanks
I will elaborate slightly more about the BeanShell method. My assumption is that you'd like to do it within one User Thread, if so my proposal would be:
Create a Loop Controller.
Logic Controller->Loop Controller
Inside Loop Controller add following entries:
Config Element -> Counter
Preprocessors -> BeanShell preprocessor
Sampler -> yourSampler
The Counter element will be used as an index that will be used to choose valid value from our array, hence we need to specify a Reference Name for the Counter - let's say that it will be loopCounter.
Now we have to switch to BeanShell preprocessor and define the array of values. A great thing is that we have vars variable available and it gives us CRUD access to variables used in the scenario:
String[] varArray = {"Value1", "Value2"};
idx = Integer.parseInt(vars.get("loopCounter"))-1;
vars.put("myVariable", varArray[idx]);
And for the final step, inside mySampler we can use a variable in a regular JMeter way : ${myVariable}
JMeter API can be very helpfull if you want a more sophisticated solution.
There are a handful of different ways to loop through different values without adding external files:
Use beanshell controller, and write javascript to set your variable
Use a counter to increment by one
Use User Parameters
You can set it up so each loop gets a different value.
Check out the various configuration controllers to find one that works best for you.
EDIT:
I meant user parameters, not user define variables.
User Parameter
You'd need one row per variable with 44 columns. Sorry for the confusion.
User Parameter Structure
test plan
- Thread Group looped 44 times
-- User parameter
-- Request
Beanshell Method
Alternately, you could do an array in javascript in connection with a counter. The Beanshell samplers have access to Jmeter variables and properties, allowing the beanshell sampler to read the counter value. This may be a faster, cleaner way then using User Parameters.
Beanshell structure
test plan
- Thread Group looped 44 times
-- Counter
-- Request
---- Beanshell pre-processor
Beanshell Pseudo code would be
def counter = value of Jmeter Counter
def array = array of values
declare the variable "sampler_value" to be used by sampler
def sampler_value = array # counter
Counter with CharAt function
If you only need to generate characters, you could use the javascript function to utilize the function charAt, using the value from the Counter. Basic structure would be:
test plan
- Thread Group looped 44 times
-- Counter
-- Request
with the request using something like ${__javaScript(charAt(${counter})) as the parameter value. You may have to use JEXL instead of javaScript or evalVar/V/eval inside the charAt function.

Resources