Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<test_name>.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/<test_name>.js
> ```
62 changes: 62 additions & 0 deletions src/test/k6/scripts/login-test.js
Original file line number Diff line number Diff line change
@@ -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);
}
43 changes: 43 additions & 0 deletions src/test/k6/scripts/stress-test.js
Original file line number Diff line number Diff line change
@@ -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);
}
Loading