Search code examples
jmeteribm-cloudcloudantjmeter-plugins

Jmeter: How I can increase the size of Request json dynamically. (IBM cloudant db maximum acceptable json file size through Jmeter.)


I have to test in IBM cloudant db maximum acceptable JSON size through Jmeter. I have created a JSON file and I need to increase the no of the citizen details in the JSON file in JMeter. Take an example, I have created a JSON file and passing information of one citizen in the first iteration the next iteration it should be two citizens and it will keep increasing... and nth iteration it should be n no of citizen details.( JSON size also increase if I increase the body). How I can do this can anyone please suggest.

"docs": [
    {
        "name": "Nicholas",
        "age": 45,
        "gender": "male",
        "_attachments": {

        }
    },
    {
        "name": "Taylor",
        "age": 50,
        "gender": "male",
        "_attachments": {

        }
    }  
]
 }

Solution

  • If you want to add new entry to the JSON payload on each iteration of the Thread Group:

    1. Add JSR223 PreProcessor as a child of the HTTP Request sampler you want to parameterise
    2. Put the following code into "Script" area:

      import groovy.json.JsonBuilder
      import groovy.json.internal.LazyMap
      import org.apache.commons.lang3.RandomStringUtils
      import org.apache.commons.lang3.RandomUtils
      
      def data = []
      
      0.upto(vars.get('__jm__Thread Group__idx') as int, {
          def entry = new LazyMap()
          entry.put('name', RandomStringUtils.randomAlphabetic(10))
          entry.put('age', RandomUtils.nextInt(18, 99))
          entry.put('gender', 'male')
          entry.put('__attachments', {})
          data.add(entry)
      })
      
      def builder = new JsonBuilder()
      
      builder(docs: data.collect {
          [name: it.get('name'), age: it.get('age'), gender: it.get('gender'), __attachments: it.get('__attachments')]
      })
      
      sampler.getArguments().removeAllArguments()
      sampler.addNonEncodedArgument('', builder.toPrettyString(), '')
      sampler.setPostBodyRaw(true)
      
    3. That's it, on each iteration of the Thread Group the HTTP Request sampler will be sending an incremented number of "docs" with the random data.

    References: