From 21e6c6119d7bad94b6771a73f323e384576ca725 Mon Sep 17 00:00:00 2001 From: Sude Date: Mon, 27 Apr 2015 16:51:17 +0300 Subject: [PATCH] Make progressbar display 10 second average download speed --- include/downloader.h | 2 ++ src/downloader.cpp | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/include/downloader.h b/include/downloader.h index d701714..4633679 100644 --- a/include/downloader.h +++ b/include/downloader.h @@ -28,6 +28,7 @@ #include #include #include +#include class Timer { @@ -72,6 +73,7 @@ class Downloader Timer timer; Config config; ProgressBar* progressbar; + std::deque< std::pair > TimeAndSize; protected: private: CURLcode downloadFile(const std::string& url, const std::string& filepath, const std::string& xml_data = std::string(), const std::string& gamename = std::string()); diff --git a/src/downloader.cpp b/src/downloader.cpp index 40a6c33..5183567 100644 --- a/src/downloader.cpp +++ b/src/downloader.cpp @@ -1643,6 +1643,7 @@ int Downloader::downloadCovers(const std::string& gamename, const std::string& d CURLcode Downloader::beginDownload() { + this->TimeAndSize.clear(); this->timer.reset(); CURLcode result = curl_easy_perform(curlhandle); this->resume_position = 0; @@ -1734,6 +1735,20 @@ int Downloader::progressCallback(void *clientp, double dltotal, double dlnow, do { downloader->timer.reset(); int iTermWidth = Util::getTerminalWidth(); + + // 10 second average download speed + // Don't use static value of 10 seconds because update interval depends on when and how often progress callback is called + downloader->TimeAndSize.push_back(std::make_pair(time(NULL), static_cast(dlnow))); + if (downloader->TimeAndSize.size() > 100) // 100 * 100ms = 10s + { + downloader->TimeAndSize.pop_front(); + time_t time_first = downloader->TimeAndSize.front().first; + size_t size_first = downloader->TimeAndSize.front().second; + time_t time_last = downloader->TimeAndSize.back().first; + size_t size_last = downloader->TimeAndSize.back().second; + rate = (size_last - size_first) / static_cast((time_last - time_first)); + } + bptime::time_duration eta(bptime::seconds((long)((dltotal - dlnow) / rate))); std::stringstream eta_ss; if (eta.hours() > 23)