如何在 Jmeter 变量中存储数组值? [英] How to store array values in Jmeter variables?

查看:59
本文介绍了如何在 Jmeter 变量中存储数组值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 CSV 文件,其中包含我使用 Bean Shell 脚本读取的数据并基于它填充 ArrayList.下面是它的代码.

I have a CSV file containing data which I read using a Bean Shell script and populate an ArrayList based upon it.Below is the code for it.

//Populate Beanshell script
import java.text.*;
import java.io.*;
import java.util.*;

ArrayList strList = new ArrayList();    

try {
File file = new File("path/to/csv");

if (!file.exists()) {
    throw new Exception ("ERROR: file not found");
}

BufferedReader bufRdr = new BufferedReader(new FileReader(file));
String line = null;

while((line = bufRdr.readLine()) != null) {
    strList.add(line);
}

bufRdr.close();   
}
catch (Exception ex) {
IsSuccess = false; 
log.error(ex.getMessage());
System.err.println(ex.getMessage());
}
catch (Throwable thex) {
System.err.println(thex.getMessage());
}

现在我想以随机方式利用这些数据,所以我尝试使用这样的东西

Now I want to utilize this data in a random manner so I am trying to use something like this

//Consumer bean shell script
//Not able to access strList since vars.put cannot store an object
Random rnd = new java.util.Random(); 
vars.put("TheValue",strList.get(rnd.nextInt(strList.size())));

但我无法做到这一点,因为在 vars.put 中我无法存储数组或列表,我只能存储原始类型.所以我无法从另一个 BeanShell 访问填充函数的 ArrayList脚本.

But I am unable to do this because in vars.put I cannot store an array or a list,I can only store only primitive types.So there is no way in which I can access the populate function's ArrayList from an another BeanShell script.

我如何在这种情况下实现随机化,因为从性能的角度来看,每次调用 populate 函数都不好.

How do I achieve randomization in this scenario since calling populate function each and every time is not good from a performance point of view.

推荐答案

我建议使用 bsh.共享 命名空间,这样您就可以存储任何 Java 对象并在需要时从不同的线程组访问它.

I would recommend using bsh.shared namespace, this way you will be able to store any Java object and access it even from different Thread Groups if needed.

JMeter 特定示例在官方文档中,共享变量 章节

JMeter-specific example is in the official documentation, in Sharing Variables chapter

在第一个脚本的结尾:

bsh.shared.strList = strList;

在第二个脚本的开头:

List strList = bsh.shared.strList;


Random rnd = new java.util.Random(); 
vars.put("TheValue",strList.get(rnd.nextInt(strList.size())));

参见 如何使用 BeanShell:JMeter 最喜欢的内置组件 有关 JMeter Beanshell 脚本的更多详细信息的指南.

See How to use BeanShell: JMeter's favorite built-in component guide for more details on Beanshell scripting for JMeter.

这篇关于如何在 Jmeter 变量中存储数组值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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