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

查看:2436
本文介绍了如何在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.

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

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天全站免登陆