-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload-test.js
More file actions
55 lines (43 loc) · 1.29 KB
/
load-test.js
File metadata and controls
55 lines (43 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import http from 'k6/http';
import { check, sleep } from 'k6';
const BASE_URL = 'http://127.0.0.1:3001/api/recommendation';
export const options = {
stages: [
{ duration: '30s', target: 10 },
{ duration: '1m', target: 10 },
{ duration: '30s', target: 0 },
],
thresholds: {
http_req_duration: ['p(95)<500'],
http_req_failed: ['rate<0.01'],
},
};
export default function () {
const payload = JSON.stringify({
fullName: 'Ivan Ferreira',
description: 'Teste de carga com k6',
stars: 5,
situation: 'Pendente',
status: true,
});
const headers = { 'Content-Type': 'application/json' };
const post = http.post(BASE_URL, payload, { headers });
const idMatch = post.body.match(/"_id"\s*:\s*"([^"]+)"/);
const id = idMatch ? idMatch[1] : null;
check(post, {
'POST status 201': (r) => r.status === 201,
'POST retornou ID': () => id !== null,
});
if (!id) return;
const get = http.get(`${BASE_URL}/${id}`);
check(get, {
'GET status 200': (r) => r.status === 200,
'GET retornou o registro': (r) => r.body.includes(id),
});
const del = http.del(`${BASE_URL}/${id}`);
console.log(`ID: ${id} | DELETE: ${del.status} | ${del.body}`);
check(del, {
'DELETE sucesso': (r) => r.status === 200 || r.status === 204,
});
sleep(1);
}