jmeter-BenaShell的使用
BeanShell是一個(gè)小型嵌入式Java源代碼解釋器,具有對象腳本語言特性,能夠動態(tài)地執(zhí)行標(biāo)準(zhǔn)JAVA語法,并利用在JavaScript和Perl中常見的松散類型、命令、閉包等通用腳本來對其進(jìn)行拓展。
前置處理器:請求發(fā)送之前對請求參數(shù)做一些處理
后置處理器:請求發(fā)送完成之后對響應(yīng)數(shù)據(jù)進(jìn)行處理
BeanShell Sampler 取樣器
BeanShell PreProcessor 預(yù)處理程序
BeanShell PostProcessor BeanShell后置處理器,主要用來對響應(yīng)數(shù)據(jù)進(jìn)行處理
BeanShell Assertion BeanShell斷言,主要對后端返回的比較復(fù)雜的數(shù)據(jù),要進(jìn)行復(fù)雜的業(yè)務(wù)邏輯處理,進(jìn)行斷言。
_BeanShell 函數(shù)
設(shè)置隨機(jī)數(shù)
數(shù)據(jù)加密
響應(yīng)報(bào)文數(shù)據(jù)提取
數(shù)據(jù)庫數(shù)據(jù)提取
復(fù)雜邏輯結(jié)構(gòu)斷言
BeanShell預(yù)處理器-隨機(jī)生成數(shù)據(jù)
例子:某一平臺下的新增接口,隨機(jī)生成新增的數(shù)據(jù)
//隨機(jī)生成一個(gè)用戶名
public static String getUsername(int length) {
String str = "abcdefghijklmnopqrstuvwxyz";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for(int i = 0; i < length; i++){
//生成一個(gè)隨機(jī)的int值
int number = random.nextInt(str.length());
sb.append(str.charAt(number));
}
String username = "wangmin" + sb;
return username.toString();
}
//隨機(jī)生成一個(gè)序列號
public static String getorganizationCode(int length){
String str = "0123456789";
Random random = new Random();
StringBuffer sb = new StringBuffer();
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);
結(jié)果:每次運(yùn)行均隨機(jī)產(chǎn)生不同數(shù)據(jù),運(yùn)行結(jié)果正確,響應(yīng)狀態(tài)碼:200,message:success
{"errorcode":0,"message":"success!","data":{"operatorId":"2107","name":"wangmingdq","address":"詳細(xì)地址","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":"詳細(xì)地址","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后置處理器-提取響應(yīng)內(nèi)容置于log中
文章來源:http://www.zghlxwxcb.cn/news/detail-416456.html
//提取響應(yīng)內(nèi)容
String statusCode = prev.getResponseCode(); //獲取響應(yīng)代碼
String response_body = prev.getResponseDataAsString(); //獲取響應(yīng)body內(nèi)容
String response_headers = prev.getResponseHeaders(); //獲取響應(yīng)頭
String response_reason = prev.getResponseMessage(); //獲取響應(yīng)信息
log.info("=====響應(yīng)代碼:=====>>"+statusCode);
log.info("=====響應(yīng)body:=====>>"+response_body);
log.info("=====響應(yīng)頭:=====>>"+response_headers);
log.info("=====響應(yīng)信息:=====>>"+response_reason);
bin下的jmeter.log文章來源地址http://www.zghlxwxcb.cn/news/detail-416456.html
2021-05-08 15:19:37,411 INFO o.a.j.u.BeanShellTestElement: =====響應(yīng)代碼:=====>>200
2021-05-08 15:19:37,411 INFO o.a.j.u.BeanShellTestElement: =====響應(yīng)body:=====>>{"errorcode":0,"message":"success!","data":{"operatorId":"2109","name":"wangminmjf","address":"詳細(xì)地址","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: =====響應(yīng)頭:=====>>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: =====響應(yīng)信息:=====>>
2021-05-08 15:19:37,412 INFO o.a.j.t.JMeterThread: Thread is done: 充電云運(yùn)營商管理 1-1
2021-05-08 15:19:37,413 INFO o.a.j.t.JMeterThread: Thread finished: 充電云運(yùn)營商管理 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*)
到了這里,關(guān)于jmeter-BeanShell預(yù)處理器與BeanShell后置處理器的使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!