Jmeter:如何一次初始化映射并为线程组中的所有线程共享它 [英] Jmeter : How to initialize map once and share it for all threads in a thread group
问题描述
我的 j-meter测试计划中有一个线程组,我想预先初始化两个映射.喜欢
I am having one thread groups in my j-meter test plan and I want to pre-initialize two map. like
java.util.HashMap myMap1 = new java.util.HashMap();
myMap1.put("foo1","bar1");
myMap1.put("foo2","bar2");
java.util.HashMap myMap2 = new java.util.HashMap();
myMap2.put("mykey",myMap1);
我必须将其用于不同的线程.有人可以帮助我解决此问题吗?
and I have to use it for different threads.Can anyone help me to sort out this problem?
推荐答案
根据您用于脚本编写的测试元素,可能有2种选择:
Depending on what test element you're using for scripting there could be 2 options:
-
如果您使用 Beanshell采样器-最简单的选择是使用 bsh.shared 命名空间作为
If you use Beanshell Sampler - the easiest option is using bsh.shared namespace as
在第一个线程组中:
Map myMap1 = new HashMap();
myMap1.put("foo","bar");
bsh.shared.myMap = myMap1;
在第二个线程组中:
Map myMap1 = bsh.shared.myMap;
log.info(myMap1.get("foo"));
更多通用"方式是使用JMeter属性.在任何启用脚本的测试元素(JSR223采样器,BSF采样器等)中,JMeter Properties的当前实例的简写都可以用作props
,它基本上是
More "generic" way is using JMeter Properties. A shorthand to current instance of JMeter Properties is available as props
in any script-enabled test element (JSR223 Sampler, BSF Sampler, etc.) and it is basically an instance of java.util.Properties class hence it has put() method which accepts arbitrary Java Object as value. So
在第一个线程组中:
Map myMap1 = new HashMap();
myMap1.put("foo","bar");
props.put("myMap", myMap1);
在第二个线程组中:
Map myMap1 = props.get("myMap");
log.info(myMap1.get("foo"));
这篇关于Jmeter:如何一次初始化映射并为线程组中的所有线程共享它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!