温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

jmeter4.0 统计结果次数 BeanShell Sampler,Debug Sampler

发布时间:2020-04-06 08:13:16 来源:网络 阅读:2349 作者:btmaxyyq 栏目:开发技术

jmeter4.0 统计结果次数 BeanShell Sampler,Debug Sampler

业务场景:
抽奖活动,程序按比例分配奖品,测试员想模拟100次抽奖,获取抽奖的结果,分析大致的概率

1.setUp Thread Group 前置,右击添加 BeanShell Sampler
输入

props.put("a",0);
props.put("b",0);
props.put("c",0);
props.put("d",0);
props.put("e",0);
props.put("f",0);
props.put("g",0);

这边犹如一个map 存放键值对
这段一定要前置,否则每次运行都会将value回0
2.添加线程组-》添加http请求
http请求下添加JSON Extractor(因为我们需要从接口返回的json中获取信息进行统计)
下列是我的接口返回回来的json数据
{"code":200,"msg":"0.3%加息劵","weight":1}

JSON Extractor中设置
JSONPath Expression: $.msg
names of created variables:messageYyq
3.添加BeanShell Sampler
此BeanShell Sampler事在线程组下的 会被多次执行
代码如下:


String value = vars.get("messageYyq");

if("飞科剃须刀".equals(value)){
    int x = props.get("a")+1;
    props.put("a",x);
    }
if("赤霞珠干红酒".equals(value)){
    int x = props.get("b")+1;
    props.put("b",x);
    }
if("亚麻籽油".equals(value)){
    int x = props.get("e")+1;
    props.put("e",x);
    }
if("30元返现劵".equals(value)){
    int x = props.get("f")+1;
    props.put("f",x);
    }
if("50元京东E卡".equals(value)){
    int x = props.get("g")+1;
    props.put("g",x);
    }
if("0.3%加息劵".equals(value)){
    int x = props.get("d")+1;
    props.put("d",x);
    }
if("1%加息劵".equals(value)){
    int x = props.get("c")+1;
    props.put("c",x);
    }

4.添加Debug Sampler,将jmeter properties 设置为true

最后Debug Sampler运行 结果如下:
    START.YMD=20180612
TESTSTART.MS=1528853588059
a=0
b=0
beanshell.server.file=../extras/startup.bsh
c=0
classfinder.functions.contain=.functions.
classfinder.functions.notContain=.gui.
cookies=cookies
cssParser.className=org.apache.jmeter.protocol.http.parser.CssParser
cssParser.types=text/css
csvdataset.file.encoding_list=UTF-8|UTF-16|ISO-8859-15|US-ASCII
d=20
e=0
f=0
g=0

可以看出 变量次数都有输出


当然也可以不用Debug Sampler
在第二个BeanShell Sampler代码最后加上

String cc = "a:"+props.get("a")+"d:"+props.get("d");

    return cc;

这样在 结果树中的BeanShell Sampler里的响应数据里 也能看到 更为清晰 。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI