diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2008-07-20 07:41:33 +0000 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2008-07-20 07:41:33 +0000 |
| commit | f242a1ec8fde0c47db52b0d652ad7890a153fd57 (patch) | |
| tree | f16de8ede7cca2464a04f3f7c277d173cfefd471 | |
| parent | 4947fa39b3029470b851a1cfc7f8d2152a5689e5 (diff) | |
2008-07-20 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Cache last calculated average download/upload speed.
PeerStat::getAvgDownloadSpeed(), PeerStat::getAvgUploadSpeed()
return
cached value.
Now SpeedCalc::changeSw() is called from
SpeedCalc::calculateSpeed()
* src/PeerStat.h
* src/SpeedCalc.cc
* src/SpeedCalc.h
| -rw-r--r-- | ChangeLog | 9 | ||||
| -rw-r--r-- | src/PeerStat.h | 22 | ||||
| -rw-r--r-- | src/SpeedCalc.cc | 8 | ||||
| -rw-r--r-- | src/SpeedCalc.h | 2 |
4 files changed, 36 insertions, 5 deletions
@@ -1,3 +1,12 @@ +2008-07-20 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> + + Cache last calculated average download/upload speed. + PeerStat::getAvgDownloadSpeed(), PeerStat::getAvgUploadSpeed() return + cached value. + * src/PeerStat.h + * src/SpeedCalc.cc + * src/SpeedCalc.h + 2008-07-19 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> Call parseUrl directly. diff --git a/src/PeerStat.h b/src/PeerStat.h index ce2860f8..eb118b71 100644 --- a/src/PeerStat.h +++ b/src/PeerStat.h @@ -54,9 +54,13 @@ private: SpeedCalc uploadSpeed; Time downloadStartTime; PeerStat::STATUS status; + unsigned int _avgDownloadSpeed; + unsigned int _avgUploadSpeed; public: - PeerStat(int32_t cuid = 0):cuid(cuid), status(PeerStat::IDLE) {} + PeerStat(int32_t cuid = 0):cuid(cuid), status(PeerStat::IDLE), + _avgDownloadSpeed(0), + _avgUploadSpeed(0) {} ~PeerStat() {} @@ -71,6 +75,11 @@ public: return downloadSpeed.calculateSpeed(now); } + unsigned int calculateAvgDownloadSpeed() { + _avgDownloadSpeed = downloadSpeed.calculateAvgSpeed(); + return _avgDownloadSpeed; + } + unsigned int calculateUploadSpeed() { return uploadSpeed.calculateSpeed(); } @@ -79,6 +88,11 @@ public: return uploadSpeed.calculateSpeed(now); } + unsigned int calculateAvgUploadSpeed() { + _avgUploadSpeed = uploadSpeed.calculateAvgSpeed(); + return _avgUploadSpeed; + } + void updateDownloadLength(size_t bytes) { downloadSpeed.update(bytes); } @@ -96,11 +110,11 @@ public: } unsigned int getAvgDownloadSpeed() const { - return downloadSpeed.getAvgSpeed(); + return _avgDownloadSpeed; } unsigned int getAvgUploadSpeed() const { - return uploadSpeed.getAvgSpeed(); + return _avgUploadSpeed; } void reset() { @@ -116,6 +130,8 @@ public: } void downloadStop() { + calculateAvgDownloadSpeed(); + calculateAvgUploadSpeed(); status = PeerStat::IDLE; } diff --git a/src/SpeedCalc.cc b/src/SpeedCalc.cc index df0d0f92..d4fa4481 100644 --- a/src/SpeedCalc.cc +++ b/src/SpeedCalc.cc @@ -64,6 +64,9 @@ unsigned int SpeedCalc::calculateSpeed() { unsigned int speed = lengthArray[sw]*1000/milliElapsed; prevSpeed = speed; maxSpeed = std::max(speed, maxSpeed); + if(isIntervalOver()) { + changeSw(); + } return speed; } else { return prevSpeed; @@ -76,6 +79,9 @@ unsigned int SpeedCalc::calculateSpeed(const struct timeval& now) { unsigned int speed = lengthArray[sw]*1000/milliElapsed; prevSpeed = speed; maxSpeed = std::max(speed, maxSpeed); + if(isIntervalOver()) { + changeSw(); + } return speed; } else { return prevSpeed; @@ -102,7 +108,7 @@ void SpeedCalc::changeSw() { nextInterval = cpArray[sw].difference()+CHANGE_INTERVAL_SEC; } -unsigned int SpeedCalc::getAvgSpeed() const { +unsigned int SpeedCalc::calculateAvgSpeed() const { uint64_t milliElapsed = start.differenceInMillis(); if(milliElapsed) { unsigned int speed = accumulatedLength*1000/milliElapsed; diff --git a/src/SpeedCalc.h b/src/SpeedCalc.h index c9677ee1..80df7311 100644 --- a/src/SpeedCalc.h +++ b/src/SpeedCalc.h @@ -71,7 +71,7 @@ public: return maxSpeed; } - unsigned int getAvgSpeed() const; + unsigned int calculateAvgSpeed() const; void update(size_t bytes); |
