Vishalendu
Vishalendu

Reputation: 83

How to handle delay in jmeter based on response code

My requirement was that whenever there was an error in the Jmeter execution, there should be a delay before starting the next iteration.

"Start Next Thread Loop" is selected at the Thread Group level for Sampler error. Due to this Jmeter will start a new Iteration if there is any error during the execution.

For this purpose I have used Beanshell Timer at the start of the Iteration. Following code is added for delay if the response code is anything other than "200"

String code = prev.getResponseCode();
if(code!="200"){
log.info("::::::::::::::::::::::::::::Response Code = "+code);
log.info("sleep for 10sec");
 return 10000;
}
else
 return 0;

Please let me know if there are any better ways of doing this.

I believe the prev.getResponseCode() can also be used to do any kind of cleanup task, in case there is any error.

Like for example, if a user logs into the application and got an error before doing logout. We can test at the start of the iteration if the previous response code is in error, if so, we can make the user logout of the application.

Upvotes: 1

Views: 1976

Answers (1)

UBIK LOAD PACK
UBIK LOAD PACK

Reputation: 34566

You can do this using:

  • If Controller that will check if last response was in error or not using

${__jexl2("${JMeterThread.last_sample_ok}" == "false",)}

enter image description here

  • Test Action that will be used to Start Next Thread Loop

enter image description here

Test plan would have the following layout:

enter image description here

Upvotes: 1

Related Questions