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
pull/1/head
Tatsuhiro Tsujikawa 2008-07-20 07:41:33 +00:00
parent 4947fa39b3
commit f242a1ec8f
4 changed files with 36 additions and 5 deletions

View File

@ -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> 2008-07-19 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Call parseUrl directly. Call parseUrl directly.

View File

@ -54,9 +54,13 @@ private:
SpeedCalc uploadSpeed; SpeedCalc uploadSpeed;
Time downloadStartTime; Time downloadStartTime;
PeerStat::STATUS status; PeerStat::STATUS status;
unsigned int _avgDownloadSpeed;
unsigned int _avgUploadSpeed;
public: 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() {} ~PeerStat() {}
@ -71,6 +75,11 @@ public:
return downloadSpeed.calculateSpeed(now); return downloadSpeed.calculateSpeed(now);
} }
unsigned int calculateAvgDownloadSpeed() {
_avgDownloadSpeed = downloadSpeed.calculateAvgSpeed();
return _avgDownloadSpeed;
}
unsigned int calculateUploadSpeed() { unsigned int calculateUploadSpeed() {
return uploadSpeed.calculateSpeed(); return uploadSpeed.calculateSpeed();
} }
@ -79,6 +88,11 @@ public:
return uploadSpeed.calculateSpeed(now); return uploadSpeed.calculateSpeed(now);
} }
unsigned int calculateAvgUploadSpeed() {
_avgUploadSpeed = uploadSpeed.calculateAvgSpeed();
return _avgUploadSpeed;
}
void updateDownloadLength(size_t bytes) { void updateDownloadLength(size_t bytes) {
downloadSpeed.update(bytes); downloadSpeed.update(bytes);
} }
@ -96,11 +110,11 @@ public:
} }
unsigned int getAvgDownloadSpeed() const { unsigned int getAvgDownloadSpeed() const {
return downloadSpeed.getAvgSpeed(); return _avgDownloadSpeed;
} }
unsigned int getAvgUploadSpeed() const { unsigned int getAvgUploadSpeed() const {
return uploadSpeed.getAvgSpeed(); return _avgUploadSpeed;
} }
void reset() { void reset() {
@ -116,6 +130,8 @@ public:
} }
void downloadStop() { void downloadStop() {
calculateAvgDownloadSpeed();
calculateAvgUploadSpeed();
status = PeerStat::IDLE; status = PeerStat::IDLE;
} }

View File

@ -64,6 +64,9 @@ unsigned int SpeedCalc::calculateSpeed() {
unsigned int speed = lengthArray[sw]*1000/milliElapsed; unsigned int speed = lengthArray[sw]*1000/milliElapsed;
prevSpeed = speed; prevSpeed = speed;
maxSpeed = std::max(speed, maxSpeed); maxSpeed = std::max(speed, maxSpeed);
if(isIntervalOver()) {
changeSw();
}
return speed; return speed;
} else { } else {
return prevSpeed; return prevSpeed;
@ -76,6 +79,9 @@ unsigned int SpeedCalc::calculateSpeed(const struct timeval& now) {
unsigned int speed = lengthArray[sw]*1000/milliElapsed; unsigned int speed = lengthArray[sw]*1000/milliElapsed;
prevSpeed = speed; prevSpeed = speed;
maxSpeed = std::max(speed, maxSpeed); maxSpeed = std::max(speed, maxSpeed);
if(isIntervalOver()) {
changeSw();
}
return speed; return speed;
} else { } else {
return prevSpeed; return prevSpeed;
@ -102,7 +108,7 @@ void SpeedCalc::changeSw() {
nextInterval = cpArray[sw].difference()+CHANGE_INTERVAL_SEC; nextInterval = cpArray[sw].difference()+CHANGE_INTERVAL_SEC;
} }
unsigned int SpeedCalc::getAvgSpeed() const { unsigned int SpeedCalc::calculateAvgSpeed() const {
uint64_t milliElapsed = start.differenceInMillis(); uint64_t milliElapsed = start.differenceInMillis();
if(milliElapsed) { if(milliElapsed) {
unsigned int speed = accumulatedLength*1000/milliElapsed; unsigned int speed = accumulatedLength*1000/milliElapsed;

View File

@ -71,7 +71,7 @@ public:
return maxSpeed; return maxSpeed;
} }
unsigned int getAvgSpeed() const; unsigned int calculateAvgSpeed() const;
void update(size_t bytes); void update(size_t bytes);