diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2016-03-16 21:26:15 +0900 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2016-03-16 21:26:15 +0900 |
| commit | 1d9a4a8aec2483b112f4b8ec6d8f3d2f527c05ff (patch) | |
| tree | 42b368c158a1574202de9a28efa53e497b159c49 /src/SpeedCalc.cc | |
| parent | f0f1b8ef47c391550ff99982f1cad3dd4e1d410d (diff) | |
| parent | 60d0cb0e24974534c10f63cf6833119b6af7be2b (diff) | |
Merge branch 'oliviercommelarbre-optimize-concurrent-downloads'
Diffstat (limited to 'src/SpeedCalc.cc')
| -rw-r--r-- | src/SpeedCalc.cc | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/src/SpeedCalc.cc b/src/SpeedCalc.cc index a2bf4ee9..2c49c9ef 100644 --- a/src/SpeedCalc.cc +++ b/src/SpeedCalc.cc @@ -42,7 +42,7 @@ namespace aria2 { namespace { -constexpr auto WINDOW_TIME = 15_s; +constexpr auto WINDOW_TIME = 10_s; } // namespace SpeedCalc::SpeedCalc() : accumulatedLength_(0), bytesWindow_(0), maxSpeed_(0) {} @@ -84,6 +84,31 @@ int SpeedCalc::calculateSpeed() return speed; } +int SpeedCalc::calculateNewestSpeed(int seconds) +{ + const auto& now = global::wallclock(); + removeStaleTimeSlot(now); + + int64_t bytesCount(0); + auto it = timeSlots_.rbegin(); + while (it != timeSlots_.rend()) { + if (it->first.difference(now) > seconds * 1_s) { + break; + } + bytesCount += (*it++).second; + } + if (it == timeSlots_.rbegin()) { + return 0; + } + + auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>( + (*--it).first.difference(now)).count(); + if (elapsed <= 0) { + elapsed = 1; + } + return bytesCount * (1000. / elapsed); +} + void SpeedCalc::update(size_t bytes) { const auto& now = global::wallclock(); |
