计算响应数据中字符串出现的次数 [英] Count the number of occurences of a string in response data

查看:132
本文介绍了计算响应数据中字符串出现的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从jmeter中的响应中搜索一个字符串,并根据要使用if控制器运行下一个请求的次数来计算发生的次数.我被用来计数发生次数的代码所困

I want to search a string from a response in jmeter and count the number of occurences based on which i want to use a if controller to run the next requests. I am stuck with the code for counting the occurences

推荐答案

您可以通过至少两种方式来做到这一点:

You can do it in at least 2 ways:

  1. 使用正则表达式提取器:

  • 将正则表达式提取器添加为请求的子代.
  • 配置如下:

  • Add Regular Expression Extractor as a child of the request.
  • Configure it as follows:

  • 参考名称:任何有意义的内容,即count
  • 正则表达式:您要计数的字符串,即JMeter
  • 模板:$1$
  • 比赛编号:-1
  • Reference Name: anything meaningful, i.e. count
  • Regular Expression: string you want to count, i.e. JMeter
  • Template: $1$
  • Match No: -1

匹配数将存储在${count_matchNr} JMeter变量

The number of matches will be stored in ${count_matchNr} JMeter Variable

使用 Bean Shell PostProcessor

  • 将Beanshell PostProcessor添加为请求的子项
  • 将以下代码放入PostProcessor的脚本"区域

  • Add Beanshell PostProcessor as a child of the request
  • Put the following code into the PostProcessor's "Script" area

import org.apache.commons.lang.StringUtils;

String response = new String(data);
int count = StringUtils.countMatches(response, "JMeter");

log.info("Found " + count + " \"JMeter\" words at the " + prev.getUrlAsString() + " URL");
vars.put("count", String.valueOf(count));

您将能够将匹配计数引用为${count} JMeter变量

You'll be able to refer the matches count as ${count} JMeter Variable

参考:

  • JMeter Regular Expressions
  • How to Use BeanShell: JMeter's Favorite Built-in Component

这篇关于计算响应数据中字符串出现的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆