所有通道统计: GroupStat

GroupStatChannelStat 的集合,用于统计所有 TCP 通道的流量数据和状态。它有两个子类:ServerGroupStatClientGroupStat。当 t-io 用作 TCP 服务器时,你将获取 ServerGroupStat,反之则获取 ClientGroupStat

如何获取 GroupStat

可以通过 TioConfig 对象获取 GroupStat

GroupStat groupStat = tioConfig.groupStat;

GroupStat 统计项详解

以下是 GroupStat 类的源代码以及每个字段和方法的详细说明:

public class GroupStat implements java.io.Serializable {
    private static final long serialVersionUID = -6988655941470121164L;

    // 关闭的连接数
    public final AtomicLong closed = new AtomicLong();

    // 接收到的消息包数
    public final AtomicLong receivedPackets = new AtomicLong();

    // 接收到的消息字节数
    public final AtomicLong receivedBytes = new AtomicLong();

    // 已处理的消息包数
    public final AtomicLong handledPackets = new AtomicLong();

    // 处理消息包的总耗时,单位:毫秒
    public final AtomicLong handledPacketCosts = new AtomicLong();

    // 已处理的字节数
    public final AtomicLong handledBytes = new AtomicLong();

    // 发送的消息包数
    public final AtomicLong sentPackets = new AtomicLong();

    // 发送的字节数
    public final AtomicLong sentBytes = new AtomicLong();

    // 接收到的 TCP 数据包次数
    public final AtomicLong receivedTcps = new AtomicLong();

主要统计项和方法

  1. closed
    记录已关闭的连接数。

  2. receivedPacketsreceivedBytes

    • receivedPackets:接收到的消息包数。
    • receivedBytes:接收到的消息字节数。
  3. handledPacketshandledBytes

    • handledPackets:已处理的消息包数。
    • handledBytes:已处理的字节数。
  4. handledPacketCosts
    处理消息包的总耗时,单位为毫秒。可用于计算每个消息包的平均处理时间。

  5. sentPacketssentBytes

    • sentPackets:发送的消息包数。
    • sentBytes:发送的字节数。
  6. receivedTcps
    接收到的 TCP 数据包次数。

计算方法

  • getBytesPerTcpReceive()
    计算平均每次 TCP 接收到的字节数。此值可用于监控潜在的慢攻击行为,如果此值非常小,可能表示系统正在遭受攻击。

  • getPacketsPerTcpReceive()
    计算平均每次 TCP 接收到的业务包数。此值越小,越有可能存在慢攻击。

  • getHandledCostsPerPacket()
    计算每个消息包的平均处理耗时,单位为毫秒。

示例方法

public double getBytesPerTcpReceive() {
    if (receivedTcps.get() == 0) {
        return 0;
    }
    return (double) receivedBytes.get() / receivedTcps.get();
}

public double getPacketsPerTcpReceive() {
    if (receivedTcps.get() == 0) {
        return 0;
    }
    return (double) receivedPackets.get() / receivedTcps.get();
}

public double getHandledCostsPerPacket() {
    if (handledPackets.get() > 0) {
        return handledPacketCosts.get() / handledPackets.get();
    }
    return 0;
}

子类:ServerGroupStat 和 ClientGroupStat

GroupStat 有两个子类,它们各自针对服务器和客户端扩展了一些特定的字段。

ServerGroupStat

用于服务器端统计,继承自 GroupStat,并增加了对已接受连接数的统计:

public class ServerGroupStat extends GroupStat {
    private static final long serialVersionUID = -139100692961946342L;

    // 已接受的连接数
    public final AtomicLong accepted = new AtomicLong();

    public AtomicLong getAccepted() {
        return accepted;
    }
}

ClientGroupStat

用于客户端统计,继承自 GroupStat,但未添加新的统计项:

public class ClientGroupStat extends GroupStat {
    private static final long serialVersionUID = 804270967157603097L;
}

总结

GroupStat 提供了全局的流量统计和监控功能,包括消息包的接收、发送、处理,以及连接的关闭和耗时统计。通过 GroupStat,可以实时监控所有通道的运行状况,识别系统性能和潜在问题。ServerGroupStatClientGroupStat 进一步扩展了服务器和客户端的特定统计项,方便开发者根据不同的场景进行监控和优化。