Ton

Форк
0
/
LoadSpeed.cpp 
56 строк · 1.7 Кб
1
/*
2
    This file is part of TON Blockchain Library.
3

4
    TON Blockchain Library is free software: you can redistribute it and/or modify
5
    it under the terms of the GNU Lesser General Public License as published by
6
    the Free Software Foundation, either version 2 of the License, or
7
    (at your option) any later version.
8

9
    TON Blockchain Library is distributed in the hope that it will be useful,
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
    GNU Lesser General Public License for more details.
13

14
    You should have received a copy of the GNU Lesser General Public License
15
    along with TON Blockchain Library.  If not, see <http://www.gnu.org/licenses/>.
16

17
    Copyright 2017-2020 Telegram Systems LLP
18
*/
19

20
#include "LoadSpeed.h"
21

22
#include "td/utils/format.h"
23

24
namespace ton {
25
void LoadSpeed::add(td::uint64 size, td::Timestamp now) {
26
  total_size_ += size;
27
  events_.push(Event{size, now});
28
  update(now);
29
}
30
double LoadSpeed::speed(td::Timestamp now) const {
31
  update(now);
32
  return (double)total_size_ / duration(now);
33
}
34
void LoadSpeed::reset() {
35
  events_ = {};
36
  total_size_ = 0;
37
}
38

39
td::StringBuilder &operator<<(td::StringBuilder &sb, const LoadSpeed &speed) {
40
  return sb << td::format::as_size(static_cast<td::uint64>(speed.speed())) << "/s";
41
}
42

43
void LoadSpeed::update(td::Timestamp now) const {
44
  while (duration(now) > 10) {
45
    total_size_ -= events_.front().size;
46
    events_.pop();
47
  }
48
}
49
double LoadSpeed::duration(td::Timestamp now) const {
50
  double res = 5;
51
  if (!events_.empty()) {
52
    res = std::max(res, now.at() - events_.front().at.at());
53
  }
54
  return res;
55
}
56
}  // namespace ton
57

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.