mirror of
https://github.com/ton-blockchain/ton
synced 2025-02-12 19:22:37 +00:00
33 lines
814 B
C++
33 lines
814 B
C++
#include "LoadSpeed.h"
|
|
|
|
#include "td/utils/format.h"
|
|
|
|
namespace ton {
|
|
void LoadSpeed::add(td::size_t size, td::Timestamp now) {
|
|
total_size_ += size;
|
|
events_.push(Event{size, now});
|
|
update(now);
|
|
}
|
|
double LoadSpeed::speed(td::Timestamp now) const {
|
|
update(now);
|
|
return total_size_ / duration();
|
|
}
|
|
|
|
td::StringBuilder &operator<<(td::StringBuilder &sb, const LoadSpeed &speed) {
|
|
return sb << td::format::as_size(static_cast<td::uint64>(speed.speed())) << "/s";
|
|
}
|
|
|
|
void LoadSpeed::update(td::Timestamp now) const {
|
|
while (duration() > 60) {
|
|
total_size_ -= events_.front().size;
|
|
events_.pop();
|
|
}
|
|
}
|
|
double LoadSpeed::duration() const {
|
|
double res = 5;
|
|
if (events_.size() > 1) {
|
|
res = std::max(res, events_.back().at.at() - events_.front().at.at());
|
|
}
|
|
return res;
|
|
}
|
|
} // namespace ton
|