From 525c1fa0fad0f3fdf750ef6a6225e745f2e06ad3 Mon Sep 17 00:00:00 2001 From: FranInfanti Date: Sun, 31 May 2026 12:12:56 -0300 Subject: [PATCH] test(k6): add new stress tests using k6 --- README.md | 17 ++++++++ src/test/k6/scripts/login-test.js | 62 ++++++++++++++++++++++++++++++ src/test/k6/scripts/stress-test.js | 43 +++++++++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 src/test/k6/scripts/login-test.js create mode 100644 src/test/k6/scripts/stress-test.js diff --git a/README.md b/README.md index 2d7adf5..f7522de 100644 --- a/README.md +++ b/README.md @@ -67,3 +67,20 @@ cp .env.example .env # The flag -v is optional docker-compose down -v ``` +--- + +## k6 Testing + +To run a k6 script tests, first you should run the whole project (see [**Running the Project**](#running-the-project)). + +Then run the following command: + +```bash +docker run --rm --network bazaar-network -v "$(pwd)/src/test/k6:/io" -w /io grafana/k6 run scripts/.js +``` + +> [!NOTE] +> If you are using Windows Powershell, you should replace `$(pwd)` with `"$($pwd.Path)"`. +> ```bash +> docker run --rm --network bazaar-network -v "$($pwd.Path)/src/test/k6:/io" -w /io grafana/k6 run scripts/.js +> ``` \ No newline at end of file diff --git a/src/test/k6/scripts/login-test.js b/src/test/k6/scripts/login-test.js new file mode 100644 index 0000000..5087e99 --- /dev/null +++ b/src/test/k6/scripts/login-test.js @@ -0,0 +1,62 @@ +import http from 'k6/http'; +import { check, sleep } from 'k6'; + +export const options = { + stages: [ + { duration: '30s', target: 20 }, + { duration: '2m', target: 50 }, + { duration: '30s', target: 0 }, + ], + thresholds: { + http_req_failed: ['rate<0.05'], + http_req_duration: ['p(95)<2000'], + }, +}; + +const PORT = '8080'; +const BASE_URL = `http://user-service:${PORT}/auth`; + +export function setup() { + const userPool = []; + + for (let i = 0; i < 50; i++) { + const uniqueId = `user_${i}_${Math.floor(Math.random() * 10000)}`; + const email = `${uniqueId}@bazaar.com`; + const password = `SecurePass123!_${i}`; + + const payload = JSON.stringify({ + name: `Tester-${i}`, + surname: `Bot`, + email: email, + password: password + }); + + const params = { headers: { 'Content-Type': 'application/json' } }; + http.post(`${BASE_URL}/register`, payload, params); + + userPool.push({ email: email, password: password }); + } + + return { users: userPool }; +} + +export default function (data) { + const user = data.users[(__VU - 1) % data.users.length]; + + const payload = JSON.stringify({ + email: user.email, + password: user.password + }); + + const params = { + headers: { + 'Content-Type': 'application/json', + 'X-Login-Source': 'OTHER' + }, + }; + + const res = http.post(`${BASE_URL}/login`, payload, params); + + check(res, { 'successful login (200)': (r) => r.status === 200 }); + sleep(Math.random() * 0.5 + 0.5); +} \ No newline at end of file diff --git a/src/test/k6/scripts/stress-test.js b/src/test/k6/scripts/stress-test.js new file mode 100644 index 0000000..7541182 --- /dev/null +++ b/src/test/k6/scripts/stress-test.js @@ -0,0 +1,43 @@ +import http from 'k6/http'; +import { check, sleep } from 'k6'; +import { uuidv4 } from 'https://jslib.k6.io/k6-utils/1.4.0/index.js'; + +export const options = { + stages: [ + { duration: '30s', target: 30 }, + { duration: '2m', target: 100 }, + { duration: '30s', target: 0 }, + ], + thresholds: { + http_req_failed: ['rate<0.01'], + http_req_duration: ['p(95)<1500'], + }, +}; + +const PORT = '8080'; +const URL = `http://user-service:${PORT}/auth/register`; + +export default function () { + const uniqueId = uuidv4().substring(0, 8); + + const payload = JSON.stringify({ + name: `User-${uniqueId}`, + surname: `Tester-${__VU}`, + email: `test-${uniqueId}@bazaar.com`, + password: `SecurePass123!_${uniqueId}` + }); + + const params = { + headers: { + 'Content-Type': 'application/json', + }, + }; + + const res = http.post(URL, payload, params); + + check(res, { + 'successful register (201)': (r) => r.status === 201, + }); + + sleep(Math.random() * 0.3 + 0.1); +} \ No newline at end of file