diff options
| author | Michele Calgaro <michele.calgaro@yahoo.it> | 2025-03-02 18:37:22 +0900 |
|---|---|---|
| committer | Michele Calgaro <michele.calgaro@yahoo.it> | 2025-03-06 12:43:06 +0900 |
| commit | bb099158e6c9fd0f1c2771cb9350d3b0a0b51a27 (patch) | |
| tree | 598156ab1f71ee857c8e1f233f0292e13f36fa28 /src/estimation-scripts/Estimators.rb | |
| parent | 7271fa27ac1881beb5326ca8c9c83471695d8487 (diff) | |
| download | ktorrent-bb099158.tar.gz ktorrent-bb099158.zip | |
Restructure source files into 'src' subfolder
Signed-off-by: Michele Calgaro <michele.calgaro@yahoo.it>
(cherry picked from commit 44ef0bd5fe47a43e47aec5f7981b6c1d728dd9a8)
Diffstat (limited to 'src/estimation-scripts/Estimators.rb')
| -rw-r--r-- | src/estimation-scripts/Estimators.rb | 90 |
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 |
