document.write("");

测试网络延迟 工具类

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
 
public class NetworkLatencyTester {
 
    public static List<Long> integerIsReachableList = new ArrayList<>();
    public static List<Long> integerTCPList = new ArrayList<>();
    private final String host;
    private final int port;
    private final long interval;
    private final ScheduledExecutorService scheduler;
 
    public NetworkLatencyTester(String host, int port, long interval) {
        this.host = host;
        this.port = port;
        this.interval = interval;
        this.scheduler = Executors.newSingleThreadScheduledExecutor();
    }
 
    public void start() {
        scheduler.scheduleAtFixedRate(this::testLatency, 0, interval, TimeUnit.SECONDS);
    }
 
    public void stop() {
        scheduler.shutdown();
    }
 
    private void testLatency() {
        try {
            // 使用 InetAddress 测试 ICMP 延迟
            long startTime = System.currentTimeMillis();
            boolean reachable = InetAddress.getByName(host).isReachable(5000); // 超时时间为 5 秒
            long endTime = System.currentTimeMillis();
            long latency = endTime - startTime;
 
            if (reachable) {
                System.out.println("Host " + host + " is reachable. Latency: " + latency + " ms");
                integerIsReachableList.add(latency);
            } else {
                System.out.println("Host " + host + " is not reachable.");
            }
 
            // 使用 Socket 测试 TCP 延迟
            startTime = System.currentTimeMillis();
            try (Socket socket = new Socket(host, port)) {
                endTime = System.currentTimeMillis();
                latency = endTime - startTime;
                integerTCPList.add(latency);
                System.out.println("TCP connection to " + host + ":" + port + " successful. Latency: " + latency + " ms");
            } catch (IOException e) {
                endTime = System.currentTimeMillis();
                latency = endTime - startTime;
                System.out.println("TCP connection to " + host + ":" + port + " failed. Latency: " + latency + " ms");
            }
 
        } catch (IOException e) {
            System.err.println("Error testing latency: " + e.getMessage());
        }
    }
 
    public static void main(String[] args) {
        String host = "xx.xx.xx.xx"; // 替换为你要测试的IP或主机名
        int port = 8000; // 替换为你要测试的端口
        long interval = 5; // 测试间隔时间,单位为秒
 
        NetworkLatencyTester tester = new NetworkLatencyTester(host, port, interval);
        tester.start();
 
        // 为了防止程序立即退出,可以添加一个休眠时间
        try {
            Thread.sleep(123 * 1000); // 休眠 120 秒
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
 
        tester.stop();
 
        // 计算 integerIsReachableList 的平均值
        double averageIsReachableLatency = calculateAverage(integerIsReachableList);
        System.out.println("Average ICMP Latency: " + averageIsReachableLatency + " ms");
 
        // 计算 integerTCPList 的平均值
        double averageTCPLatency = calculateAverage(integerTCPList);
        System.out.println("Average TCP Latency: " + averageTCPLatency + " ms");
    }
 
    private static double calculateAverage(List<Long> list) {
        if (list.isEmpty()) {
            return 0.0;
        }
        long sum = 0;
        for (Long latency : list) {
            sum += latency;
        }
        return (double) sum / list.size();
    }
}

  

posted @   人间春风意  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示

距今时间:
1025天8.00 小时 52.40 分钟

当前新增阅读数:140327