-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperformance.js
100 lines (91 loc) · 2.73 KB
/
performance.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import http from "k6/http";
import { check, sleep, group } from "k6";
import { htmlReport } from "https://raw.githubusercontent.com/benc-uk/k6-reporter/main/dist/bundle.js";
import { textSummary } from "https://jslib.k6.io/k6-summary/0.0.1/index.js";
const baseURL = "https://reqres.in";
export const options = {
vus: 1000,
iterations: 3500,
thresholds: {
http_req_duration: ["avg < 2000"], // Response API Max 2s
http_req_failed: ["rate < 0.01"], // 1% error rate
},
};
// Assertions
export function assertResponseCode(response, expectedStatusCode) {
check(response, {
[`Correct Status code is ${expectedStatusCode}`]: (res) => {
const body = JSON.parse(res.body);
return res.status === expectedStatusCode;
},
});
}
export function assertResponseBodyPOST(postRes, fieldName, expectedValue) {
check(postRes, {
[`Response body field '${fieldName}' same with '${expectedValue}'`]: (
res
) => {
const body = JSON.parse(res.body);
return body[fieldName] === expectedValue;
},
});
}
export function assertResponseBodyPUT(putRes, fieldName, expectedValue) {
check(putRes, {
[`Response body field '${fieldName}' same with '${expectedValue}'`]: (
res
) => {
const body = JSON.parse(res.body);
return body[fieldName] === expectedValue;
},
});
}
export default function(postRes, putRes) {
group("postScenario", function () {
// POST Request
const postPathUrl = "/api/users";
const postPayload = JSON.stringify({
name: "morpheus",
job: "leader",
});
const postParams = {
headers: {
"Content-Type": "application/json",
},
};
const postRes = http.post(
`${baseURL}${postPathUrl}`,
postPayload,
postParams
);
// Assertions
assertResponseCode(postRes, 201);
assertResponseBodyPOST(postRes, "name", "morpheus");
assertResponseBodyPOST(postRes, "job", "leader");
});
sleep(2);
group("putScenario", function () {
// PUT Request
const putPathUrl = "/api/users/2";
const putPayload = JSON.stringify({
name: "morpheus",
job: "zion resident",
});
const putParams = {
headers: {
"Content-Type": "application/json",
},
};
const putRes = http.put(`${baseURL}${putPathUrl}`, putPayload, putParams);
// Assertions
assertResponseCode(putRes, 200);
assertResponseBodyPUT(putRes, "name", "morpheus");
assertResponseBodyPUT(putRes, "job", "zion resident");
});
}
export function handleSummary(data) {
return {
"result.html": htmlReport(data),
stdout: textSummary(data, { indent: " ", enableColors: true }),
};
}