JMeter 将变量设置为随机选项 [英] JMeter set variable to random option
问题描述
我一直在使用 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}
设置为 one
、two
或 three
使用一个随机控制器,将用户定义的变量作为子变量.这似乎应该工作,但是我总是将 ${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屋!