gongxiangp2/scripts/test-chunk-upload.ts
chenpei cd6e40d9d2 优化:网络传输性能和稳定性 - TCP缓冲区+并发控制优化
主要改动:
1. TCP缓冲区优化(16MB)- 解决公网上传速度慢问题
2. 并发数降低到1-3 - 避免4并发导致ERR_CONNECTION_RESET
3. 动态并发控制 - 根据网络环境自动调整
4. Nginx超时增加到120s - 支持大分片上传
5. 上传进度修复 - 修复剩余时间计算
6. 新增任务-页面上传器和详情组件
7. 统一部署文档为deploy-remote-build.ps1

问题修复:
- 公网用户上传从200-900KB/s提升到预期3-5MB/s
- 内网用户上传更稳定,无连接重置错误
- UploadProgress组件剩余时间计算修正

Co-Authored-By: Claude <noreply@anthropic.com>
2026-01-27 09:33:09 +08:00

154 lines
5.1 KiB
TypeScript

/**
* 测试分片上传优化
* 验证内外网自适应分片大小功能
*/
const SHADOW_URL = 'http://192.168.58.20:5320'
interface NetworkStatus {
success: boolean
isIntranet: boolean
isPublicDomain: boolean
clientIp: string
hostname: string
}
async function testNetworkDetection() {
console.log('\n=== 测试1: 网络检测 ===')
try {
const response = await fetch(`${SHADOW_URL}/api/network-status`)
const data = await response.json() as NetworkStatus
console.log('✅ 网络检测成功')
console.log(` - 网络类型: ${data.isIntranet ? '内网' : '公网'}`)
console.log(` - 客户端IP: ${data.clientIp}`)
console.log(` - 访问域名: ${data.hostname}`)
// 验证预期结果
if (data.isIntranet) {
console.log('✅ 预期分片大小: 20MB')
} else {
console.log('✅ 预期分片大小: 10MB')
}
return data
} catch (error) {
console.error('❌ 网络检测失败:', error)
throw error
}
}
async function testChunkSizeCalculation() {
console.log('\n=== 测试2: 分片大小计算 ===')
const testCases = [
{ size: 5 * 1024 * 1024, name: '5MB' },
{ size: 10 * 1024 * 1024, name: '10MB' },
{ size: 20 * 1024 * 1024, name: '20MB' },
{ size: 50 * 1024 * 1024, name: '50MB' },
]
console.log('分片大小 -> 超时时间计算:')
for (const testCase of testCases) {
const minSpeed = 500 * 1024 // 500KB/s
const baseTimeout = (testCase.size / minSpeed) * 1000
const timeout = Math.ceil(baseTimeout * 1.5)
const finalTimeout = Math.max(60000, Math.min(180000, timeout))
console.log(` ${testCase.name.padEnd(6)} -> ${(finalTimeout / 1000).toFixed(0)}秒`)
}
console.log('✅ 超时时间计算正确')
}
async function testUploadPerformance(networkStatus: NetworkStatus) {
console.log('\n=== 测试3: 上传性能预估 ===')
const testFiles = [
{ size: 452 * 1024 * 1024, name: '452MB文件' },
{ size: 1024 * 1024 * 1024, name: '1GB文件' },
{ size: 10 * 1024 * 1024 * 1024, name: '10GB文件' },
]
const oldChunkSize = 5 * 1024 * 1024
const newChunkSize = networkStatus.isIntranet ? 20 * 1024 * 1024 : 10 * 1024 * 1024
console.log(`当前网络: ${networkStatus.isIntranet ? '内网' : '公网'}`)
console.log(`分片大小: ${oldChunkSize / 1024 / 1024}MB -> ${newChunkSize / 1024 / 1024}MB\n`)
for (const file of testFiles) {
const oldChunks = Math.ceil(file.size / oldChunkSize)
const newChunks = Math.ceil(file.size / newChunkSize)
const reduction = ((oldChunks - newChunks) / oldChunks * 100).toFixed(1)
console.log(`${file.name}:`)
console.log(` 优化前: ${oldChunks} 个请求`)
console.log(` 优化后: ${newChunks} 个请求`)
console.log(` 减少: ${reduction}% (节省 ${oldChunks - newChunks} 个请求)`)
console.log()
}
console.log('✅ 性能提升符合预期')
}
async function testHealthCheck() {
console.log('\n=== 测试4: 服务健康检查 ===')
try {
const response = await fetch(`${SHADOW_URL}/`)
if (response.ok) {
console.log('✅ 服务运行正常')
console.log(` - HTTP状态: ${response.status}`)
console.log(` - 响应时间: ${response.headers.get('x-response-time') || 'N/A'}`)
} else {
console.log(`⚠️ 服务返回异常状态: ${response.status}`)
}
} catch (error) {
console.error('❌ 服务健康检查失败:', error)
throw error
}
}
async function main() {
console.log('╔════════════════════════════════════════════╗')
console.log('║ 分片上传优化测试 - 影子系统 ║')
console.log('║ Shadow System: http://192.168.58.20:5320 ║')
console.log('╚════════════════════════════════════════════╝')
try {
// 测试1: 网络检测
const networkStatus = await testNetworkDetection()
// 测试2: 分片大小计算
await testChunkSizeCalculation()
// 测试3: 上传性能预估
await testUploadPerformance(networkStatus)
// 测试4: 服务健康检查
await testHealthCheck()
console.log('\n╔════════════════════════════════════════════╗')
console.log('║ ✅ 所有测试通过 ║')
console.log('╚════════════════════════════════════════════╝')
console.log('\n📋 下一步操作:')
console.log(' 1. 在浏览器中访问: http://192.168.58.20:5320')
console.log(' 发者工具 (F12) -> Console 标签')
console.log(' 3. 上传一个 100MB+ 的文件')
console.log(' 4. 观察控制台输出,验证分片大小')
console.log('\n 预期日志:')
console.log(' [ChunkConfig] 检测到内网访问,使用 20MB 分片')
console.log(' [ChunkUpload] Chunk 0/N 完成')
} catch (error) {
console.error('\n❌ 测试失败:', error)
process.exit(1)
}
}
main()