summaryrefslogtreecommitdiff
path: root/src/SpeedCalc.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/SpeedCalc.cc')
-rw-r--r--src/SpeedCalc.cc27
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();