backend/test/tests/pre/stress-auth-test.js

59 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

2025-11-07 07:44:23 +00:00
/**
* 認證功能壓力測試
*
* 此測試用於 Pre-release 環境模擬高負載下的認證功能
* 測試重點系統在高負載下的穩定性和錯誤處理
*/
import { registerWithCredentials, loginWithCredentials } from '../../scenarios/apis/auth.js';
export const options = {
scenarios: {
stress_auth: {
executor: 'ramping-vus',
startVUs: 0,
stages: [
{ duration: '1m', target: 50 }, // 1 分鐘內增加到 50 個 VU
{ duration: '2m', target: 50 }, // 維持 50 個 VU 2 分鐘
{ duration: '1m', target: 100 }, // 1 分鐘內增加到 100 個 VU
{ duration: '2m', target: 100 }, // 維持 100 個 VU 2 分鐘
{ duration: '1m', target: 0 }, // 1 分鐘內減少到 0 個 VU
],
gracefulRampDown: '30s',
tags: { test_type: 'stress', api: 'auth', environment: 'pre' },
},
},
thresholds: {
checks: ['rate>0.90'], // 90% 的檢查必須通過(壓力測試允許較低成功率)
http_req_duration: ['p(95)<3000'], // 95% 的請求應在 3 秒內完成
http_req_failed: ['rate<0.10'], // 失敗率應低於 10%
},
};
export default function () {
const baseUrl = __ENV.BASE_URL || 'https://localhost:8888';
const timestamp = Date.now();
const randomId = Math.floor(Math.random() * 1000000);
const loginId = `stress_test_${timestamp}_${randomId}@example.com`;
const password = 'StressTest123!';
// 1. 註冊
const registerResult = registerWithCredentials({
baseUrl,
loginId,
password,
});
if (!registerResult.success) {
return;
}
// 2. 登入
loginWithCredentials({
baseUrl,
loginId,
password,
});
}