/** * 使用者功能負載測試 * * 此測試用於 Pre-release 環境,模擬正常負載下的使用者功能。 * 測試重點:系統在正常負載下的性能和穩定性。 */ import { registerWithCredentials } from '../../scenarios/apis/auth.js'; import { getUserInfo, updateUserInfo } from '../../scenarios/apis/user.js'; export const options = { scenarios: { load_user: { 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: 'user', 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 || 'https://localhost:8888'; const timestamp = Date.now(); const randomId = Math.floor(Math.random() * 1000000); const loginId = `load_user_${timestamp}_${randomId}@example.com`; const password = 'LoadTest123!'; // 1. 註冊並獲取 Token const registerResult = registerWithCredentials({ baseUrl, loginId, password, }); if (!registerResult.success || !registerResult.tokens) { return; } const accessToken = registerResult.tokens.accessToken; // 2. 取得使用者資訊 const getInfoResult = getUserInfo({ baseUrl, accessToken, }); if (!getInfoResult.success) { return; } // 3. 更新使用者資訊 updateUserInfo({ baseUrl, accessToken, updateData: { nickname: `LoadTest_${timestamp}_${randomId}`, preferred_language: 'zh-tw', }, }); }