-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_test.js
More file actions
545 lines (260 loc) · 9.02 KB
/
Copy pathhttp_test.js
File metadata and controls
545 lines (260 loc) · 9.02 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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
import http from 'k6/http';
import { check, sleep } from 'k6';
import { randomString } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js';
// --- 测试配置 ---
export const options = {
// 定义虚拟用户(VU)和测试时长
stages: [
{ duration: '20s', target: 10 }, // 20秒内,从0 ramp-up 到 10个虚拟用户
{ duration: '1m', target: 10 }, // 维持10个虚拟用户运行1分钟
{ duration: '20s', target: 30 }, // 20秒内增加到30个虚拟用户
{ duration: '1m', target: 30 }, // 维持30个虚拟用户运行1分钟
{ duration: '15s', target: 0 }, // 15秒内 ramp-down 到 0
],
// 定义全局阈值,测试不达标时会失败
thresholds: {
'http_req_duration': ['p(95)<1000'], // 95%的请求响应时间必须低于1秒
'http_req_failed': ['rate<0.05'], // 请求失败率必须低于5%
'http_req_duration{name:GetRooms}': ['p(95)<500'], // 获取房间列表响应时间
'http_req_duration{name:GetMe}': ['p(95)<300'], // 获取用户信息响应时间
'http_req_duration{name:CreateRoom}': ['p(95)<800'], // 创建房间响应时间
'http_req_duration{name:JoinRoom}': ['p(95)<500'], // 加入房间响应时间
},
};
// --- 准备工作 (在测试开始前运行一次) ---
export function setup() {
console.log('🚀 开始性能测试准备工作...');
// 创建一个基础房间供测试使用
const baseUser = {
username: `base_user_${randomString(8)}`,
password: 'password123'
};
// 注册基础用户
const registerRes = http.post(`${__ENV.BASE_URL}/api/v1/auth/register`,
JSON.stringify(baseUser),
{ headers: { 'Content-Type': 'application/json' } }
);
if (registerRes.status !== 201) {
throw new Error(`无法注册基础用户: ${registerRes.status} - ${registerRes.body}`);
}
const baseToken = registerRes.json('data.token');
// 创建一个测试房间
const roomData = {
name: `PerfTest_Room_${randomString(6)}`,
description: 'k6性能测试专用房间'
};
const roomRes = http.post(`${__ENV.BASE_URL}/api/v1/rooms`,
JSON.stringify(roomData),
{ headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${baseToken}`
}}
);
if (roomRes.status !== 201) {
throw new Error(`无法创建测试房间: ${roomRes.status} - ${roomRes.body}`);
}
const roomId = roomRes.json('data.id');
console.log(`✅ 准备工作完成,创建了测试房间: ${roomId}`);
return {
baseToken: baseToken,
testRoomId: roomId,
baseUser: baseUser
};
}
// --- 虚拟用户执行的脚本 ---
export default function (setupData) {
const vuId = __VU; // 虚拟用户ID
const iterationId = __ITER; // 当前迭代ID
// 每个虚拟用户注册自己的账号(除了VU 1使用基础用户)
let token, userId;
if (vuId === 1) {
// VU 1 使用基础用户
token = setupData.baseToken;
userId = 'base_user';
} else {
// 其他VU注册新用户
const userData = {
username: `perf_user_${vuId}_${iterationId}_${randomString(4)}`,
password: 'password123'
};
const registerRes = http.post(`${__ENV.BASE_URL}/api/v1/auth/register`,
JSON.stringify(userData),
{
headers: { 'Content-Type': 'application/json' },
tags: { name: 'Register' }
}
);
if (!check(registerRes, {
'Register: status is 201': (r) => r.status === 201,
'Register: has token': (r) => r.json('data.token') !== undefined,
})) {
// 如果注册失败,尝试登录(可能用户已存在)
const loginRes = http.post(`${__ENV.BASE_URL}/api/v1/auth/login`,
JSON.stringify(userData),
{
headers: { 'Content-Type': 'application/json' },
tags: { name: 'Login' }
}
);
if (loginRes.status === 200) {
token = loginRes.json('data.token');
userId = loginRes.json('data.id');
} else {
console.error(`VU ${vuId}: 注册和登录都失败`);
return;
}
} else {
token = registerRes.json('data.token');
userId = registerRes.json('data.id');
}
}
const headers = {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
};
// 模拟真实用户行为场景
performUserScenario(headers, setupData, vuId);
// 随机等待时间,模拟真实用户行为
sleep(Math.random() * 2 + 1); // 1-3秒随机等待
}
function performUserScenario(headers, setupData, vuId) {
// 场景1: 获取房间列表
const roomsRes = http.get(`${__ENV.BASE_URL}/api/v1/rooms`, {
headers: headers,
tags: { name: 'GetRooms' },
});
check(roomsRes, {
'GetRooms: status is 200': (r) => r.status === 200,
'GetRooms: has rooms data': (r) => r.json('data.rooms') !== undefined,
});
// 场景2: 获取自己的用户信息
const meRes = http.get(`${__ENV.BASE_URL}/api/v1/users/me`, {
headers: headers,
tags: { name: 'GetMe' },
});
check(meRes, {
'GetMe: status is 200': (r) => r.status === 200,
'GetMe: has user data': (r) => r.json('data.user') !== undefined,
});
// 场景3: 随机决定是否创建新房间(30%概率)
if (Math.random() < 0.3) {
const roomData = {
name: `Room_VU${vuId}_${randomString(4)}`,
description: `由VU${vuId}创建的测试房间`
};
const createRoomRes = http.post(`${__ENV.BASE_URL}/api/v1/rooms`,
JSON.stringify(roomData),
{
headers: headers,
tags: { name: 'CreateRoom' }
}
);
check(createRoomRes, {
'CreateRoom: status is 201': (r) => r.status === 201,
'CreateRoom: has room id': (r) => r.json('data.id') !== undefined,
});
if (createRoomRes.status === 201) {
const newRoomId = createRoomRes.json('data.id');
// 短暂等待后尝试更新房间描述
sleep(0.5);
const updateData = { description: `更新的描述 - ${new Date().toISOString()}` };
const updateRes = http.patch(`${__ENV.BASE_URL}/api/v1/rooms/${newRoomId}`,
JSON.stringify(updateData),
{
headers: headers,
tags: { name: 'UpdateRoom' }
}
);
check(updateRes, {
'UpdateRoom: status is 200': (r) => r.status === 200,
});
}
}
// 场景4: 加入现有测试房间(如果存在)
if (setupData.testRoomId && Math.random() < 0.6) {
const joinData = { room_id: setupData.testRoomId };
const joinRes = http.post(`${__ENV.BASE_URL}/api/v1/rooms/join`,
JSON.stringify(joinData),
{
headers: headers,
tags: { name: 'JoinRoom' }
}
);
check(joinRes, {
'JoinRoom: status is 200 or 409': (r) => r.status === 200 || r.status === 409, // 409表示已经是成员
});
// 如果加入成功,获取已加入的房间列表
if (joinRes.status === 200) {
sleep(0.2);
const joinedRoomsRes = http.get(`${__ENV.BASE_URL}/api/v1/rooms/joined`, {
headers: headers,
tags: { name: 'GetJoinedRooms' },
});
check(joinedRoomsRes, {
'GetJoinedRooms: status is 200': (r) => r.status === 200,
});
}
}
// 场景5: 获取用户列表(分页测试)
if (Math.random() < 0.4) {
const usersRes = http.get(`${__ENV.BASE_URL}/api/v1/users?limit=10&offset=0`, {
headers: headers,
tags: { name: 'GetUsers' },
});
check(usersRes, {
'GetUsers: status is 200': (r) => r.status === 200,
'GetUsers: has pagination': (r) => {
const data = r.json('data');
return data && data.hasOwnProperty('total') && data.hasOwnProperty('limit');
},
});
}
}
// --- 测试结束后清理工作 ---
export function teardown(setupData) {
console.log('🧹 开始清理测试数据...');
if (setupData && setupData.testRoomId && setupData.baseToken) {
// 删除测试房间
const deleteRes = http.del(`${__ENV.BASE_URL}/api/v1/rooms/${setupData.testRoomId}`, null, {
headers: {
'Authorization': `Bearer ${setupData.baseToken}`
}
});
if (deleteRes.status === 200) {
console.log('✅ 测试房间已清理');
} else {
console.log(`⚠️ 清理测试房间失败: ${deleteRes.status}`);
}
}
console.log('📊 性能测试完成!');
}