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
npm
installed. -
Install the necessary packages:
npm install
This will install the required TypeScript and
k6
packages for development. -
Install
k6
globally if you haven't already:npm install -g k6
-
Run the complete build and test process:
npm test
This command will:
- Compile TypeScript files.
- Copy
payloads
andheaders
directories todist
. - Run the
k6
script with the environment variables specified in the.env
file.
-
Alternatively, you can run individual steps:
-
Compile TypeScript:
npm run build
-
Copy
payloads
andheaders
directories:npm run postbuild
-
Run the
k6
script: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 toPOST
if not specified.
- Ensure that the
payloads
andheaders
folders exist in thesrc
directory and contain the appropriate files as specified in the.env
file. - The
payloads
folder should contain JSON files with request payloads. - The
headers
folder should contain JSON files with custom headers. - The
HTTP_METHOD
variable allows you to specify the request method dynamically.
Feel free to customize the .env
file according to your testing needs.