首页 > 解决方案 > 多个随机值 Jmeter

问题描述

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

我可以通过重复代码来做到这一点,但我更喜欢使用一些循环的更聪明的方式。

我的代码(JSR223 PreProcessor)看起来像:

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。

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

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

标签: jmeter

解决方案


为避免代码重复:

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)]);
}

推荐阅读