summaryrefslogtreecommitdiffstats
path: root/src/estimation-scripts/Sample.rb
diff options
context:
space:
mode:
authorMichele Calgaro <michele.calgaro@yahoo.it>2025-03-02 18:37:22 +0900
committerMichele Calgaro <michele.calgaro@yahoo.it>2025-03-06 12:43:06 +0900
commitbb099158e6c9fd0f1c2771cb9350d3b0a0b51a27 (patch)
tree598156ab1f71ee857c8e1f233f0292e13f36fa28 /src/estimation-scripts/Sample.rb
parent7271fa27ac1881beb5326ca8c9c83471695d8487 (diff)
downloadktorrent-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/Sample.rb')
-rw-r--r--src/estimation-scripts/Sample.rb64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/estimation-scripts/Sample.rb b/src/estimation-scripts/Sample.rb
new file mode 100644
index 0000000..b0c38c2
--- /dev/null
+++ b/src/estimation-scripts/Sample.rb
@@ -0,0 +1,64 @@
+
+class Sample
+
+ attr_reader :time, :speed, :bytesDownloaded, :bytesLeft, :peersTotal
+
+ def Sample.averageSpeed(sample1, sample2)
+ if sample2.time - sample1.time > 0
+ return (sample1.bytesLeft - sample2.bytesLeft).to_f / (sample2.time - sample1.time).to_f
+ else
+ return sample1.speed
+ end
+ end
+
+ def <=>(other)
+ @time <=> other.time
+ end
+
+ # parses a single sample from a line. Format is
+ #
+ # \<tt>timestamp,speed,bytesDownloaded,bytesLeft,peersTotal</tt>
+ #
+ # where
+ # - timestamp is in seconds since epoch (Integer)
+ # - speed is bytes/seconds as Integer
+ # - bytesDownloaded, bytesLeft are bytes as Integer
+ # - peersTotal is the number of available peers (both seeders and leecher, both
+ # connected and not connected to us)
+
+ def Sample.parse(line)
+
+ splitted = line.split(",")
+
+ # TODO: do better error checking
+ return nil if splitted.length != 5
+
+ time = splitted[0].to_i
+ speed = splitted[1].to_i
+ bytesDownloaded = splitted[2].to_i
+ bytesLeft = splitted[3].to_i
+ peersTotal = splitted[4].to_i
+ return Sample.new(time, speed, bytesDownloaded, bytesLeft, peersTotal)
+ end
+
+ # parses samples from a text file, with one sample per line
+ def Sample.parseFromFile(filename)
+ samples = Hash.new
+
+ input = File.open(filename)
+ input.each_line do |line|
+ s = Sample.parse(line)
+ samples[s.time] = s unless s == nil
+ end
+ input.close
+ return samples
+ end
+
+ def initialize(time, speed, bytesDownloaded, bytesLeft, peersTotal)
+ @time = time
+ @speed = speed
+ @bytesDownloaded = bytesDownloaded
+ @bytesLeft = bytesLeft
+ @peersTotal = peersTotal
+ end
+end