Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

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.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.