summaryrefslogtreecommitdiffstats
path: root/estimation-scripts/Sample.rb
blob: b0c38c2a9db7b177529594c0f916ebc68be5fe69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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