Make progressbar display 10 second average download speed

This commit is contained in:
Sude 2015-04-27 16:51:17 +03:00
parent 43ee08227c
commit 21e6c6119d
2 changed files with 17 additions and 0 deletions

View File

@ -28,6 +28,7 @@
#include <json/json.h> #include <json/json.h>
#include <ctime> #include <ctime>
#include <fstream> #include <fstream>
#include <deque>
class Timer class Timer
{ {
@ -72,6 +73,7 @@ class Downloader
Timer timer; Timer timer;
Config config; Config config;
ProgressBar* progressbar; ProgressBar* progressbar;
std::deque< std::pair<time_t, size_t> > TimeAndSize;
protected: protected:
private: private:
CURLcode downloadFile(const std::string& url, const std::string& filepath, const std::string& xml_data = std::string(), const std::string& gamename = std::string()); CURLcode downloadFile(const std::string& url, const std::string& filepath, const std::string& xml_data = std::string(), const std::string& gamename = std::string());

View File

@ -1643,6 +1643,7 @@ int Downloader::downloadCovers(const std::string& gamename, const std::string& d
CURLcode Downloader::beginDownload() CURLcode Downloader::beginDownload()
{ {
this->TimeAndSize.clear();
this->timer.reset(); this->timer.reset();
CURLcode result = curl_easy_perform(curlhandle); CURLcode result = curl_easy_perform(curlhandle);
this->resume_position = 0; this->resume_position = 0;
@ -1734,6 +1735,20 @@ int Downloader::progressCallback(void *clientp, double dltotal, double dlnow, do
{ {
downloader->timer.reset(); downloader->timer.reset();
int iTermWidth = Util::getTerminalWidth(); 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<size_t>(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<double>((time_last - time_first));
}
bptime::time_duration eta(bptime::seconds((long)((dltotal - dlnow) / rate))); bptime::time_duration eta(bptime::seconds((long)((dltotal - dlnow) / rate)));
std::stringstream eta_ss; std::stringstream eta_ss;
if (eta.hours() > 23) if (eta.hours() > 23)