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

70 lines
1.9 KiB
JavaScript
Raw Normal View History

2025-11-07 07:44:23 +00:00
/**
* 認證功能負載測試
*
* 此測試用於 Pre-release 環境模擬正常負載下的認證功能
* 測試重點系統在正常負載下的性能和穩定性
*/
import { registerWithCredentials, loginWithCredentials, refreshToken } from '../../scenarios/apis/auth.js';
export const options = {
scenarios: {
load_auth: {
executor: 'ramping-vus',
startVUs: 0,
stages: [
{ duration: '30s', target: 10 }, // 30 秒內增加到 10 個 VU
{ duration: '1m', target: 10 }, // 維持 10 個 VU 1 分鐘
{ duration: '30s', target: 20 }, // 30 秒內增加到 20 個 VU
{ duration: '1m', target: 20 }, // 維持 20 個 VU 1 分鐘
{ duration: '30s', target: 0 }, // 30 秒內減少到 0 個 VU
],
gracefulRampDown: '30s',
tags: { test_type: 'load', api: 'auth', environment: 'pre' },
},
},
thresholds: {
checks: ['rate>0.95'], // 95% 的檢查必須通過
http_req_duration: ['p(95)<2000'], // 95% 的請求應在 2 秒內完成
http_req_failed: ['rate<0.05'], // 失敗率應低於 5%
},
};
export default function () {
const baseUrl = __ENV.BASE_URL || 'http://localhost:8888';
const timestamp = Date.now();
const randomId = Math.floor(Math.random() * 1000000);
const loginId = `load_test_${timestamp}_${randomId}@example.com`;
const password = 'LoadTest123!';
// 1. 註冊
const registerResult = registerWithCredentials({
baseUrl,
loginId,
password,
});
if (!registerResult.success) {
return;
}
// 2. 登入
const loginResult = loginWithCredentials({
baseUrl,
loginId,
password,
});
if (!loginResult.success || !loginResult.tokens) {
return;
}
// 3. 刷新 Token
refreshToken({
baseUrl,
accessToken: loginResult.tokens.accessToken,
refreshToken: loginResult.tokens.refreshToken,
});
}