使用HashedWheelTimer进行调度
博客专区 > go4it 的博客 > 博客详情
使用HashedWheelTimer进行调度
go4it 发表于1年前
使用HashedWheelTimer进行调度
  • 发表于 1年前
  • 阅读 6
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

##实例

public class PerformanceMetricsTask implements TimerTask {

    // Values to use with TimeOutTaskCreator
    // Count performance metrics every second
    public static final long DELAY = 1;
    public static final TimeUnit UNIT = TimeUnit.SECONDS;

    private static final int BYTES_PER_KILO = 1024;
    private ByteCounterHandler byteCounter;
    private MessageCounterHandler msgCounter;
    private static Logger logger = LoggerFactory.getLogger(PerformanceMetricsTask.class);

    public PerformanceMetricsTask(ByteCounterHandler byteCounter, MessageCounterHandler msgCounter) {
        this.msgCounter = msgCounter;
        this.byteCounter = byteCounter;
    }

    @Override
    public void run(Timeout timeout) throws Exception {

        if (logger.isInfoEnabled()) {

            // ****************************** Network Stats

            // Bytes read and written in kilobytes
            double bytesRead = (byteCounter != null) ? byteCounter.getReadBytesAndClear()
                    / (double) (BYTES_PER_KILO * DELAY) : 0;
            double bytesWritten = (byteCounter != null) ? byteCounter.getWrittenBytesAndClear()
                    / (double) (BYTES_PER_KILO * DELAY) : 0;

            // Message read and written in thousands
            double msgRead = (msgCounter != null) ? msgCounter.getReadMessagesAndClear()
                    / (double) (BYTES_PER_KILO * DELAY) : 0;
            double msgWritten = (msgCounter != null) ? msgCounter.getWrittenMessagesAndClear()
                    / (double) (BYTES_PER_KILO * DELAY) : 0;

            // ***************************** CPU / RAM Stats

            RamUsageInfo systemRam = PerformanceMetrics.instance().getSystemRamInfo();
            CpuUsageInfo cpuInfo = PerformanceMetrics.instance().getCpuUsageInfo();

            double cpuUsage = cpuInfo.totalPerc * 100;
            double ramUsage = (systemRam.ramUsed / (double) systemRam.ramMax) * 100;

            // Form the reqRespReport
            String reqRespReport = "\t(Key:AvgResp)";
            for (Integer key : ServerState.reqRespMetricsMap.keySet()) {
                ReqRespMetrics.Data rrMet = ServerState.reqRespMetricsMap.get(key).getAndResetInfo();

                double avgResp = (rrMet.totalNumReqResps == 0) ? 0 : rrMet.totalRespTime
                        / (double) rrMet.totalNumReqResps;
                reqRespReport += String.format("(" + OneMessage.Type.valueOf(key) + ":%.2f) ", avgResp);
            }
            reqRespReport += "\n";

            String networkReport = String.format("Performance Report\n"
                    + "\t(Read/Write): Messages(1000s): (%.2f/%.2f) KB: (%.2f/%.2f)\n", msgRead, msgWritten, bytesRead,
                    bytesWritten);
            String statusReport = String.format("\tNum Active Connections: %d Num Logger In Users: %d\n",
                    ServerState.numActiveConnections.longValue(), ServerState.numLoggedInUsers.longValue());
            String cpuMemReport = String.format("\tSystem CPU Usage (%%): %.2f System Ram Usage (%%): %.2f", cpuUsage,
                    ramUsage);
            logger.info(networkReport + reqRespReport + statusReport + cpuMemReport);
        }

        // Make this task recurring
        TimeOutTaskCreator.createTask(new PerformanceMetricsTask(byteCounter, msgCounter), DELAY, UNIT);
    }
}

##docs

标签: Netty
共有 人打赏支持
粉丝 21
博文 453
码字总数 237132
×
go4it
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: