This repository contains a k6 load testing script written in TypeScript, designed to dynamically handle payloads, headers, and HTTP methods using environment variables.
- NodeJS v22.5.1
To clone this repository, use the following command:
git clone https://github.com/abas/k6-loadtester.git
cd k6-loadtester-
Ensure you have Node.js and
npminstalled. -
Install the necessary packages:
npm install
This will install the required TypeScript and
k6packages for development. -
Install
k6globally if you haven't already:npm install -g k6
-
Run the complete build and test process:
npm testThis command will:
- Compile TypeScript files.
- Copy
payloadsandheadersdirectories todist. - Run the
k6script with the environment variables specified in the.envfile.
-
Alternatively, you can run individual steps:
-
Compile TypeScript:
npm run build
-
Copy
payloadsandheadersdirectories:npm run postbuild
-
Run the
k6script:npm run run:test
-
The .env file should be located in the root directory of the repository and contains environment-specific variables. Here's an example .env file:
TARGET_URL=https://example.com/api/endpoint
ITERATIONS=10
TARGET_PAYLOAD=./payloads/payload.1.json
TARGET_HEADERS=./headers/headers.json
HTTP_METHOD=POST
TARGET_URL: The URL to which the request will be sent.ITERATIONS: The number of times the request will be sent.TARGET_PAYLOAD: The path to the JSON file containing the request payload.TARGET_HEADERS: The path to the JSON file containing the custom headers.HTTP_METHOD: The HTTP method to use for the request. Supported values arePOST,GET,PUT, andDELETE. Defaults toPOSTif not specified.
- Ensure that the
payloadsandheadersfolders exist in thesrcdirectory and contain the appropriate files as specified in the.envfile. - The
payloadsfolder should contain JSON files with request payloads. - The
headersfolder should contain JSON files with custom headers. - The
HTTP_METHODvariable allows you to specify the request method dynamically.
Feel free to customize the .env file according to your testing needs.