-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatist.py
More file actions
76 lines (56 loc) · 1.98 KB
/
statist.py
File metadata and controls
76 lines (56 loc) · 1.98 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
import time
class Statist(object):
def __init__(self):
self.all_repo = 0
self.all_user = 0
self.start_time = time.time()
self.last_fail_time = 0
self.repos_from_fail = 0
self.user_from_fail = 0
self.req_from_fail = 0
self.fail_cnt = 0
self.max_fail_interval = 0
self.min_fail_interval = 0
self._sleep_time = 0
self.req_count = 0
def increase_repos(self, num=1):
self.all_repo += num
self.repos_from_fail += num
def increase_user(self, num=1):
self.all_user += num
self.user_from_fail += num
def increase_req(self, num=1):
self.req_count += num
self.req_from_fail += num
def get_avg_speed(self):
now = time.time()
cost = now - self.start_time
result = (self.all_repo / cost, self.all_user / cost, self.req_count / cost)
return result
def get_recent_speed(self, sleep_during=90):
if self.last_fail_time == 0:
return self.get_avg_speed()
now = time.time()
cost = now - (self.last_fail_time + self._sleep_time)
result = (self.repos_from_fail / cost, self.user_from_fail / cost, self.req_count / cost)
return result
def get_cost_time(self):
now = time.time()
cost = now - self.start_time
return cost
def record_error(self, sleep_time=0):
self._sleep_time = sleep_time
now = time.time()
interval = now - self.last_fail_time
self.max_fail_interval = max(interval, self.max_fail_interval)
self.min_fail_interval = min(interval, self.min_fail_interval)
self.repos_from_fail = 0
self.user_from_fail = 0
self.req_from_fail = 0
self.fail_cnt += 1
self.last_fail_time = now
def test():
statist = Statist()
statist.get_avg_speed()
if __name__ == "__main__":
test()