==================================================
1.While Controller之前有一个BeanShell Sampler,用于Init Note Parameters
vars.put("INCREMENT", "0");
vars.putObject("ANYSAMPLEFAIL", false);
vars.putObject("ISCONTINUE", true);
vars.putObject("MAXLOOP", 5);
==================================================
2.While Controller内则是这样的
==================================================
2.1首先是需要在失败之后重复执行的一些步骤,每个HTTP request名称后面都可以加上一个“_${INCREMENT}”,这样就可以标识出循环到了第几次;
==================================================
2.2接着是一个BeanShell Assertion,用于判断取样器的结果(Judge Sample Result)
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.assertions.AssertionResult;
SampleResult prev_result=ctx.getPreviousResult();
var increment = vars.get("INCREMENT");
Integer maxloop = (Integer)vars.getObject("MAXLOOP");
if (prev_result != null && !prev_result.isSuccessful())
{
vars.putObject("ANYSAMPLEFAIL", true);
if (Integer.parseInt(increment)< (maxloop.intValue() - 1))
{
prev_result.setSuccessful(true);
}
}
==================================================
2.3最后是一个BeanShell Sampler用于控制循环次数(Control Loop Count)
import org.apache.jmeter.util.JMeterUtils;
var increment = vars.get("INCREMENT");
Integer tempIncrement=Integer.parseInt(increment);
tempIncrement++;
Boolean requestResult = (Boolean)vars.getObject("ANYSAMPLEFAIL");
Integer maxloop = (Integer)vars.getObject("MAXLOOP");
boolean iscontinue = (tempIncrement < maxloop) && requestResult.booleanValue() ;
vars.putObject("ISCONTINUE", iscontinue);
vars.putObject("ANYSAMPLEFAIL", false);
vars.put("INCREMENT",tempIncrement.toString());
==================================================
备注:
只有以vars.put("INCREMENT", "0")的方式定义变量,才可以在HTTP request中通过${INCREMENT}直接进行引用,以vars.putObject("INCREMENT", "0")的方式定义变量就不可以。
而且,通过vars.get("INCREMENT")获取变量的值时,必须这样:var increment = vars.get("INCREMENT"),即左边的变量必须定义为var,否则就总是报错。