单条通道统计: ChannelStat

ChannelStat 是用于统计单条 TCP 通道的流量数据的类。t-io 自称拥有最强级别的流量统计和监控,这一点也绝非空谈。通过 ChannelStat,你可以获取一条 TCP 通道的各种统计信息,包括流量、消息处理效率、连接状态等。

如何获取 ChannelStat

每个 ChannelContext 对象都有一个 ChannelStat 实例,可以通过以下方式获取:

ChannelStat channelStat = channelContext.stat;

ChannelStat 统计项详解

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

ChannelStat 类定义

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

    // 本次解码失败的次数
    public int decodeFailCount = 0;

    // 最近一次收到完整业务消息包的时间
    public long latestTimeOfReceivedPacket = SystemTimer.currTime;

    // 最近一次发送完整业务消息包的时间
    public long latestTimeOfSentPacket = SystemTimer.currTime;

    // 最近一次收到字节的时间(无论是否为完整消息包)
    public long latestTimeOfReceivedByte = SystemTimer.currTime;

    // 最近一次发送字节的时间(无论是否为完整消息包)
    public long latestTimeOfSentByte = SystemTimer.currTime;

    // ChannelContext 对象创建的时间
    public long timeCreated = System.currentTimeMillis();

    // 第一次连接成功的时间
    public Long timeFirstConnected = null;

    // 连接关闭的时间
    public long timeClosed = SystemTimer.currTime;

    // 进入重连队列的时间
    public long timeInReconnQueue = SystemTimer.currTime;

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

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

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

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

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

    // 本连接已接收的字节数
    public final AtomicLong receivedBytes = new AtomicLong();

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

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

主要统计项和方法

  1. decodeFailCount
    解码失败的次数,用于统计连接在解码过程中发生错误的次数。

  2. latestTimeOfReceivedPacketlatestTimeOfSentPacket
    最近一次接收和发送完整业务消息包的时间,单位为毫秒。用于监控消息包的接收和发送状态。

  3. latestTimeOfReceivedBytelatestTimeOfSentByte
    最近一次接收和发送字节的时间,包括未组成完整消息包的字节。

  4. timeCreatedtimeFirstConnectedtimeClosedtimeInReconnQueue

    • timeCreatedChannelContext 对象的创建时间。
    • timeFirstConnected:第一次成功连接的时间。
    • timeClosed:连接关闭的时间。
    • timeInReconnQueue:进入重连队列的时间。
  5. sentBytessentPackets

    • sentBytes:本连接已发送的总字节数。
    • sentPackets:本连接已发送的消息包数。
  6. handledByteshandledPackets

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

  8. receivedBytesreceivedTcpsreceivedPackets

    • receivedBytes:本连接已接收的字节数。
    • receivedTcps:本连接已接收的 TCP 数据包次数。
    • receivedPackets:本连接已接收的消息包数。
  9. 计算方法

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

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

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

示例方法

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

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

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

总结

ChannelStat 提供了全面的流量统计和监控功能,包括字节数、消息包数、解码失败次数、连接时间、处理耗时等。这些统计信息对于分析 TCP 通道的性能、诊断问题、检测潜在攻击行为都非常有用。通过 ChannelStat,你可以实时监控每条 TCP 通道的运行状况,从而更好地保障系统的稳定和安全。