本文介绍了多个随机值Jmeter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就我的脚本而言,我需要在50左右生成多个随机值,其中我的数据馈送器是一个数组.

For the purpose of my script, I need to generate multiple random values Around 50, where my data feeder is an array.

我可以通过代码复制来做到这一点,但是我更喜欢使用一些循环的智能方式.

I can manage to do it with code duplication but I prefer a smarter way using some loop.

我的代码(JSR223预处理器)如下:

My code (JSR223 PreProcessor) looks like:

import java.util.*;  

String[] categories = [0, 1, 2, 3, 4, 5]

int idx1 = new Random().nextInt(categories.length);
String category1 = (categories[idx1]);

int idx2 = new Random().nextInt(categories.length);
String category2 = (categories[idx2]);

vars.put("pickValue1", category1);
vars.put("pickValue2", category2);

然后我在脚本中使用pickValue1和pickValue2.

Then I use pickValue1, pickValue2 in the script.

如何使用更智能的循环,而无需复制/粘贴以下代码50倍?

How can I use smarter looping, without copy/paste 50 times the below code?

int idx1 = new Random().nextInt(categories.length);
String category1 = (categories[idx1]);

推荐答案

为避免代码重复:

String[] categories = [0, 1, 2, 3, 4, 5]
for (int i=0; i<categories.length; i++) {
   vars.put("pickValue" + (i+1), categories[new Random().nextInt(categories.length)]);
}

这篇关于多个随机值Jmeter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 06:14