jmeter-BeanShell预处理器与BeanShell后置处理器的使用

jmeter-BenaShell的使用
BeanShell是一个小型嵌入式Java源代码解释器,具有对象脚本语言特性,能够动态地执行标准JAVA语法,并利用在JavaScript和Perl中常见的松散类型、命令、闭包等通用脚本来对其进行拓展。
前置处理器:请求发送之前对请求参数做一些处理
后置处理器:请求发送完成之后对响应数据进行处理

BeanShell Sampler 取样器
BeanShell PreProcessor 预处理程序
BeanShell PostProcessor BeanShell后置处理器,主要用来对响应数据进行处理
BeanShell Assertion BeanShell断言,主要对后端返回的比较复杂的数据,要进行复杂的业务逻辑处理,进行断言。
_BeanShell 函数

设置随机数
数据加密
响应报文数据提取
数据库数据提取
复杂逻辑结构断言

BeanShell预处理器-随机生成数据
例子:某一平台下的新增接口,随机生成新增的数据

 

 

 

 

 1 //随机生成一个用户名
 2 public static String getUsername(int length) {
 3     String str = "abcdefghijklmnopqrstuvwxyz";
 4     Random random = new Random();
 5     StringBuffer sb = new StringBuffer();
 6     for(int i = 0; i < length; i++){
 7         //生成一个随机的int值
 8          int number = random.nextInt(str.length());
 9         sb.append(str.charAt(number));
10     }
11     String username = "wangmin" + sb;
12     return username.toString();
13 }
14 
15 //随机生成一个序列号
16 public static String getorganizationCode(int length){
17     String str = "0123456789";
18     Random random = new Random();
19     StringBuffer sb = new StringBuffer();
20     for(int i = 0; i < length; i++){
21         int number = random.nextInt(str.length());
22         sb.append(str.charAt(number));
23     }
24     String organizationCode = "009" + sb;
25     return organizationCode.toString();
26 }
27 
28 
29 String name = getUsername(3);
30 log.info(name);
31 vars.put("name",name);
32 
33 String organizationCode = getorganizationCode(6);
34 log.info(organizationCode);
35 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":""}}
1
{"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":""}}
1
BeanShell后置处理器-提取响应内容置于log中

 

 1 //提取响应内容
 2 String statusCode = prev.getResponseCode(); //获取响应代码
 3 String response_body = prev.getResponseDataAsString(); //获取响应body内容
 4 String response_headers = prev.getResponseHeaders();   //获取响应头
 5 String response_reason = prev.getResponseMessage();   //获取响应信息
 6 
 7 log.info("=====响应代码:=====>>"+statusCode);
 8 log.info("=====响应body:=====>>"+response_body);
 9 log.info("=====响应头:=====>>"+response_headers);
10 log.info("=====响应信息:=====>>"+response_reason);

bin下的jmeter.log

2021-05-08 15:19:37,411 INFO o.a.j.u.BeanShellTestElement: =====响应代码:=====>>200
2021-05-08 15: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-08 15:19:37,412 INFO o.a.j.u.BeanShellTestElement: =====响应头:=====>>HTTP/1.1 200 
Server: nginx/1.15.6
Date: Sat, 08 May 2021 07: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: 1620458377964

2021-05-08 15:19:37,412 INFO o.a.j.u.BeanShellTestElement: =====响应信息:=====>>
2021-05-08 15:19:37,412 INFO o.a.j.t.JMeterThread: Thread is done: 充电云运营商管理 1-1
2021-05-08 15:19:37,413 INFO o.a.j.t.JMeterThread: Thread finished: 充电云运营商管理 1-1
2021-05-08 15:19:37,413 INFO o.a.j.e.StandardJMeterEngine: Notifying test listeners of end of test
2021-05-08 15:19:37,413 INFO o.a.j.g.u.JMeterMenuBar: setRunning(false, *local*)

  

————————————————
版权声明:本文为CSDN博主「wangmin2334158648」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44406011/article/details/116526063

posted @ 2022-06-17 17:46  忧伤恋上了快乐  阅读(2735)  评论(0编辑  收藏  举报