summaryrefslogtreecommitdiffstats
path: root/src/estimation-scripts/Estimators.rb
diff options
context:
space:
mode:
authorMichele Calgaro <[email protected]>2025-03-02 18:37:22 +0900
committerMichele Calgaro <[email protected]>2025-03-06 12:31:12 +0900
commit44ef0bd5fe47a43e47aec5f7981b6c1d728dd9a8 (patch)
tree2b29e921a9bccea53444ed9bbed06a25a5fe20cc /src/estimation-scripts/Estimators.rb
parentd1f24dae035c506d945ca13f2be398aa0a4de8cc (diff)
downloadktorrent-master.tar.gz
ktorrent-master.zip
Restructure source files into 'src' subfolderHEADmaster
Signed-off-by: Michele Calgaro <[email protected]>
Diffstat (limited to 'src/estimation-scripts/Estimators.rb')
-rw-r--r--src/estimation-scripts/Estimators.rb90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/estimation-scripts/Estimators.rb b/src/estimation-scripts/Estimators.rb
new file mode 100644
index 0000000..1ef0c6e
--- /dev/null
+++ b/src/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