JMeter 将变量设置为随机选项 [英] JMeter set variable to random option

查看:24
本文介绍了JMeter 将变量设置为随机选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 JMeter,我知道 __Random__RandomString 函数.我需要选择一个随机选项并将其存储在一个变量中,因为它将用作多个调用的参数路径的一部分.例如:

I've been using JMeter and I'm aware of the __Random and __RandomString functions. I need to pick a random option and store it in a variable because it will be used as part of a parameter path for multiple calls. For example:

http://www.example.com/pets/{随机选项例如:猫、狗、长尾小鹦鹉}/

我尝试过像这样简单,我将变量 ${query} 设置为 onetwothree 使用一个随机控制器,将用户定义的变量作为子变量.这似乎应该工作,但是我总是将 ${query} 设置为 three.

I've tried doing simple like this, where I set the variable ${query} to one, two, or three using a random controller with userdefined variables as children. This seems like it should work, however I always get ${query} set to three.

任何见解或想法都会很受欢迎.提前致谢.

Any insight or ideas are will be well recieved. Thanks to all in advance.

推荐答案

您可以使用 Beanshell Pre处理器生成随机值

You can use Beanshell Pre Processor to generate random value

    String[] query = new String[]{"cat", "dog", "parakeet"};
    Random random = new Random();
    int i = random.nextInt(query.length);
    vars.put("randomOption",query[i]);

之后在您的 HTTP 请求

http://www.example.com/pets/${randomOption}

作为 String[] query = new String[]{"cat", "dog", "parakeet"}; 的替代方案,您可以使用 Beanshell 预定义的 Parameters 节.

As an alternative to String[] query = new String[]{"cat", "dog", "parakeet"}; you can use Beanshell pre-defined Parameters stanza.

    Random random = new Random();
    int i = random.nextInt(query.length);
    vars.put("randomOption",bsh.args[i]);

这篇关于JMeter 将变量设置为随机选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆