Loop request with different data in Postman

Loop request with different data in Postman

Hello everyone, I am Kent, backend developer of the SI team. Today, I would like to share a topic about Loop requests with different data in Postman.
Loop request with different data in Postman
Table of contents

Introduction

One day, I got a task to investigate performance issues in the production environment. 

After checking the log, the issue happens when the search result returns more than 50,000 user records.

In the SI system, we can invite another user by email, so how can I invite 50,000 users to verify it on a staging environment ?

If I input the email then click Invite button in 5 seconds, it will take 5 * 50,000 / 3600 = 69.4 hours to create 50,000 records.

After googling, I found another way to do it. That is using Postman to automatically send requests. Let’s try this.

Setup

Body tab

The body for Invite API is simple with just the email. The is the syntax to reference a variable in Postman.

Pre-request Script tab

The Pre-request Script tab will execute JavaScript before a request runs. For short, the script below creates an array of emails for the first time, and then shifts each email from the array as the email input to call Invite API.

Note that I use test.moneyforward.vn domain to prevent sendgrid from sending email.

Tests tab

By setting Invite API requestID to the function setNextRequest, it will continue to call the Invite API until the emails array is empty. I also put a test to check whether the call success or not.

Execute

Postman Collection Runner

Let’s execute 100 requests with Postman Collection Runner.

Result:

It takes 3.5 minutes to finish 100 requests. To finish 50,000 request it will take 3.5 * 500 / 60 = 29.1 hours. It is much better than manually inputting email and clicking the Invite button right ?

But the Postman Free plan gives you a limited number (25) of collection runs you can use each month. Let’s try running with the Command Line.

Running collections on the command line with Newman

First, export the collection as a json file. The Invite API requires jwt_access and X-CSRF-Token for authentication. For some reason, the jwt_access is not exported in the file, so I had to add it manually.

Execute the script: newman run invite_user.json -n 100

Result:

It takes nearly 5 minutes to finish 100 requests. To finish 50,000 request it will take 5 * 500 / 60 = 41.6 hours. You think it is still a high number right ?

Using Newman, we have no limited collection runs, and we can reduce the time by executing it parallel. For example, if I run 5 sets of data concurrently, the time will be 41.6 / 5 = 8.3 hours. It is a reasonable number right ? So I just prepare sets of data, let Newman execute them, and I have time to do other tasks.

 

Thanks for reading. Hopefully this article is useful for you.

Reference:

 

More like this

Principal Software Engineer nói: “Ruby đi, dev cho nhanh”
Jan 05, 2022

Principal Software Engineer nói: “Ruby đi, dev cho nhanh”

MFV Tech Talk #3 - Service Operation | Kitto & Leon
Apr 12, 2024

MFV Tech Talk #3 - Service Operation | Kitto & Leon