Loop controller inside While controller in JMeter - jmeter

I have a while controller that stops after running for 5 seconds.
This while controller works fine when inside it, there is one sampler or one HTTP request.
Now I want to have a loop controller inside this while controller. But now, the while controller doesn't stop after 5 seconds, and the script runs for the number specified in loop controller.
Is there any way my loop controller stop working when the while controller trigers in 5 seconds?
Here's the schematic of my test plan. I want that "search" request stops after 5 seconds (the condition inside while controller), no matter the specified number in loop controller.
PS. The code inside JSR223 Sampler1 calculates the maximum time:
max = ${__timeShift(,,PT5S,,)};
vars.putObject("max", max);
And this is the logic inside While Controller:
${__groovy( now = ${__time()}; max = vars.get("max") as long; now <= max,)}

Why would you need the Loop Controller if While Controller generates loops itself?
Don't inline JMeter Functions or Variables into JSR223 Test Elements or __groovy() function otherwise you might get unexpected behaviour.
If you want to limit While Controller's number of loops to some specific maximum value just include it into your condition clause.
In the JSR223 Sampler:
max = System.currentTimeMillis() + 5000L
In the While Controller:
${__groovy( now = System.currentTimeMillis(); max = vars.getObject("max"); now <= max && (vars.get('__jm__While Controller__idx') as int) < 10,)}
More information: Using the While Controller in JMeter

Related

How to create a counter in JMeter and save the value for the next execution?

i've been trying to save the value of a counter once the execution finishes, with the idea that the next one starts with that same value. For example: I start with a counter that has 1 as value, loop it 5 times and the execution finishes with that counter having his value in 5. Then, i want that counter to start with his value in 5, how is this doable?
You can save it into a file using a suitable JSR223 Test Element like:
new File('counter.txt').text = vars.get('your-counter-variable-name-here')
where vars stands for JMeterVariables class instance, see Top 8 JMeter Java Classes You Should Be Using with Groovy article for more information on this and other JMeter API shorthands
the same for initialization, you can use __groovy() function with the following code:
${__groovy(file = new File('counter.txt'); if (file.exists()) {return file.text} else { return '0'},)}

Increment time value for 2 fields for every Thread and loop in jmeter

I have to Schedule a Meeting, In the particular post request body data I have to give from and to time. Every time the From & To time hours be different and time should not overlap.
For this requirement I tried the below code using JSR223 Sampler, But the issue I'm facing here is that only one time gets incremented and for every thread and loop .The value is same and it is not incrementing. Every Thread the time value should be incremented. Please let me know how I achieve it , as below code is returning same value for each Thread
def now = new Date()
log.info('Before: ' + now.format('HH:mm'))
use(groovy.time.TimeCategory) {
def nowPlus60Mins = now + 60.minutes
def nowPlus15Mins = nowPlus60Mins + 15.minutes
log.info('After: ' + nowPlus60Mins.format('HH:mm'))
log.info('End: ' + nowPlus15Mins.format('HH:mm'))
vars.put("AfterTime",nowPlus60Mins.format('HH:mm'));
vars.put("EndTime",nowPlus15Mins.format('HH:mm'));
if you want to affect all thread you must use JMeter properties, represented in script as props:
props.put("AfterTime",nowPlus60Mins.format('HH:mm'));
props.put("EndTime",nowPlus15Mins.format('HH:mm'));
To get the property value outside JSR223 Sampler using __P function as ${__P(AfterTime,)}
In JSR223 get property with props.get("EndTime")
If you run more than 1 iteration in 1 minute - it's absolutely expected that you will get the same generated offsets because given your SimpleDateFormat setting the value will update every minute.
Also you don't need any scripting, you can achieve the same using __timeShift() function directly in your request body:
plus 60 minutes: ${__timeShift(HH:mm,,PT60M,,)}
plus 15 minutes: ${__timeShift(HH:mm,,PT15M,,)}
More information: Creating Dates in JMeter Using the TimeShift Function

JMETER : While condition is failing with syntax error

I have a scenario where in I need to wait for the response text .I need to send the same request till i get the required response text. I included my http samples in while loop with a counter. Now I am not able get the correct while condition.
Tried with below conditions.
${__javaScript(("${recordTypeLabel1}"!="asdf" && ${counter} < 5),)}
${__jexl3("${recordTypeLabel1}" != "asdf",)}
Both are failing. How to handle this?
Pleasae help.
Threadgroup
Once only controller Login
loop controller
HTTP req
HTTP req
While loop {
Counter
HTTP request
HTTP Request
JSON extractor
}
HTTP req
Once only Controller Logout
The correct syntax for the __jexl3() function would be:
${__jexl3("${recordTypeLabel1}" != "asdf" && ${__jm__While Controller__idx} < 5,)}
Don't use __javascript() function as it is some form of a performance anti-pattern, stick to __jexl3() or __groovy() functions if you need to script some extra logic
Also you don't need to introduce a Counter, since JMeter 5.0 you have a special pre-defined variable called ${__jm__While Controller__idx} which holds zero-based iteration number of the While Controller. (If you change the While Controller's label to something else - make sure to amend the variable accordingly)
Exit when loop number exceeds the threshold
Exit when variable value becomes expected:

issue in while controller jmeter

replicate steps:
1. -ThreadGroup
2. --loop controller (Count 10)
3. ---counter
4. ----while controller(`${__javaScript("${RegexValue}".indexOf("olum") == -1 && ${Counter}<5,)}`)
5. -----Counter
6. -----dummy sampler
7. ------regex `"c([A-z]+)nId` (extracting oulm value)
Question: in jmeter during run if while controller conditions are met it is stopping both the loops. is there a way in which i can only stop the inner while loop.
I don't see any Samplers which would be the children of the Loop Controller so when the While Controller ends the Loop Controller will end as well
You don't need these "Counters" as there are special variables holding the values of the current loop, they are:
${__jm__Loop Controller__idx} - for the Loop Controller
${__jm__While Controller__idx} - for the While Controller
You should be using either __jexl3() or __groovy() function in order to define your condition
Demo:
i was not intializing the while loop.

Jmeter- how to pass a fixed number of values from an array at a time until iterating through all values to a http request

In my get htttp request, I get an array of ids ex: list=[1, 2, 3.....1000]
And then for my next http request, I want to pass all the values in the list 10 at a time, so it will be 100 requests total and each time, it takes 10 values from the list array. I'm using a loop controller to call the http request 100 times. but I dont know how to retrieve 10 values at a time, and then go to the next 10, next 10 until all values are used.
How should I do that?
Add a regular expression extractor to your get request as a child.
Add the following properties shown in the screenshot to extract List
Add a Beanshell sampler/ JSR223 sampler and add the following code.
The code below creates a series of variables and store series of 10 values in a variable starting from Final_0 to Final_99
import java.util.Arrays
String complete_List=vars.get("List");
String[] complete_List_Array = complete_List.split(",");
int i;
for(i=1;i<=complete_List_Array.length;i++)
{vars.put("List_"+i,complete_List_Array[i-1]);}
int j;
int loopcount=complete_List_Array.length/10;
vars.put("loopcount",Integer.toString(loopcount));
for(j=0;j
{
StringBuilder sb = new StringBuilder();
sb.append("[");
for(i=j*10+1;i<=(j+1)*10;i++)
{
sb.append(vars.get("List_" + i));
if (i < (j+1)*10)
sb.append(",");
}
sb.append("]");
vars.put("Final_"+j,sb.toString());
}
vars.put("counter","0");
Add a loop counter and mention loop count as ${loopcount} as shown below
You can add HTTP Request as a child of loop counter and to pass series of 1o value at a time use ${__V(Final_${counter})}
Add a beanshell post processor to the http request to that will increment the counter
add the following code to the beanshell sampler
int counter = Integer.parseInt(vars.get("counter")) +1;
vars.put("counter",Integer.toString(counter));
You can see in the results its passing series of 10 values at a time and loop will run for 100 times
For more info on beanshell please follow the link
Please let me know if it helps.........
Considering you have the list array available at your disposal by using any extractor.
Extract the sublist from the array and put them in properties. Then, fetch the properties where ever you want it.
In the below I am fetching the sublist from the array and putting it in jmeter properties.
Here, I am fetching the values from the properties. This is just for demonstration and you dont need this. After putting list in properties just fetch in HTTP sampler as shown in the last image.
Now, to fetch it in HTTP sampler you can use loop and counter and fetch the properties using groovy. Loop for iteration and counter for increment the variable mylist_x.
Hope this helps.

Resources