-
Notifications
You must be signed in to change notification settings - Fork 0
/
stress.js
298 lines (258 loc) ยท 9.39 KB
/
stress.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import http from 'k6/http';
import { check, sleep } from 'k6';
import stomp from 'k6/x/stomp';
const BASE_URL = 'http://localhost:8080';
export let options = {
vus: 500,
duration: '120s',
thresholds: {
http_req_failed: ['rate<0.01'],
http_req_duration: ['p(95)<300'],
},
};
// ์ ํธ ํจ์: ๋๋ค ๋ฐฐ์ด ์ ํ
function getRandomElement(array) {
return array[Math.floor(Math.random() * array.length)];
}
// API ํธ์ถ์ ์ฌ์ฉ๋ ํค๋ ์ธํ
function getHeaders(accessToken) {
return {
headers: {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/json',
},
};
}
// ๋ก๊ทธ์ธ ํ ์ก์ธ์ค ํ ํฐ ์ถ์ถ API
function loginAPI() {
const userId = __VU;
const email = `user${userId}@example.com`;
const nickname = `user${userId}`;
const payload = JSON.stringify({
profileNickname: nickname,
accountEmail: email,
profileImage: 'image_url',
socialId: 1,
});
const params = {
headers: { 'Content-Type': 'application/json' },
};
const res = http.post(`${BASE_URL}/api/v1/login/android/kakao`, payload, params);
const success = check(res, { 'login succeeded': (r) => r.status === 200 });
if (!success) {
console.error(`Login failed for user: ${email}`);
}
const jsonResponse = res.json();
return {
accessToken: jsonResponse.data.accessToken,
userCode: jsonResponse.data.userCode, // userCode ์ถ์ถ
};
}
// ๋ก๋น ๊ด๋ จ API ํธ์ถ
function favoriteAPI(accessToken) {
const params = getHeaders(accessToken);
const res = http.get(`${BASE_URL}/api/v1/favorite`, params);
const success = check(res, { 'favorite succeeded': (r) => r.status === 200 });
if (!success) {
console.error('Favorite API failed');
}
}
function groupPagingAPI(accessToken) {
const params = getHeaders(accessToken);
const res = http.get(`${BASE_URL}/api/v1/group/paging`, params);
const success = check(res, { 'group succeeded': (r) => r.status === 200 });
if (!success) {
console.error('Group paging API failed');
}
}
function todayScheduleAPI(accessToken) {
const params = getHeaders(accessToken);
const res = http.get(`${BASE_URL}/api/v1/common/schedule/today`, params);
const success = check(res, { 'todaySchedule succeeded': (r) => r.status === 200 });
if (!success) {
console.error('Today schedule API failed');
}
}
function invitationAPI(accessToken) {
const params = getHeaders(accessToken);
const res = http.get(`${BASE_URL}/api/v1/invitation`, params);
const success = check(res, { 'invite succeeded': (r) => r.status === 200 });
if (!success) {
console.error('Invitation API failed');
}
}
function notificationSchedulesAPI(accessToken) {
const params = getHeaders(accessToken);
const res = http.get(`${BASE_URL}/api/v1/notification/schedules`, params);
const success = check(res, { 'schedule succeeded': (r) => r.status === 200 });
if (!success) {
console.error('Notification schedules API failed');
}
}
// ๊ฐ์ธ ์ค์ผ์ค ๊ด๋ จ API
function fetchPersonalScheduleAPI(accessToken, startDate, endDate) {
const params = getHeaders(accessToken);
const res = http.get(`${BASE_URL}/api/v1/schedule/week/${startDate}/${endDate}`, params);
const success = check(res, { 'schedule fetched': (r) => r.status === 200 });
if (!success) {
console.error('Failed to fetch personal schedule');
}
}
function createPersonalScheduleAPI(accessToken, schedulePayload) {
const params = getHeaders(accessToken);
const res = http.post(`${BASE_URL}/api/v1/schedule`, schedulePayload, params);
const success = check(res, { 'schedule created': (r) => r.status === 200 });
if (!success) {
console.error('Failed to create personal schedule');
}
}
// ๊ทธ๋ฃน ์ค์ผ์ค ๊ด๋ จ API
function fetchGroupPagingAPI(accessToken) {
const params = getHeaders(accessToken);
const res = http.get(`${BASE_URL}/api/v1/group/paging`, params);
const success = check(res, { 'group succeeded': (r) => r.status === 200 });
if (!success) {
console.error('Failed to fetch group information');
}
return res.json().data[0].code;
}
function createGroupScheduleStomp(client, groupCode, title, content, scheduleDate, startTime, endTime) {
client.send(
`/pub/schedule/create/${groupCode}`,
'application/json',
JSON.stringify({
title: title,
content: content,
scheduleDate: scheduleDate,
startTime: startTime,
endTime: endTime,
})
);
}
function createGroupPlannerStomp(client, groupCode, title, content, scheduleDate, userCode, scheduleId) {
client.send(
`/pub/planner/create/${groupCode}`,
'application/json',
JSON.stringify({
title: title,
content: content,
deadline: scheduleDate,
status: 'TODO',
managerCode: userCode,
userCodes: [],
scheduleId: scheduleId,
})
);
}
function fetchGroupPlannerAPI(accessToken, groupCode, startDate, endDate) {
const params = getHeaders(accessToken);
const res = http.get(`${BASE_URL}/api/v1/group-rooms/planner/week/${groupCode}/${startDate}/${endDate}`, params);
const success = check(res, { 'planner fetched': (r) => r.status === 200 });
if (!success) {
console.error('Failed to fetch group planner');
}
}
function handleStompMessage(subscription) {
const msg = subscription.read();
if (!msg) {
console.error('No message received.');
return null;
}
console.log('Raw Message:', msg);
try {
const msgString = String.fromCharCode.apply(null, new Uint8Array(msg.body));
console.log('Decoded Message String:', msgString);
const msgJson = JSON.parse(msgString);
console.log('Decoded Message JSON:', msgJson);
if (msgJson && msgJson.data && msgJson.data.scheduleCommonResponse && msgJson.data.scheduleCommonResponse.id) {
console.log('Schedule ID:', msgJson.data.scheduleCommonResponse.id);
return msgJson.data.scheduleCommonResponse.id;
} else {
console.error('Expected property not found in message:', msgJson);
}
} catch (error) {
console.error('Error decoding or parsing message:', error);
}
return null;
}
// STOMP ๋ฉ์์ง ์ฒ๋ฆฌ ํธ๋ค๋ฌ (๋ก๊ทธ ์ถ๊ฐ)
function connectStompAndHandleMessages(accessToken, userCode, groupCode) {
const stompOptions = {
addr: 'localhost:8080',
protocol: 'ws',
path: '/api/v1/ws',
headers: {
Authorization: `Bearer ${accessToken}`,
groupCode: groupCode,
},
timeout: '5s',
error: function (err) {
console.error('STOMP Error:', err);
},
};
const client = stomp.connect(stompOptions);
const subscription = client.subscribe(`/sub/schedule/${groupCode}`);
try {
// ๊ทธ๋ฃน ์ค์ผ์ค ์์ฑ ๋ฉ์์ง ์ ์ก
['2024-09-20', '2024-09-27', '2024-09-28'].forEach((date, index) => {
createGroupScheduleStomp(
client,
groupCode,
`Test Event ${index + 1}`,
'Test Content',
date,
Math.min(1 + index, 9),
Math.min(10 + index, 23)
);
});
// ๋ฉ์์ง ์์ ๋ฐ ์ค์ผ์ค ID ์ถ์ถ
const scheduleId = handleStompMessage(subscription);
if (!scheduleId) {
console.error('Failed to handle STOMP message');
return;
}
console.log('Schedule ID received:', scheduleId);
// ๊ทธ๋ฃน ํ๋๋ ์์ฑ ๋ฉ์์ง ์ ์ก
['2024-10-20T00:00:00', '2024-10-07T00:00:00', '2024-09-28T00:00:00'].forEach((date, index) => {
createGroupPlannerStomp(
client,
groupCode,
`Test Event ${index + 1}`,
'Test Content',
date,
userCode, // ๋ก๊ทธ์ธํ ์ ์ ์ userCode ์ ๋ฌ
scheduleId // ํ๋๋ ์์ฑ ์ scheduleId ์ฌ์ฉ
);
});
} catch (error) {
console.error('Error during STOMP processing:', error);
}
}
// ํ
์คํธ ์๋๋ฆฌ์ค ์คํ
export default function () {
const { accessToken, userCode } = loginAPI(); // userCode๋ ํจ๊ป ์ถ์ถ
// ๋ก๋น ๊ด๋ จ API ํธ์ถ
favoriteAPI(accessToken);
groupPagingAPI(accessToken);
todayScheduleAPI(accessToken);
invitationAPI(accessToken);
notificationSchedulesAPI(accessToken);
sleep(1);
// ๊ฐ์ธ ์ค์ผ์ค ๊ด๋ จ API ํธ์ถ
const startDate = getRandomElement(['2024-08-01', '2024-08-02', '2024-08-03']);
const endDate = getRandomElement(['2024-09-01', '2024-09-02', '2024-09-03']);
fetchPersonalScheduleAPI(accessToken, startDate, endDate);
const schedulePayload = JSON.stringify({
title: 'New Schedule',
content: 'Team meeting',
scheduleDate: '2024-09-28',
startTime: '10',
endTime: '12',
});
createPersonalScheduleAPI(accessToken, schedulePayload);
sleep(3);
// ๊ทธ๋ฃน ์ค์ผ์ค ๊ด๋ จ API ํธ์ถ
const groupCode = fetchGroupPagingAPI(accessToken);
connectStompAndHandleMessages(accessToken, userCode, groupCode);
fetchGroupPlannerAPI(accessToken, groupCode, startDate, endDate);
}