概述
上面一章讲了BeanShell preprocessor的基本用法,本次主要讲更深一步的嵌套循环,
本文中所需要的jar包有:libextfastjson -1.2.7.jar postgresql -42.2.5.jar
例如:在使用jmeter做并发测试时,在一个POST请求中,body需要做嵌套循环,则就需要使用前置处理器,body如下:
message:[
{""headerVo":{"contractNumber":"A0001","schedelId":"B0001"},"lineVo":[{"lineId":"C0001","name":"appid"},{"lineId":"C0002","name":"appid"},{"lineId":"C0003","name":"appid""}]},
{""headerVo":{"contractNumber":"A0001","schedelId":"B0002"},"lineVo":[{"lineId":"C0004","name":"appid"},{"lineId":"C0005","name":"appid"},{"lineId":"C0006","name":"appid""}]},
{""headerVo":{"contractNumber":"A0002","schedelId":"B0003"},"lineVo":[{"lineId":"C0007","name":"appid"},{"lineId":"C0008","name":"appid"},{"lineId":"C0009","name":"appid""}]},
{""headerVo":{"contractNumber":"A0002","schedelId":"B0004"},"lineVo":[{"lineId":"C0010","name":"appid"},{"lineId":"C0011","name":"appid"},{"lineId":"C0012","name":"appid""}]}
]
数据类型如下:1个合同(contractNumber)对应3个PL(schedelId),1个PL对应3个订单行(lineId),则一个合同对应6个订单行(这里只是举例,工作中一般都是1对200个)
contractNumber | schedelId | lineId |
A0001 | B0001 | C0001 |
A0001 | B0001 | C0002 |
A0001 | B0001 | C0003 |
A0001 | B0002 | C0004 |
A0001 | B0002 | C0005 |
A0001 | B0002 | C0006 |
A0002 | B0003 | C0007 |
A0002 | B0003 | C0008 |
A0002 | B0003 | C0009 |
A0002 | B0004 | C0010 |
A0002 | B0004 | C0011 |
A0002 | B0004 | C0012 |
import java.util.ArrayList ;
import java.util.List ;
import com.alibaba.fastjson.*;
String stust1= "{"lineId":"1111111","name":"appid"}" ; //订单行
String stust2 = "{"contractNumber":"2222222","schedelId":"33333333"}" ; //PL行
String stust3 = "{"headerVo":"44444444","lineVo":"55555555"}" ; //合成订单行和PL行
List listtest1 = new ArrayList() ;
List listtest2 = new ArrayList() ;
List listtest3 = new ArrayList() ;
List listtest4 = new ArrayList() ;
List listtest5 = new ArrayList() ;
List listtest6 = new ArrayList() ;
String regx = """ ;
int c = Integer.parseInt( vars.get("lineId_#"); 这个意思就是一个迭代循环多少次,为了达到上面body的效果,一次设置成12次
int m = 0 ; int k = 0 ; int n = 0 ;
String res2 ="";
String res4 ="";
String res6 ="";
String res7 ="";
String res8 ="";
for( int i = 0 ; i < c ; i++ ){
//lineId 订单行数据处理
listtest1.add(stust1);
String res1 = "" ;
res1 = (String )listtest1.get(m) ;
JSONObject jsonob1 = JSONObject.parseobject(res1) ;
int j = i +1 ;
jsonob1.put("contractNumber",vars.get("lineId_"+j));
res2 = jsonob.toJSONString() ;
listtest2.add(res2) ;
m = m +1 ;
//PL分支,因为1个PL对应3个订单行(订单行是最末端,也就是最小粒度)
if( i%3 == 0){
listtest3.add(stust2);
String res3 = "" ;
res3 = (String )listtest1.get(k) ;
JSONObject jsonob2 = JSONObject.parseobject(res3) ;
jsonob2.put("contractNumber",vars.get("contractNumber_"+j)); //这里的contractNumber和schedelId都是从数据库中获取,当然也有其他的获取方式,这里就步再重复了
jsonob2.put("schedelId",vars.get("schedelId_"+j));
res4 = jsonob2.toJSONString() ;
//合并PL和订单行
listtest5.add(stust3);
String res5 = "" ;
res5 = (String )listtest1.get(n) ;
JSONObject jsonob3 = JSONObject.parseobject(res5) ;
jsonob3.put("headerVo",res4 );
jsonob3.put("headerVo",listtest2);
res6 = jsonob3.toJSONString() ;
listtest6.add(res6) ;
listtest2.clear ;
res4="";
k = k +1 ;
n = n+1 ;
m = 0 ;
} }
res7 = listtest6.toString().replace("\"",""").replace(""{" , "{").replace("}"" , "}");
res8 = res7.repalce(regx,"\"");
vars.putObject("jrbody",res8);
最后
以上就是开放抽屉为你收集整理的jmeter组件-前置处理器-BeanShell preprocessor 嵌套循环(二)的全部内容,希望文章能够帮你解决jmeter组件-前置处理器-BeanShell preprocessor 嵌套循环(二)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复