在测试中,有时候会遇到要统计响应结果中某个参数出现了多少次,如果量级很大,一个一个数不太现实,下面讲一下实现自动打印出该参数出现的次数的方法。 例如我的响应信息为:{"ip":"1.1.1.1","ip":"1.1.1.1","ip":"1.1.1.1","ip":"1.1.1.1","ip":"1.1.1.1","ip":"1.1.1.1","ip":"1.1.1.1","ip":"1.1.1.1","ip":"1.1.1.1","ip":"1.1.1.1","ip":"1.1.1.1","ip":"1.1.1.1","ip":"1.1.1.1","ip":"1.1.1.1","ip":"1.1.1.1","ip":"1.1.1.1","ip":"1.1.1.1","ip":"1.1.1.1",} 现在请求上创建正则表达式提取器,把响应信息全部提取到变量中如下面的截图:
然后创建一个beanshell前置处理器,脚本如下: [url=][/url]
1 import java.util.regex.Matcher; 2 import java.util.regex.Pattern; 3 4 ip = vars.get("ip"); 5 subs = "\"ip\""; 6 Pattern pattern = Pattern.compile(subs); 7 Matcher matcher = pattern.matcher(ip); 8 int count=0; 9 while(matcher.find()){ 10 count++; 11 } 12 System.out.println(count);[url=][/url]
执行Jmeter,控制台就打印出数值了
|