Search code examples
jmeterhashmap

Avoid Hashmap overwriting in Jmeter with multiple requests


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"));

Solution

  • 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)