jmeter-beanshellPostprocessor,
实现需求:判断响应时间大于10ms的响应,并记录想要的响应字段,比如流水号,注册的用户名密码等
代码如下:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//这里引入判断响应时间的代码,如判断响应时间超过10ms,则记录流水号
long responseTime = prev.getTime();
if (responseTime > 10) {
String response = prev.getResponseDataAsString();
// 定义正则表达式模式
Pattern patternUserId = Pattern.compile("\"userid\":\"([^\"]+)\"");
Pattern patternToken = Pattern.compile("\"token\":\"([^\"]+)\"");
Matcher matcherUserId = patternUserId.matcher(response);
Matcher matcherToken = patternToken.matcher(response);
String userid = "";
String token = "";
if (matcherUserId.find()) {
userid = matcherUserId.group(1);
}
if (matcherToken.find()) {
token = matcherToken.group(1);
}
// 接下来进行判断和写入文件
String code = "10000"; // 假设这里获取到的 code 为 10000
if (code.equals("10000")) {
File logFolder = new File("D:\\log");
if (!logFolder.exists()) {
logFolder.mkdirs();
}
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("D:\\log\\log.txt", true));
writer.write(userid + "," + token + "\n");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
标签:regex,prev,String,getTime,Pattern,userid,token,jmeter
From: https://www.cnblogs.com/yimouz-219/p/18262814