document.write("");

测试网络延迟 工具类

 

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 @ 2024-11-12 15:37  人间春风意  阅读(0)  评论(0编辑  收藏  举报