Java 如何在 Jmeter 变量中存储数组值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26381643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:24:15  来源:igfitidea点击:

How to store array values in Jmeter variables?

javajmeter

提问by Madusudanan

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.

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

//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())));

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.

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

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.

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

采纳答案by Dmitri T

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

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

JMeter-specific example is in the official documentation, in Sharing Variableschapter

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

At the end of 1st script:

在第一个脚本的末尾:

bsh.shared.strList = strList;

At the beginning of the 2nd script:

在第二个脚本的开头:

List strList = bsh.shared.strList;


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

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

有关JMeter 的Beanshell 脚本编写的更多详细信息,请参阅如何使用 BeanShell:JMeter 最喜欢的内置组件指南。

回答by PeterMmm

vars.putonly supports String values. There is vars.putObject:

vars.put仅支持字符串值。有vars.putObject

Scripts can also access JMeter variables using the get() and put() methods of the "vars" variable, for example: vars.get("HOST"); vars.put("MSG","Successful"); . The get() and put() methods only support variables with String values, but there are also getObject() and putObject() methods which can be used for arbitrary objects. JMeter variables are local to a thread, but can be used by all test elements (not just Beanshell).

脚本还可以使用“vars”变量的 get() 和 put() 方法访问 JMeter 变量,例如: vars.get("HOST"); vars.put("MSG","成功"); . get() 和 put() 方法仅支持具有 String 值的变量,但还有 getObject() 和 putObject() 方法可用于任意对象。JMeter 变量对于线程来说是本地的,但可以被所有测试元素(不仅仅是 Beanshell)使用。

回答by manoj

Hope this will help someone.

希望这会帮助某人。

For sharing variables globally, i.e. among threads in all thread groups, use props.

要全局共享变量,即在所有线程组中的线程之间,请使用props.

For e.g,

例如,

  • In setup thread group, do this
    props.put("mylist", new ArrayList());

  • Now, in post processor for every thread in the thread group, add values to the list.
    props.get("mylist").add(<some value>);

  • In tear down thread group, fetch the entire list again.
    log.info(props.get("mylist").toString());

  • 在设置线程组中,执行此操作
    props.put("mylist", new ArrayList());

  • 现在,在线程组中每个线程的后处理器中,将值添加到列表中。
    props.get("mylist").add(<some value>);

  • 在拆卸线程组中,再次获取整个列表。
    log.info(props.get("mylist").toString());

In thread group, list is updated by multiple threads then prefer to use Vector instead of ArrayList.

在线程组中,列表由多个线程更新,然后更喜欢使用 Vector 而不是 ArrayList。

Ref : scope-of-variables-sharing-among-threads-and-thread-groups

参考:范围变量共享线程和线程组之间