How to send a lot of POST request in JSON format through JMeter?

Viewed 27

So I have this huge file of json requests that I need to send to an API through POST, they are about 4000 different requests. I tried the CSV method and reference the JSON_FILE in code but it didn't work due to a timeout error, I think 4000 files is just too much for this method

I could create 4000 thread groups, each one with it's individual json request but that would be a huge manual labor

Is there anyway to automatize this process?

The json looks basically like this

{
    "u_id": "00",
    "u_operation": "Address",
    "u_service": "Fiber",
    "u_characteristic": 2,
    "u_name": "Address #1"
},
{
    "u_id": "01",
    "u_operation": "Address",
    "u_service": "TV",
    "u_characteristic": 2,
    "u_name": "Address #2"
}

All the way up to 4000

1 Answers

What is the anticipated usage of the API endpoint? If it's supposed to process 4000 files at once and it doesn't - this sounds like a bug or a bottleneck and you need to report it.

If you have a large file with 4000 objects like this:

{
  "u_id": "00",
  "u_operation": "Address",
  "u_service": "Fiber",
  "u_characteristic": 2,
  "u_name": "Address #1"
}

and want to send them one by one with arbitrary number of users/iterations - you can play the following trick

  1. Add setUp Thread Group to your Test Plan

  2. Add JSR223 Sampler to the setUp Thread Group

  3. Put the following code into "Script" area:

    def entries = new groovy.json.JsonSlurper().parse(new File('/path/to/your/large/file.json'))
    
    entries.eachWithIndex { entry, index ->
        props.put('entry_' + (index + 1), new groovy.json.JsonBuilder(entry).toPrettyString())
    }
    

it will create 4000 JMeter Properties like entry_1, entry_2, each one holding one entry from your large file:

enter image description here

Then in the main Thread Group you will be able to use __P() and __counter() functions combination so each user would take the next "entry" on each iteration like:

${__P(entry_${__counter(FALSE,)},)}

enter image description here

Related