Subversion Repositories svnkaklik

Rev

Details | Last modification | View Log

Rev Author Line No. Line
36 kaklik 1
# Written by Bram Cohen
2
# see LICENSE.txt for license information
3
 
4
from clock import clock
5
 
6
class Measure:
7
    def __init__(self, max_rate_period, fudge = 1):
8
        self.max_rate_period = max_rate_period
9
        self.ratesince = clock() - fudge
10
        self.last = self.ratesince
11
        self.rate = 0.0
12
        self.total = 0l
13
 
14
    def update_rate(self, amount):
15
        self.total += amount
16
        t = clock()
17
        self.rate = (self.rate * (self.last - self.ratesince) + 
18
            amount) / (t - self.ratesince + 0.0001)
19
        self.last = t
20
        if self.ratesince < t - self.max_rate_period:
21
            self.ratesince = t - self.max_rate_period
22
 
23
    def get_rate(self):
24
        self.update_rate(0)
25
        return self.rate
26
 
27
    def get_rate_noupdate(self):
28
        return self.rate
29
 
30
    def time_until_rate(self, newrate):
31
        if self.rate <= newrate:
32
            return 0
33
        t = clock() - self.ratesince
34
        return ((self.rate * t) / newrate) - t
35
 
36
    def get_total(self):
37
        return self.total