JMeter While循环多条件判断异常:陷入无限循环问题排查
JMeter循环条件修正方案
问题分析
你的需求是:循环过程中只要4个状态变量compilerstatus/compilerstatus2/compilerstatus3/compilerstatus4任一等于Compilation Successful.就退出循环;若所有状态都未匹配成功,则最多循环9次后退出。
当前使用的表达式逻辑错误,直接导致无限循环:
${jexl3("${compilerstatus}" != "Compilation Successful." && "${compilerstatus2}" != "Compilation Successful." && "${compilerstatus3}" != "Compilation Successful." && "${compilerstatus4}" != "Compilation Successful.")} || ${jexl3("${counter_value}" <= "9")}
问题出在JMeter循环控制器的条件是满足时继续循环,而你的表达式逻辑是「所有状态都未成功」或者「循环次数≤9」就继续循环:
- 当次数超过9后,只要所有状态都未成功,第一个判断为true,循环会持续执行
- 当次数≤9时,不管状态是否匹配成功,第二个判断为true,循环都会继续
正确表达式
把逻辑调整为只有当所有状态都未成功,且循环次数还没到9次时,才继续循环,对应的JEXL3表达式:
${jexl3("${compilerstatus}" != "Compilation Successful." && "${compilerstatus2}" != "Compilation Successful." && "${compilerstatus3}" != "Compilation Successful." && "${compilerstatus4}" != "Compilation Successful." && ${counter_value} < 9)}
更简洁的写法(单个JEXL3函数处理所有逻辑,避免多次调用):
${jexl3(vars.get("compilerstatus") != "Compilation Successful." && vars.get("compilerstatus2") != "Compilation Successful." && vars.get("compilerstatus3") != "Compilation Successful." && vars.get("compilerstatus4") != "Compilation Successful." && vars.get("counter_value") as int < 9)}
逻辑说明
- 只要任意一个状态变量等于
Compilation Successful.,整个表达式结果为false,循环直接终止 - 当所有状态都未成功,但循环次数达到9次时,
counter_value <9为false,表达式结果为false,循环终止 - 仅同时满足「所有状态未成功」和「次数小于9」两个条件时,表达式为true,循环继续执行
内容的提问来源于stack exchange,提问作者Muthu
相关产品推荐
相关产品推荐

