-
Notifications
You must be signed in to change notification settings - Fork 0
/
payments.test.js
100 lines (86 loc) · 3.3 KB
/
payments.test.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 } from 'k6';
import { randomString, randomItem } from 'https://jslib.k6.io/k6-utils/1.4.0/index.js';
const SLEEP_DURATION = 0.01;
const BASE_URL = __ENV.BASE_URL ||'http://localhost:8080';
const TARGET_USERS = __ENV.TARGET_USERS || 5
const params = {
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic am9objpqb2hu'
},
};
export const options = {
stages: [
{ duration: `5s`, target: TARGET_USERS }, // simulate ramp-up of traffic from 1 to 5 users over 10 seconds
{ duration: `10s`, target: TARGET_USERS }, // stay at 5 users for 20 seconds
{ duration: `5s`, target: 0 }, // ramp-down to 0 users
],
thresholds: {
http_req_duration: ['p(90)<1000'], // 90% of requests must complete below 1s,
http_req_failed: ['rate<0.03'], // http errors should be less than 3%
},
};
function getCall(path) {
const response = http.get(`${BASE_URL}/${path}`, params);
check(response, {
'status code is 200': (r) => [200, 304].includes(r.status),
});
if (response.status === 304) {
console.info(`Call to '${path}' returned status 304`);
return null;
} else if (response.status !== 200) {
console.error(`Call to '${path}' is failed with status ${response.status} and error code ${response.error_code}: ${response.error}`);
return null;
}
sleep(SLEEP_DURATION);
return response.json();
}
export function setup() {
const maxRetries = 20;
const retryInterval = 3;
let retries = 0;
while (retries < maxRetries) {
// Use the check function to make an HTTP request to the host
const res = http.get(`${BASE_URL}`);
if (res.status === 200) {
console.log(`Host is available after ${retries + 1} retries.`);
break;
}
retries++;
console.log(`Host not available. Retrying in ${retryInterval} seconds...`);
sleep(retryInterval);
}
}
export default function () {
// create payment with dynamic data
let name = randomString(10);
let createPaymentResp = http.post(`${BASE_URL}/payments`, JSON.stringify({
amount: 11.23,
currency: randomItem(["USD", "EUR", "GBP"]),
name: name,
}), params);
if (createPaymentResp.status !== 201) {
console.error(`Creation for ${name} is failed with status ${createPaymentResp.status} and error code ${createPaymentResp.error_code}`)
}
check(createPaymentResp, {
'create payment status code is 201': (r) => r.status === 201
});
sleep(SLEEP_DURATION);
// get created payment
let getPaymentResp = getCall(`payments/${createPaymentResp.json()['id']}`);
check(getPaymentResp, {
'payment has correct name': (r) => r['name'] === createPaymentResp.json()['name'],
});
// get all payments
let allPaymentsResp = getCall('payments');
check(allPaymentsResp, {
'payments has created payment': (r) => r.map(p => p['id']).includes(createPaymentResp.json()['id'])
});
// delete payment
let deletePaymentResp = http.del(`${BASE_URL}/payments/${createPaymentResp.json()['id']}`, null, params);
check(deletePaymentResp, {
'delete payment returns status 204': (r) => r.status === 204
});
sleep(SLEEP_DURATION);
}