c++ - 吞吐量网络估计

标签 c++ algorithm networking network-programming

我如何估计瞬时吞吐量?例如,以类似于浏览器在下载文件时所做的方式。这不仅仅是平均吞吐量,而是瞬时估计,可能带有“移动平均数”。我正在寻找算法,但您可以在 C++ 中指定它。理想情况下,它不会涉及线程(即持续刷新,比如说每秒),而是仅在询问值时才进行评估。

最佳答案

您可以使用指数移动平均线,如 here 所述,但我会重复这个公式:

accumulator = (alpha * new_value) + (1.0 - alpha) * accumulator

为了实现估算,假设您打算每秒查询一次计算,但您想要最后一分钟的平均值。那么,这里是获得该估计值的一种方法:

struct AvgBps {
    double rate_;            // The average rate
    double last_;            // Accumulates bytes added until average is computed
    time_t prev_;            // Time of previous update
    AvgBps () : rate_(0), last_(0), prev_(time(0)) {}
    void add (unsigned bytes) {
        time_t now = time(0);
        if (now - prev_ < 60) {       // The update is within the last minute
            last_ += bytes;           // Accumulate bytes into last
            if (now > prev_) {        // More than a second elapsed from previous
                // exponential moving average
                // the more time that has elapsed between updates, the more
                // weight is assigned for the accumulated bytes
                double alpha = (now - prev_)/60.0;
                rate_ = (1 -alpha) * last_ + alpha * rate_;
                last_ = 0;            // Reset last_ (it has been averaged in)
                prev_ = now;          // Update prev_ to current time
            }
        } else {                      // The update is longer than a minute ago
            rate_ = bytes;            // Current update is average rate
            last_ = 0;                // Reset last_
            prev_ = now;              // Update prev_
        }
    }
    double rate () {
        add(0);                       // Compute rate by doing an update of 0 bytes
        return rate_;                 // Return computed rate
    }
};

您实际上应该使用单调时钟而不是 time

关于c++ - 吞吐量网络估计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11735258/

相关文章:

c++ - 多个 WT 应用程序可以在同一个端口上运行吗?

python - 如何从一组 GPS 点中获取 "fastest miles"的列表

c++ - 从图像制作多边形的算法

c++ - 哪种数据结构最适合实现字典?

c++ - 找到两个给定单词和字典之间的最短单词阶梯

algorithm - 统计 11 中 1 的 N 次方个数

c# - 7zip 压缩网络流

c++ - 在 Windows CE 7 上指定动态端口范围

http - 为什么浏览器会对同一个文件发出两个单独的请求?

c++ - 编译 libpng 并将其与 netbeans 和 mingw 一起使用