Home

Awesome

🧪 Load Testing Framework

Installation

Before running the tests locally, you must ensure you have k6 installed and have the necessary environmental variables set. Also, installing the required npm modules and compiling the typescript tests (.ts) into Javascript files (.js) under dist/ is necessary in order to make the tests runnable by k6.

The following steps help you achieve all these in one go.

Windows Setup

  1. Set your values for the environmental variables in local.ps1.

  2. Source the PowerShell script to set up the environment and dependencies:

    . .\scripts\setup\local.ps1
    

Unix-based OS Setup

  1. Set your values for the environmental variables in local.sh.

  2. Make the script executable and source it:

    chmod +x ./scripts/setup/local.sh && source ./scripts/setup/local.sh
    

Running the Tests

Execute the tests based on your environment:

Developing a New Test

Step 1: Set the Load

Define the load for the tests in load.js, using the filename as the key.

For load scenario options, refer to the k6 options documentation.

Step 2: Define Test Endpoints

Specify the endpoints to test in endpoints.js.

Step 3: Define Request Functions

Create request functions (e.g., POST, GET) for your endpoints under requests/.

Step 4: Mandatory Environmental Variables

See configure.js for mandatory environmental variables needed for test execution.

Step 5: Test Example

Here's a template for a typical load test:

// imports

// K6 Phase Options
export const options = {
  ext: {
    loadimpact: {
      // ...
    },
  },
  thresholds: {
    // ...
  },
  scenarios: {
    jsonPlaceholderPosts: {
      executor: "ramping-vus",
      gracefulStop: "30s",
      stages: load.jsonPlaceholder.getPosts,
      gracefulRampDown: "30s",
    },
  },
};

export function setup() {
  // ...
}

// Testcase
export default function () {
  group("Get all Posts", () => {
    jsonPlaceholderRequests.getPosts();
    sleep(1);
  });

  group("Create a Post", () => {
    const payload = {
      userId: 1,
      title: "Good post!",
      body: "This is a good post.",
    };
    jsonPlaceholderRequests.createPost(payload);
    sleep(1);
  });
}

Analyzing the Report

The test output will include various metrics:

Test output

To help you understand each of these metrics better:

Step 6: Cleanup (Optional)

For cleanup, use scripts in cleaners. These can be manually triggered or automatically in tearDown().