Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:

>> GET ACCESS NOW

1. Overview

Load testing is a critical part of the software development life cycle (SDLC) for modern enterprise applications. In this tutorial, we'll use Postman collections to perform a simple load testing activity.

2. Setup

We can download and install the desktop client that's compatible with our system's operating system. Alternatively, we can create a free Postman account and access the web client.

Now, let's create a new collection called “Google Apps – Load Testing” by importing a few sample HTTP requests available in Postman's Collection Format v2.1:

{
  "info": {
    "_postman_id": "ddbb5536-b6ad-4247-a715-52a5d518b648",
    "name": "Google Apps - Load Testing",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "item": [
    {
      "name": "Get Google",
      "event": [
        {
          "listen": "test",
          "script": {
            "exec": [
              ""
            ],
            "type": "text/javascript"
          }
        }
      ],
      "request": {
        "method": "GET",
        "header": [],
        "url": {
          "raw": "https://www.google.com",
          "protocol": "https",
          "host": [
            "www",
            "google",
            "com"
          ]
        }
      },
      "response": []
    },
    {
      "name": "Get Youtube",
      "event": [
        {
          "listen": "test",
          "script": {
            "exec": [
              ""
            ],
            "type": "text/javascript"
          }
        }
      ],
      "request": {
        "method": "GET",
        "header": [],
        "url": {
          "raw": "https://www.youtube.com/",
          "protocol": "https",
          "host": [
            "www",
            "youtube",
            "com"
          ],
          "path": [
            ""
          ]
        }
      },
      "response": []
    },
    {
      "name": "Get Google Translate",
      "event": [
        {
          "listen": "test",
          "script": {
            "exec": [
              ""
            ],
            "type": "text/javascript"
          }
        }
      ],
      "request": {
        "method": "GET",
        "header": [],
        "url": {
          "raw": "https://translate.google.com/",
          "protocol": "https",
          "host": [
            "translate",
            "google",
            "com"
          ],
          "path": [
            ""
          ]
        }
      },
      "response": []
    }
  ]
}

We should use the “Raw text” option while importing the data:
Postman Import Collection
That's it! We just need to follow through with the import task by clicking on the Continue action, and we'll have our test collection ready within Postman.

3. Using Postman Collection Runner

In this section, we'll explore how we can use Postman's Collection Runner to execute the API requests in the “Google Apps – Load Testing” collection and perform basic load testing.

3.1. Basic Configuration

We can launch the Collection Runner with a right-click over the collection:

Postman Collection Runner

In the Runner mode, let's configure the run by specifying the order of execution, number of iterations, and delay between consecutive API hits:

Postman Runner Mode

Next, let's click on “Run Google Apps – Load Testing” to start the basic load testing of the API requests within the collection:

Postman Basic Load Testing

As the runner executes the API requests, we can see live results for each API hit spanned over multiple iterations.

3.2. Advanced Configuration Using Test Scripts

Using the Postman GUI, we were able to control the order of execution for the APIs. However, we can gain finer control over the execution flow by using the Test Scripts feature of Postman.

Let's say we want to include the “Google Translate” API in the workflow only if hits to “Google API” are returning with an HTTP 200 status code. Otherwise, we want to directly hit the “Youtube API”:

Postman Advanced Flow

We'll start by adding a simple conditional statement in the Tests section for the “Get Google” request:

if (pm.response.code == 200) {
    postman.setNextRequest("Get Google Translate");
}
else {
    postman.setNextRequest("Get Youtube");
}

Next, we'll set “Get Youtube” as the subsequent request to be executed after “Get Google Translate”:

postman.setNextRequest("Get Youtube");

Moreover, we know that “Get Youtube” is the last request in the flow, so we'll set the next request after it as null:

postman.setNextRequest(null);

Finally, let's see the complete collection with test scripts:

{
  "info": {
    "_postman_id": "ddbb5536-b6ad-4247-a715-52a5d518b648",
    "name": "Google Apps - Load Testing",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "item": [
    {
      "name": "Get Google",
      "event": [
        {
          "listen": "test",
          "script": {
            "exec": [
              "if (pm.response.code == 200) {",
              "  postman.setNextRequest(\"Get Google Translate\");",
              "}",
              "else {",
              "  postman.setNextRequest(\"Get Youtube\");",
              "}"
            ],
            "type": "text/javascript"
          }
        }
      ],
      "request": {
        "method": "GET",
        "header": [],
        "url": {
          "raw": "https://www.google.com",
          "protocol": "https",
          "host": [
            "www",
            "google",
            "com"
          ]
        }
      },
      "response": []
    },
    {
      "name": "Get Youtube",
      "event": [
        {
          "listen": "test",
          "script": {
            "exec": [
              "postman.setNextRequest(null);"
            ],
            "type": "text/javascript"
          }
        }
      ],
      "request": {
        "method": "GET",
        "header": [],
        "url": {
          "raw": "https://www.youtube.com/",
          "protocol": "https",
          "host": [
            "www",
            "youtube",
            "com"
          ],
          "path": [
            ""
          ]
        }
      },
      "response": []
    },
    {
      "name": "Get Google Translate",
      "event": [
        {
          "listen": "test",
          "script": {
            "exec": [
              "postman.setNextRequest(\"Get Youtube\");"
            ],
            "type": "text/javascript"
          }
        }
      ],
      "request": {
        "method": "GET",
        "header": [],
        "url": {
          "raw": "https://translate.google.com/",
          "protocol": "https",
          "host": [
            "translate",
            "google",
            "com"
          ],
          "path": [
            ""
          ]
        }
      },
      "response": []
    }
  ]
}

Like earlier, we can use the Collection Runner to execute this custom flow.

4. Using Newman Runner

We can use the Newman CLI utility to run a Postman collection through the command line. Taking this approach opens up wider opportunities for automation.

Let's use it to run two iterations of the custom flow for our existing collection:

newman run -n2 "Custom Flow Google Apps - Load Testing.postman_collection.json"

Once all the iterations are over, we'd get a statistics summary where we can see the average response time of the requests:
newman result summary

We must note that we're deliberately using lower values for our demonstration as most modern services have a rate-limiting and request-blocking logic that will start blocking our requests for higher values or duration.

5. Using Grafana K6

Postman is the easiest way to formulate the request collection and execution flow. However, while using Postman or Newman, we're invoking the requests one after the other sequentially.

In a practical scenario, we need to test our systems for requests that are coming from multiple users at the same time. For such a use case, we can use Grafana's k6 utility.

First, we need to convert our existing Postman collection to a k6 compatible format. We can use the postman-to-k6 library for this milestone:

postman-to-k6 "Google Apps - Load Testing.json" -o k6-script.js

Next, let's do a live run for three seconds with two virtual users:

k6 run --duration 3s --vus 2 k6-script.js

On completion, we get a detailed statistics report showing metrics such as average response time, number of iterations, and many others:
k6

6. Conclusion

In this tutorial, we leveraged Postman collections to do basic load testing using the GUI and the Newman runner. Additionally, we learned about the k6 utility that can be used to do advanced load testing of the requests in a Postman collection.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:

>> GET ACCESS NOW

Junit footer banner
Comments are closed on this article!