test-ping-new.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. const SpeedTester = require('./src/core/speedTester');
  2. async function testNewPing() {
  3. console.log('🚀 开始测试重写后的Ping功能...\n');
  4. const speedTester = new SpeedTester();
  5. // 测试单个ping
  6. console.log('📡 测试单个Ping...');
  7. const singleResult = await speedTester.pingHosts('www.google.com', 443, 3, 2000);
  8. console.log('单个Ping结果:', JSON.stringify(singleResult, null, 2));
  9. // 测试批量ping
  10. console.log('\n📡 测试批量Ping...');
  11. const hosts = [
  12. { host: 'www.google.com', port: 443 },
  13. { host: 'www.github.com', port: 443 },
  14. { host: 'www.youtube.com', port: 443 },
  15. { host: 'www.cloudflare.com', port: 443 },
  16. { host: 'www.baidu.com', port: 443 }
  17. ];
  18. const batchResult = await speedTester.batchPingHosts(hosts);
  19. console.log('批量Ping结果:');
  20. batchResult.results.forEach((result, index) => {
  21. const status = result.success ? '✅' : '❌';
  22. const latency = result.success ? `${result.latency}ms (平均: ${result.avg}ms)` : '失败';
  23. console.log(`${index + 1}. ${status} ${result.host}:${result.port} - ${latency}`);
  24. });
  25. // 统计信息
  26. console.log('\n📊 统计信息:');
  27. console.log(`总测试数: ${batchResult.summary.total}`);
  28. console.log(`成功数: ${batchResult.summary.successful}`);
  29. console.log(`失败数: ${batchResult.summary.failed}`);
  30. console.log(`平均延迟: ${Math.round(batchResult.summary.avgLatency)}ms`);
  31. // 测试API兼容性
  32. console.log('\n🔧 测试API兼容性...');
  33. const testHosts = [
  34. { host: 'www.google.com', port: 443 },
  35. { host: 'www.github.com', port: 443 }
  36. ];
  37. try {
  38. const response = await fetch('http://localhost:3000/api/test/ping/batch', {
  39. method: 'POST',
  40. headers: {
  41. 'Content-Type': 'application/json'
  42. },
  43. body: JSON.stringify({
  44. hosts: testHosts,
  45. attempts: 3,
  46. timeout: 2000
  47. })
  48. });
  49. const apiResult = await response.json();
  50. console.log('API测试结果:', JSON.stringify(apiResult, null, 2));
  51. } catch (error) {
  52. console.log('API测试失败 (服务器可能未启动):', error.message);
  53. }
  54. console.log('\n✅ 测试完成!');
  55. }
  56. // 运行测试
  57. testNewPing().catch(console.error);