-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadtesting.js
122 lines (100 loc) · 3.42 KB
/
loadtesting.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import http from 'k6/http';
import { check, group } from 'k6';
export let options = {
scenarios: {
user_tests: {
executor: 'constant-vus',
vus: 20,
duration: '15s',
exec: 'userScenario'
},
store_tests: {
executor: 'constant-vus',
vus: 100,
startTime: '15s',
duration: '1m',
exec: 'storeScenario'
},
},
};
export function setup() {
const user = {
"name": "k6user",
"email": "[email protected]",
"password": "k6user"
};
http.post('http://localhost:8080/users', JSON.stringify(user), {
headers: { 'Content-Type': 'application/json' },
});
let res = http.post('http://localhost:8080/users/login', JSON.stringify(user));
if (res.status !== 200) throw new Error('Auth failed');
let authToken = res.body;
return authToken;
}
export function userScenario() {
group('user', function () {
const randomName = getRandomString(getRandomInt(4, 15));
const user = {
"name": randomName,
"email": randomName + "@email.com",
"password": randomName
};
let resRegister = http.post('http://localhost:8080/users', JSON.stringify(user), {
headers: { 'Content-Type': 'application/json' },
});
check(resRegister, {
'register status was 201': (r) => r.status == 201,
});
let resLogin = http.post('http://localhost:8080/users/login', JSON.stringify(user));
check(resLogin, {
'login status was 200': (r) => r.status == 200,
});
});
}
export function storeScenario(authToken) {
group('products', function () {
let resListProducts = http.get('http://localhost:8080/products');
check(resListProducts, {
'list products status was 200': (r) => r.status == 200,
});
let products = JSON.parse(resListProducts.body);
let randomProduct = products[getRandomInt(0, products.length - 1)];
let resProductDetails = http.get(`http://localhost:8080/products/${randomProduct.id}`);
check(resProductDetails, {
'get product details status was 200': (r) => r.status == 200,
});
});
group('cart', function () {
// Add a random product to the cart
let headers = { 'Authorization': `Bearer ${authToken}` };
let resAddToCart = http.post('http://localhost:8080/cart/add/1', {}, { headers: headers });
check(resAddToCart, {
'add to cart status was 204': (r) => r.status == 204,
});
let resViewToCart = http.get('http://localhost:8080/cart', { headers: headers });
check(resViewToCart, {
'view cart status was 200': (r) => r.status === 200,
});
});
group('checkout', function () {
let headers = { 'Authorization': `Bearer ${authToken}` };
let checkoutRes = http.post('http://localhost:8080/cart/checkout', {}, { headers: headers });
check(checkoutRes, {
'checkout status was 200': (r) => r.status === 200,
});
});
}
// Helper functions
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function getRandomString(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
return result;
}