jmeter 项目混合接口实战记录
1. 项目结构:
1.1 混合接口,创建事务控制器,将多个接口,按照业务逻辑,串行排列组合,放到一个事务里执行,接口之间采用后置beanshell 或者jsonpath 、正则关联
1.1 第一个接口beanshell 取样器,编辑参数化需要的随机参数,防止数据命中缓存
import org.apache.jmeter.config.Arguments;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
String japanesekanji ="张王李赵钱孙";
String fullkana = "abcdefgh";
String halfNumber = "1234567890";
vars.put("name1",name1);
vars.put("name2",name2);
vars.put("halfNumber",halfNumber);
try{
Date date = new Date(); //获取当前时间
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
String insuredDate = sf.format(date);
Calendar cal = Calendar.getInstance();
cal.setTime(sf.parse(insuredDate));
cal.add(Calendar.DAY_OF_YEAR,-10); //加1天
String insuredDate = sf.format(cal.getTime());
//cal.add(Calendar.DAY_OF_YEAR,+10); //加2天
cal.add(Calendar.DAY_OF_YEAR,-11);
String paymentTime = sf.format(cal.getTime());
cal.add(Calendar.DAY_OF_YEAR,+22);
String insuredDateStart = sf.format(cal.getTime());
vars.put("insuredDate",insuredDate);
vars.put("paymentTime",paymentTime);
vars.put("insuredDateStart",insuredDateStart);
long id = ${__Random(100000000,999999999,id)};
String channelOrgID = "C-I_D" + id.toString();
vars.put("channelOrgID",channelOrgID);
String channelOrgID = "C-I_D" + id.toString();
vars.put("channelOrgID",channelOrgID);
long id = ${__Random(100000000,999999999,id)};
String transNo = "TRNO" + id.toString();
vars.put("transNo",transNo);
}
catch(Exception e){
}
1.2 业务接口1,使用jsonpath 语法 获取想要的字段,传递给下个接口
2. 业务接口2,同业务接口1逻辑相同
1.3 业务接口3 的 后置beanshell,获取上个接口的请求数据中的个别字段
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
String response_data = prev.getResponseDataAsString(); //获取登录请求的响应信息
//log.info("response_data="+response_data);
//利用上一步获取的字符串形式的JSON,结合Java处理Json的方法,将需要的键值提取出来;
JSONObject responseJson = JSON.parseObject(response_data);
JSONArray data = jsonObject.getJSONObject("responseJson").getJSONArray("xxxxxx");
vars.put("xxxxx",xxxx)
1.4 倒数第二个业务接口,需要加入固定定时器,相当于loadrunner 的思考时间,不会记录到事务整体的运行结果当中