Search code examples
javahashmapjmeterjmsjsr223

JMeter - Store/update a hashmap object in a variable/property


I need to create a hash-map inside of a JSR233 sampler that will contain certain headers and properties elements for an external java utility that I will call using a Java Request Sampler. I am going to need to create many hashmaps as the key-value pairs will vary based on the systems I am testing. For example, I am going to have to change the JMSReplyTo, the JMSCorrelationID, $TextBody: fields for every hash-map. All of this is done inside one thread group, but I may also want to implement it in other thread groups down the road.

How should I structure my Test Plan? Is it possible to create a global variable that will hold the hash-map? And then inside the respective JSR223 Sampler, for each test, modify the value of the variable?

I don't know when to use properties and when to use variables. In this case I would like 1 var or property that will change throughout my test-plan whenever I create a new hash-map object. I would like to know if this route is feasible, or if an alternative is advised.

The reason I would like to create a HashMap object in JMeter is because I don't want to have thousands of txt files that the Java utility will read and parse for the key-value pairs (those values vary in every txt file).

Instead I was wondering if there was a way to create this HashMap object in Jmeter and store it in a variable/property object that can be passed as a parameter in my Java Request.

I would appreciate it if someone guide me in the right direction.


Solution

  • Instead of using regular get put operations

     vars.get("map"); vars.put("map", map);
    

    You can use get put operations on Objects other than string:

     vars.putObject("map", map);
    

    And get map later:

     map = vars.getObject("map");