添加客服微信
400 035 7887
021-60725088-8054
JMeter的线程组里有一个设置, 停止线程,停止测试,立即停止测试。但该设置在发生一个错误时,即会触发相应的停止操作。而我们在负载测试,自动化测试中,可以容忍少量的错误,仅仅当错误数量过大时,才想停止测试。所以这时候我们需要借助JSR223 Listener 来实现。
首先监听每一个取样器的结果,如果失败将失败数+1
当失败数大于某个设定的阈值后,停止测试
在JSR223 Listener 中添加如下代码(groovy脚本)
import java.nio.channels.FileLock;import java.nio.channels.FileChannel;import java.io.File;
try{ // Get a file channel for the file File file = new File("lock.file"); FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
// Use the file channel to create a lock on the file. // This method blocks until it can retrieve the lock.
FileLock lock = channel.lock(); if(!prev.isSuccessful()){ if( vars.get("totalErrorCount")!= null){
vars.put("totalErrorCount",String.valueOf(Integer.valueOf(vars.get("totalErrorCount")) + 1)); } else{
vars.put("totalErrorCount","1"); }
log.info("totalErrorCount: "+vars.get("totalErrorCount"));
if(Integer.valueOf(vars.get("totalErrorCount"))> 10 ) { ctx.getEngine().stopTest(); } }
channel.close(); // Close the file
}catch (Exception e){ log.info(e.toString());}
运行的console日志:
2021-08-27 00:06:44,099 INFO o.a.j.v.J.JSR223 监听器: totalErrorCount: 12021-08-27 00:06:44,110 INFO o.a.j.v.J.JSR223 监听器: totalErrorCount: 22021-08-27 00:06:44,120 INFO o.a.j.v.J.JSR223 监听器: totalErrorCount: 32021-08-27 00:06:44,132 INFO o.a.j.v.J.JSR223 监听器: totalErrorCount: 42021-08-27 00:06:44,147 INFO o.a.j.v.J.JSR223 监听器: totalErrorCount: 52021-08-27 00:06:44,161 INFO o.a.j.v.J.JSR223 监听器: totalErrorCount: 62021-08-27 00:06:44,174 INFO o.a.j.v.J.JSR223 监听器: totalErrorCount: 72021-08-27 00:06:44,185 INFO o.a.j.v.J.JSR223 监听器: totalErrorCount: 82021-08-27 00:06:44,197 INFO o.a.j.v.J.JSR223 监听器: totalErrorCount: 92021-08-27 00:06:44,209 INFO o.a.j.v.J.JSR223 监听器: totalErrorCount: 102021-08-27 00:06:44,222 INFO o.a.j.v.J.JSR223 监听器: totalErrorCount: 11
相关阅读:
本文转载自网络,如有侵权等问题请联系我们删除。
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-60725088-8054),我们将立即处理,马上删除。