Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
388 views
in Technique[技术] by (71.8m points)

怎么获取到redis每秒执行的命令数?

一些redis的监控工具能够计算出redis每秒执行的命令数,例如redis-stat:
image.png

请问这是如何计算出来的?是通过info命令拿到总命令数去相减吗?如果是这样的话,考虑到发命令获取数据会有一定的延迟,数据是否会有一些偏差?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

对,就是 redis info,结果里有个 instantaneous_ops_per_sec 指标,每秒执行命令数,这个是由 Redis 自己统计的。

但第一,这个值不是实时的,有延后性;第二,这个值由 Redis 自行维护,实际是 16 个周期的平均采样数据(即 1.6 秒的总数,除以 1.6),结果只能说是近似。毕竟强实时、高精度,必然会影响性能。


Redis 相关源码:

#define STATS_METRIC_SAMPLES 16     /* Number of samples per metric. */
#define STATS_METRIC_COMMAND 0      /* Number of commands executed. */

/* Return the mean of all the samples. */
long long getInstantaneousMetric(int metric) {
    int j;
    long long sum = 0;

    for (j = 0; j < STATS_METRIC_SAMPLES; j++)
        sum += server.inst_metric[metric].samples[j];
    return sum / STATS_METRIC_SAMPLES;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...