Sending multiple API requests every 2, 4, 6, and 8 seconds in a loop

Hi,

I want to send multiple API requests every 2, 4, 6, and 8 seconds in a loop as follows…

Loop Start
API Request 1
Delay 2 seconds
API Request 2
Delay 2 seconds
API Request 3
Delay 2 seconds
Loop End

Then I want to replace the 2 seconds above with 4, then 6, then 8 seconds for each pass through the loop. Any ideas on the best way to do this?

Thanks!

This will be a little bit tricky since JavaScript doesn’t really have a “sleep” API to pause for an exact amount of time when running procedural code. Also, if your test code is generating new requests, those are done in Promises and there’s no guarantee when they would return, so you would have to write out the test in a nested format like this:

// create a folder setup like this:
+ Collection Name
  |
  +-- Subfolder
         |
         +--- request

Then, in the “Collection” folder’s pre-request script, set an environment variable like this:

pm.environment.set('delay-time', 2)

Then, in your “request” test script, do something like this:

var delay = pm.environment.get('delay-time');

pm.sendRequest("https://localhost/request1", function (err, response) {
    // handle the response here
    // figure out some way to sleep for "DELAY" seconds

    pm.sendRequest("https://localhost/request2", function (err, response) {
        // handle the response here
        // figure out some way to sleep for "DELAY" seconds
        pm.sendRequest("https://localhost/request3", function (err, response) {
            // handle the response here
            // figure out some way to sleep for "DELAY" seconds
            if (delay * 2 < 64) {
                pm.environment.set('delay-time', delay * 2)
                pm.setNextRequest('request')
            } else {
                pm.setNextRequest(null)
            } 
        });
    });

});

Because pm.sendRequest() is asynchronous, you can’t simply do them “in order” as they would all get called around the same time and you wouldn’t know which one would respond first … nesting them in the structure I’ve outlined above should at least ensure that request 1 will be called, and then progress to 2, then to 3, at which point the code will do your exponential back-off and retry the API endpoints again by called pm.setNextRequest … once you hit a threshold of some sort, in this case I say less than 64, then you call pm.setNextRequest(null) which will stop the looping.

Another approach would be to make this structure:

// create a folder setup like this:
+ Collection Name
  |
  +-- Subfolder
         |
         +--- request1
         +--- request2
         +--- request3

… and the test code for each of those three requests would be something like this maybe:

// request 1's test code:
var delay = pm.environment.get('delay-time');
// figure out how to "sleep" for that many seconds
pm.setNextRequest('request2')

then

// request 2's test code:
var delay = pm.environment.get('delay-time');
// figure out how to "sleep" for that many seconds
pm.setNextRequest('request3')

then

// request 3's test code:
var delay = pm.environment.get('delay-time');
// figure out how to "sleep" for that many seconds

if (delay * 2 < 64) {
  pm.environment.set('delay-time', delay * 2)
  pm.setNextRequest('request1') // start over
} else {
  pm.setNextRequest(null)
}

Hey @iandouglas - many thanks for this super informative and detailed response!

Before I go down this road, I wondered if you or anyone in the community had thoughts on doing what I’m trying to accomplish here via Postman versus Apache JMeter?

A third option is a Powershell script I have developed that basically accomplishes the same thing.

I use Postman (great tool btw!) for single session API simulations and its works great for me for that.

I was asking about this question on this thread because I may need to get into load testing where I have multiple API sessions running concurrently. I’m that impressed with Postman that I thought I’d bring all my testing under the one roof here, but I’m not sure if Powershell scripts and/or JMeter are worth the learning curve effort or better suited to my purpose? Any input on this from you and/or the community much appreciated.

If you have access to a shell environment then I would just use Newman and it’s collection runner where you could control the timing yourself. If all of your requests are absolutely uniquely named, a fun hidden feature of Newman is that you can pass a single request as the “folder” argument. Your shell script can then control more precise timing, stop any time it sees an error, etc.

hey @teamgyst that previous reply was also from me, I was logged in on a different account on my phone. :slight_smile:

I understand what you’re trying to do regarding load testing. You might want to see about using a proper benchmarking tool like Apache Benchmark where it can give you full load results, timings, etc, if you’re looking to do something like what you’ve described.