77 lines
2.1 KiB
JavaScript
77 lines
2.1 KiB
JavaScript
|
|
/**
|
|||
|
|
* 使用者功能夜間測試
|
|||
|
|
*
|
|||
|
|
* 此測試用於 Production 環境,在夜間低峰時段執行。
|
|||
|
|
* 測試重點:監控生產環境的使用者功能穩定性,識別長期性能變化。
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
import { loginWithCredentials } from '../../scenarios/apis/auth.js';
|
|||
|
|
import { getUserInfo, updateUserInfo } from '../../scenarios/apis/user.js';
|
|||
|
|
|
|||
|
|
export const options = {
|
|||
|
|
scenarios: {
|
|||
|
|
nightly_user: {
|
|||
|
|
executor: 'constant-vus',
|
|||
|
|
vus: 5, // 低並發,避免影響生產環境
|
|||
|
|
duration: '5m', // 執行 5 分鐘
|
|||
|
|
tags: { test_type: 'nightly', api: 'user', environment: 'prod' },
|
|||
|
|
},
|
|||
|
|
},
|
|||
|
|
thresholds: {
|
|||
|
|
checks: ['rate>0.98'], // 98% 的檢查必須通過(生產環境要求更高)
|
|||
|
|
http_req_duration: ['p(95)<2000'], // 95% 的請求應在 2 秒內完成
|
|||
|
|
http_req_failed: ['rate<0.02'], // 失敗率應低於 2%
|
|||
|
|
},
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
export default function () {
|
|||
|
|
const baseUrl = __ENV.BASE_URL || 'https://localhost:8888';
|
|||
|
|
|
|||
|
|
// 注意:生產環境測試應使用預先創建的測試帳號
|
|||
|
|
// 不要創建新帳號,避免污染生產資料
|
|||
|
|
const loginId = __ENV.TEST_LOGIN_ID || 'test@example.com';
|
|||
|
|
const password = __ENV.TEST_PASSWORD || 'TestPassword123!';
|
|||
|
|
|
|||
|
|
// 1. 登入(使用預先創建的測試帳號)
|
|||
|
|
const loginResult = loginWithCredentials({
|
|||
|
|
baseUrl,
|
|||
|
|
loginId,
|
|||
|
|
password,
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
if (!loginResult.success || !loginResult.tokens) {
|
|||
|
|
console.error('Nightly test failed: Login failed');
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const accessToken = loginResult.tokens.accessToken;
|
|||
|
|
|
|||
|
|
// 2. 取得使用者資訊
|
|||
|
|
const getInfoResult = getUserInfo({
|
|||
|
|
baseUrl,
|
|||
|
|
accessToken,
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
if (!getInfoResult.success) {
|
|||
|
|
console.error('Nightly test failed: Get user info failed');
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 3. 更新使用者資訊(僅更新非關鍵欄位,避免影響測試帳號)
|
|||
|
|
const updateInfoResult = updateUserInfo({
|
|||
|
|
baseUrl,
|
|||
|
|
accessToken,
|
|||
|
|
updateData: {
|
|||
|
|
nickname: `NightlyTest_${Date.now()}`,
|
|||
|
|
},
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
if (!updateInfoResult.success) {
|
|||
|
|
console.error('Nightly test failed: Update user info failed');
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
console.log('Nightly test passed: User operations succeeded');
|
|||
|
|
}
|
|||
|
|
|