120 lines
3.8 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// stress-test.js
'use strict';
const https = require('https');
const FC_URL = 'https://get-sts-token-qltjykaafr.cn-shanghai.fcapp.run';
const TEST_STEAM_ID = '76561198000000000';
// 压测配置
const CONFIG = {
concurrency: 20, // 并发数
totalRequests: 100, // 总请求数
sameIp: true, // 模拟同一IP
sameSteamId: true // 模拟同一SteamID
};
const stats = {
success: 0,
rateLimited: 0,
forbidden: 0,
error: 0,
startTime: 0,
endTime: 0
};
function makeRequest(steamId) {
return new Promise((resolve) => {
const urlObj = new URL(FC_URL);
const body = JSON.stringify({ steamId });
const options = {
hostname: urlObj.hostname,
port: 443,
path: urlObj.pathname,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(body),
'User-Agent': 'StressTest/1.0'
}
};
const req = https.request(options, (res) => {
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => {
resolve({ statusCode: res.statusCode, body: data });
});
});
req.on('error', (err) => {
resolve({ statusCode: 0, body: err.message, error: true });
});
req.write(body);
req.end();
});
}
async function runBatch(batchSize, steamId) {
const promises = [];
for (let i = 0; i < batchSize; i++) {
const id = CONFIG.sameSteamId ? steamId : `7656119${String(Math.floor(Math.random() * 10000000000)).padStart(10, '0')}`;
promises.push(makeRequest(id));
}
return Promise.all(promises);
}
async function runStressTest() {
console.log('=== 开始压测 ===');
console.log(`目标: ${FC_URL}`);
console.log(`并发: ${CONFIG.concurrency}, 总请求: ${CONFIG.totalRequests}`);
console.log(`同一SteamID: ${CONFIG.sameSteamId}, 同一IP: ${CONFIG.sameIp}\n`);
stats.startTime = Date.now();
let completed = 0;
while (completed < CONFIG.totalRequests) {
const batchSize = Math.min(CONFIG.concurrency, CONFIG.totalRequests - completed);
const results = await runBatch(batchSize, TEST_STEAM_ID);
for (const result of results) {
completed++;
if (result.error) {
stats.error++;
} else if (result.statusCode === 200) {
stats.success++;
} else if (result.statusCode === 429) {
stats.rateLimited++;
} else if (result.statusCode === 403) {
stats.forbidden++;
} else {
stats.error++;
}
// 实时进度
process.stdout.write(`\r进度: ${completed}/${CONFIG.totalRequests} | 成功: ${stats.success} | 限流: ${stats.rateLimited} | 403: ${stats.forbidden} | 错误: ${stats.error}`);
}
}
stats.endTime = Date.now();
const duration = (stats.endTime - stats.startTime) / 1000;
console.log('\n\n=== 压测结果 ===');
console.log(`总耗时: ${duration.toFixed(2)}s`);
console.log(`QPS: ${(CONFIG.totalRequests / duration).toFixed(2)}`);
console.log(`成功 (200): ${stats.success}`);
console.log(`限流 (429): ${stats.rateLimited}`);
console.log(`禁止 (403): ${stats.forbidden}`);
console.log(`错误: ${stats.error}`);
console.log(`限流生效率: ${((stats.rateLimited / CONFIG.totalRequests) * 100).toFixed(1)}%`);
if (stats.rateLimited > 0) {
console.log('\n✅ 限流机制正常工作');
} else {
console.log('\n⚠ 未触发限流,可能配置过于宽松');
}
}
runStressTest().catch(console.error);