diff options
| author | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-01-20 02:37:40 +0000 |
|---|---|---|
| committer | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-01-20 02:37:40 +0000 |
| commit | 9ad5c7b5e23b4940e7a3ea3ca3a6fb77e6a8fab0 (patch) | |
| tree | d088b5210e77d9fa91d954d8550e00e372b47378 /estimation-scripts/Sample.rb | |
| download | ktorrent-9ad5c7b5.tar.gz ktorrent-9ad5c7b5.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/Sample.rb')
| -rw-r--r-- | estimation-scripts/Sample.rb | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/estimation-scripts/Sample.rb b/estimation-scripts/Sample.rb new file mode 100644 index 0000000..b0c38c2 --- /dev/null +++ b/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 |
