Avoid Hashmap overwriting in Jmeter with multiple requests

Viewed 51

I have added a Beanshell post processor in a Jmeter thread group and I have multiple HTTP requests in the same thread group that uses the post processor. I am creating a Hashmap and putting values into the map after execution of every HTTP request and then creating a json object of the map. The issue is that after every HTTP request, the map values are getting replaced by only the latest values of the last HTTP request that was executed. How to avoid this?

The code in my postprocessor is as follows:

HashMap map = new HashMap();
map.put(extID,intID);  

object = new JSONObject(map);

vars.put("idMap" , object.toString());

JSONParser parser = new JSONParser();
JSONObject json = (JSONObject)parser.parse(vars.get("idMap"));
2 Answers

JMeter's Post-Processors obey JMeter Scoping Rules so if you place the PostProcessor at the same level as Samplers - the PostProcessor will be executed after each sampler

If you want it to be executed only after a specific Sampler - make the PostProcessor a direct child of this sampler

enter image description here

Also be aware that since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting so it worth considering migrating, you won't have to change a single line of your code if you want, or alternatively Groovy has built-in JSON support

You can create a map and share with all threads using props, example:

Map<String, String> map = props.get("map")
if (map == null) {
    map = new HashMap<>()
} else {
    // in your case map.put(extID,intID); 
    String uuid = UUID.randomUUID().toString().replace("-", "")
    map.put(uuid,uuid)
}
props.put("map", map)
vars.putObject("map", map)
Related