Jmeter: how to initialize a map once and share it for all threads in a thread group

I have one group of threads in my j-meter testing plan , and I want to pre-initialize two cards. as

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 someone help me sort this out?

+4
source share
2 answers

Depending on which test item you use for the scripts, there may be 2 options:

  • If you use Beanshell Sampler , the simplest option uses bsh.shared namespace as

    In the group of the first thread:

    Map myMap1 = new HashMap();
    myMap1.put("foo","bar");
    bsh.shared.myMap = myMap1;
    

    In the second group of threads:

    Map myMap1 = bsh.shared.myMap;
    log.info(myMap1.get("foo"));
    
  • "" - JMeter. JMeter props script (JSR223 Sampler, BSF Sampler ..), java.util.Properties, put(), Java . ,

    :

    Map myMap1 = new HashMap();
    myMap1.put("foo","bar");
    props.put("myMap", myMap1);
    

    :

    Map myMap1 = props.get("myMap");
    log.info(myMap1.get("foo"));
    
+3

, Singleton Object. , .

: -

import java.util.HashMap;

public class SingletonMap {
    private  HashMap myMap1 = null;
    private  HashMap myMap2 = null;
    private static volatile SingletonMap singletonMapObj = null;

    private SingletonMap(){
        myMap1 = new HashMap();
        myMap2 = new HashMap();

        myMap1.put("foo1","bar1");
        myMap1.put("foo2","bar2");

        myMap2.put("mykey",myMap1);
    }

    public static SingletonMap getSingletonMap(){
        if(singletonMapObj == null){
            new SingletonMap();
        }

        return singletonMapObj;
    }
}
0

All Articles