Selaa lähdekoodia

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
Tatsuhiro Tsujikawa 17 vuotta sitten
vanhempi
commit
f242a1ec8f
4 muutettua tiedostoa jossa 36 lisäystä ja 5 poistoa
  1. 9 0
      ChangeLog
  2. 19 3
      src/PeerStat.h
  3. 7 1
      src/SpeedCalc.cc
  4. 1 1
      src/SpeedCalc.h

+ 9 - 0
ChangeLog

@@ -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.

+ 19 - 3
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;
   }
 

+ 7 - 1
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;

+ 1 - 1
src/SpeedCalc.h

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