123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- const SpeedTester = require('./src/core/speedTester');
- async function testNewPing() {
- console.log('🚀 开始测试重写后的Ping功能...\n');
-
- const speedTester = new SpeedTester();
-
- // 测试单个ping
- console.log('📡 测试单个Ping...');
- const singleResult = await speedTester.pingHosts('www.google.com', 443, 3, 2000);
- console.log('单个Ping结果:', JSON.stringify(singleResult, null, 2));
-
- // 测试批量ping
- console.log('\n📡 测试批量Ping...');
- const hosts = [
- { host: 'www.google.com', port: 443 },
- { host: 'www.github.com', port: 443 },
- { host: 'www.youtube.com', port: 443 },
- { host: 'www.cloudflare.com', port: 443 },
- { host: 'www.baidu.com', port: 443 }
- ];
-
- const batchResult = await speedTester.batchPingHosts(hosts);
-
- console.log('批量Ping结果:');
- batchResult.results.forEach((result, index) => {
- const status = result.success ? '✅' : '❌';
- const latency = result.success ? `${result.latency}ms (平均: ${result.avg}ms)` : '失败';
- console.log(`${index + 1}. ${status} ${result.host}:${result.port} - ${latency}`);
- });
-
- // 统计信息
- console.log('\n📊 统计信息:');
- console.log(`总测试数: ${batchResult.summary.total}`);
- console.log(`成功数: ${batchResult.summary.successful}`);
- console.log(`失败数: ${batchResult.summary.failed}`);
- console.log(`平均延迟: ${Math.round(batchResult.summary.avgLatency)}ms`);
-
- // 测试API兼容性
- console.log('\n🔧 测试API兼容性...');
- const testHosts = [
- { host: 'www.google.com', port: 443 },
- { host: 'www.github.com', port: 443 }
- ];
-
- try {
- const response = await fetch('http://localhost:3000/api/test/ping/batch', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json'
- },
- body: JSON.stringify({
- hosts: testHosts,
- attempts: 3,
- timeout: 2000
- })
- });
-
- const apiResult = await response.json();
- console.log('API测试结果:', JSON.stringify(apiResult, null, 2));
- } catch (error) {
- console.log('API测试失败 (服务器可能未启动):', error.message);
- }
-
- console.log('\n✅ 测试完成!');
- }
- // 运行测试
- testNewPing().catch(console.error);
|