jmeter使用Beanshell预处理器从指定列表中获取随机值

时间:2021-03-29 21:41:55

1、jmeter添加BeanShell PreProcessorjmeter使用Beanshell预处理器从指定列表中获取随机值

2、script输入框中设定如下变量randomOption,变量randomOption将从列表{"cat", "dog", "parakeet"}中随机取值,如事例:

String[] query = new String[]{"cat", "dog", "parakeet"};

    Random random = new Random();

    int i = random.nextInt(query.length);

    vars.put("randomOption",query[i]);

3、在需要配置的 HTTP Request请求中获取变量randomOption的值,如事例:

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

4、

如果要设置两个变量且变量值随机但不重复,可以通过两个列表放置不同值实现

 String[] query = new String[]{"cat", "dog", "parakeet","3"};

    Random random = new Random();

    int i = random.nextInt(query.length);

    vars.put("randomOption",query[i]);


 String[] t = new String[]{"5", "23", "42"};

    Random r = new Random();

    int j = r.nextInt(t.length);

    vars.put("a",t[j]);



使用参考http://4byte.cn/question/25579/jmeter-set-variable-to-random-option.html# 整理