jmeter-BenaShell的使用
jmeter-BenaShell的使用
BeanShell是一个小型嵌入式Java源代码解释器,具有对象脚本语言特性,能够动态地执行标准JAVA语法,并利用在JavaScript和Perl中常见的松散类型、命令、闭包等通用脚本来对其进行拓展。
前置处理器:请求发送之前对请求参数做一些处理
后置处理器:请求发送完成之后对响应数据进行处理
BeanShell Sampler 取样器
BeanShell PreProcessor 预处理程序
BeanShell PostProcessor BeanShell后置处理器,主要用来对响应数据进行处理
BeanShell Assertion BeanShell断言,主要对后端返回的比较复杂的数据,要进行复杂的业务逻辑处理,进行断言。
_BeanShell 函数
设置随机数
数据加密
响应报文数据提取
数据库数据提取
复杂逻辑结构断言
BeanShell预处理器-随机生成数据
例子:某一平台下的新增接口,随机生成新增的数据
//随机生成一个用户名publicstatic String getUsername(int length){
String str ="abcdefghijklmnopqrstuvwxyz";
Random random =newRandom();
StringBuffer sb =newStringBuffer();for(int i =0; i < length; i++){//生成一个随机的int值int number = random.nextInt(str.length());
sb.append(str.charAt(number));}
String username ="wangmin"+ sb;return username.toString();}//随机生成一个序列号publicstatic String getorganizationCode(int length){
String str ="0123456789";
Random random =newRandom();
StringBuffer sb =newStringBuffer();for(int i =0; i < length; i++){int number = random.nextInt(str.length());
sb.append(str.charAt(number));}
String organizationCode ="009"+ sb;return organizationCode.toString();}
String name =getUsername(3);
log.info(name);
vars.put("name",name);
String organizationCode =getorganizationCode(6);
log.info(organizationCode);
vars.put("organizationCode",organizationCode);
结果:每次运行均随机产生不同数据,运行结果正确,响应状态码:200,message:success
{"errorcode":0,"message":"success!","data":{"operatorId":"2107","name":"wangmingdq","address":"详细地址","contacts":"汪敏","phone":"17664012113","proprietorship":null,"organizationCode":"009051270","longitude":118000000,"latitude":39000000,"status":5,"type":null,"provinceId":"356","cityId":"357","countyId":"358","append":"","createTime":1620457032,"updateTime":1620457032,"imageId":null,"imageUrl":""}}
{"errorcode":0,"message":"success!","data":{"operatorId":"2107","name":"wangmingdq","address":"详细地址","contacts":"汪敏","phone":"17664012113","proprietorship":null,"organizationCode":"009051270","longitude":118000000,"latitude":39000000,"status":5,"type":null,"provinceId":"356","cityId":"357","countyId":"358","append":"","createTime":1620457032,"updateTime":1620457032,"imageId":null,"imageUrl":""}}
BeanShell后置处理器-提取响应内容置于log中
//提取响应内容
String statusCode = prev.getResponseCode();//获取响应代码
String response_body = prev.getResponseDataAsString();//获取响应body内容
String response_headers = prev.getResponseHeaders();//获取响应头
String response_reason = prev.getResponseMessage();//获取响应信息
log.info("=====响应代码:=====>>"+statusCode);
log.info("=====响应body:=====>>"+response_body);
log.info("=====响应头:=====>>"+response_headers);
log.info("=====响应信息:=====>>"+response_reason);
bin下的jmeter.log
2021-05-0815:19:37,411 INFO o.a.j.u.BeanShellTestElement:=====响应代码:=====>>2002021-05-0815:19:37,411 INFO o.a.j.u.BeanShellTestElement:=====响应body:=====>>{"errorcode":0,"message":"success!","data":{"operatorId":"2109","name":"wangminmjf","address":"详细地址","contacts":"汪敏","phone":"17664012113","proprietorship":null,"organizationCode":"009582005","longitude":118000000,"latitude":39000000,"status":5,"type":null,"provinceId":"356","cityId":"357","countyId":"358","append":"","createTime":1620458377,"updateTime":1620458377,"imageId":null,"imageUrl":""}}2021-05-0815:19:37,412 INFO o.a.j.u.BeanShellTestElement:=====响应头:=====>>HTTP/1.1200
Server: nginx/1.15.6
Date: Sat,08 May 202107:19:38 GMT
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
X-Application-Context: server-gateway:docker:8506
apiRemark:%E4%BF%9D%E5%AD%98%E8%BF%90%E8%90%A5%E5%95%86%E4%BF%A1%E6%81%AF
requestTime:16204583779642021-05-0815:19:37,412 INFO o.a.j.u.BeanShellTestElement:=====响应信息:=====>>2021-05-0815:19:37,412 INFO o.a.j.t.JMeterThread: Thread is done: 充电云运营商管理 1-12021-05-0815:19:37,413 INFO o.a.j.t.JMeterThread: Thread finished: 充电云运营商管理 1-12021-05-0815:19:37,413 INFO o.a.j.e.StandardJMeterEngine: Notifying test listeners of end of test
2021-05-0815:19:37,413 INFO o.a.j.g.u.JMeterMenuBar:setRunning(false,*local*)
版权归原作者 汪敏wangmin 所有, 如有侵权,请联系我们删除。