需求:A-B-C三个功能需要单交易负载测试。其中,功能C的请求入参是功能A、B的出参(返回值),为了能够单独压测C功能,需要把A、B生成的出参保存至csv或txt文件中,作为参数化文件。
实现:1)提取功能A产生的(cookie)需要使用Beanshell Preprocessor插件及cookie管理器;cookie管理器(一定要勾上每次迭代都要清理cookies)
Beanshell Preprocessor插件:
Beanshell代码:
String str=prev.getResponseHeaders();//获取请求返回头信息
String start="this.sid=";//定义cookie的开始位置
String end=";Path="://定义cookie的结束位置
String cook =str substring(str.indexOf(start)+start.length0,str.indexOf(end));//根据起止信息获取cookie
vars.put("cookies".cook);//把cookie信息保存到meter变量中,方便后续使用
提取功能B产生的(studentid)出参字段,需要使用正规表达式提取器(Regular Exception Extractor);
2)将功能A、B产生的数据写入文件操作,使用jmeter beanshell sampler插件。
Beanshell代码:
import java.io.:
public static void writeExternalfile){
String studentld =vars.get("studentld");//获取studentld
String cookies=vars.get("cookies")://获取cookie
//保存文件名及目录
File file=new File("C:\\tools\\apache-jmeter-3.2-plugins\\bin\\csh\\playGameVar.txt");
FileOutputStream fos=nul;
if(!file.existsO){
file.createNewFile);
fos=new FileOutputStream(file);
}else{
fos=new FileOutputStream(file,true);
}
OutputStreamWriter osw=new OutputStreamWriter(fos,"UTF-8");
try{
osw.write(studentld+","+cookies);//写入studentid与cookie值,并以逗号隔开
osw.write("\r\n");//换行
osw.close0;//关闭
}catch(IOException e){
e.printStackTrace();
}
}
writeExternalfile();//调用写入文件方法
看下执行效果:
生成的文件及文件内容: