summaryrefslogtreecommitdiffstats
path: root/estimation-scripts/Estimators.rb
diff options
context:
space:
mode:
authortpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2010-01-20 02:37:40 +0000
committertpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2010-01-20 02:37:40 +0000
commit9ad5c7b5e23b4940e7a3ea3ca3a6fb77e6a8fab0 (patch)
treed088b5210e77d9fa91d954d8550e00e372b47378 /estimation-scripts/Estimators.rb
downloadktorrent-9ad5c7b5e23b4940e7a3ea3ca3a6fb77e6a8fab0.tar.gz
ktorrent-9ad5c7b5e23b4940e7a3ea3ca3a6fb77e6a8fab0.zip
Updated to final KDE3 ktorrent release (2.2.6)
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/applications/ktorrent@1077377 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'estimation-scripts/Estimators.rb')
-rw-r--r--estimation-scripts/Estimators.rb90
1 files changed, 90 insertions, 0 deletions
diff --git a/estimation-scripts/Estimators.rb b/estimation-scripts/Estimators.rb
new file mode 100644
index 0000000..1ef0c6e
--- /dev/null
+++ b/estimation-scripts/Estimators.rb
@@ -0,0 +1,90 @@
+require 'Sample'
+
+# abstract base class of all estimators
+
+class Estimator
+
+ # processes a sample
+ def process(sample)
+ end
+
+ # returns an estimate (ETA as float)
+ # note that you must process at least one sample before this will return meaningful output
+ def estimate
+ end
+
+ # returns the name of the estimator
+ def name
+ end
+end
+
+# estimator that uses the current speed
+class CSAEstimator < Estimator
+ def process(sample)
+ @sample = sample.clone
+ end
+
+ def estimate
+ return @sample.bytesLeft.to_f / @sample.speed
+ end
+
+ def name
+ 'CurrentSpeedEstimator'
+ end
+end
+
+# estimator that uses the global average speed of the whole torrent download for estimation
+
+class GASAEstimator < Estimator
+ def process(sample)
+ @first = sample.clone if @first == nil
+ @last = sample.clone
+ @avgSpeed = Sample.averageSpeed(@first, @last)
+ end
+
+ def estimate
+ return @last.bytesLeft.to_f / @avgSpeed
+ end
+
+ def name
+ 'AverageSpeedEstimator'
+ end
+end
+
+# estimator that uses the average over the last n seconds
+
+class WINXEstimator < Estimator
+
+ attr_reader :windowSize
+
+ def process(sample)
+ # remove all samples that are older than the window size. Note: samples are sorted.
+ @list.pop until @list.length <= 1 or (sample.time - @list.last.time) <= @windowSize
+
+ # prepend array with newest sample
+ @list.unshift(sample.clone)
+ end
+
+ def estimate
+
+ if @list.length > 1
+ first = @list.first
+ last = @list.last
+ return first.bytesLeft.to_f / Sample.averageSpeed(last, first)
+ elsif @list.length == 1
+ sample = @list.first
+ return sample.bytesLeft.to_f / sample.speed
+ elsif @list.length == 0
+ return 0
+ end
+ end
+
+ def name
+ "MovingAverageEstimator_#{@windowSize}s"
+ end
+
+ def initialize(windowSizeInSeconds)
+ @list = Array.new
+ @windowSize = windowSizeInSeconds
+ end
+end