forked from Tribler/dispersy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatistics.py
393 lines (298 loc) · 13 KB
/
statistics.py
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
from abc import ABCMeta, abstractmethod
from collections import defaultdict
from threading import RLock
from time import time
class Statistics(object):
__metaclass__ = ABCMeta
def __init__(self):
self._lock = RLock()
def dict_inc(self, dictionary, key, value=1):
with self._lock:
assert hasattr(self, dictionary), u"%s doesn't exist in statistics" % dictionary
if getattr(self, dictionary) is not None:
getattr(self, dictionary)[key] += value
def get_dict(self):
"""
Returns a deep clone of SELF as a dictionary.
Warning: there is no recursion protection, if SELF contains self-references it will hang.
"""
def clone(o):
if isinstance(o, Statistics):
return dict((key, clone(value))
for key, value
in o.__dict__.items()
if not key.startswith("_"))
if isinstance(o, dict):
return dict((clone(key), clone(value))
for key, value
in o.items())
if isinstance(o, tuple):
return tuple(clone(value) for value in o)
if isinstance(o, list):
return [clone(value) for value in o]
return o
return clone(self)
@abstractmethod
def update(self):
pass
class MessageStatistics(object):
def __init__(self):
super(MessageStatistics, self).__init__()
self._lock = RLock()
self.total_received_count = 0
self.success_count = 0
self.drop_count = 0
self.created_count = 0
self.outgoing_count = 0
self.delay_received_count = 0
self.delay_send_count = 0
self.delay_timeout_count = 0
self.delay_success_count = 0
self.success_dict = None
self.drop_dict = None
self.created_dict = None
self.delay_dict = None
self.outgoing_dict = None
self.walk_attempt_count = 0
self.walk_success_count = 0
self.walk_failure_count = 0
self.walk_failure_dict = None
self.invalid_response_identifier_count = 0
self.incoming_intro_count = 0
self.incoming_intro_dict = None
self.outgoing_intro_count = 0
self.outgoing_intro_dict = None
self._enabled = None
def increase_count(self, category, name, value=1):
with self._lock:
count_name = u"%s_count" % category
dict_name = u"%s_dict" % category
if hasattr(self, count_name):
setattr(self, count_name, getattr(self, count_name) + value)
if getattr(self, dict_name) is not None:
getattr(self, dict_name)[name] += value
def increase_delay_count(self, category, value=1):
with self._lock:
count_name = u"delay_%s_count" % category
setattr(self, count_name, getattr(self, count_name) + value)
def enable(self, enabled):
with self._lock:
if self._enabled != enabled:
self._enabled = enabled
assigned_value = lambda: defaultdict(int) if enabled else None
self.success_dict = assigned_value()
self.outgoing_dict = assigned_value()
self.created_dict = assigned_value()
self.drop_dict = assigned_value()
self.delay_dict = assigned_value()
self.walk_failure_dict = assigned_value()
self.incoming_intro_dict = assigned_value()
self.outgoing_intro_dict = assigned_value()
def reset(self):
with self._lock:
self.total_received_count = 0
self.success_count = 0
self.drop_count = 0
self.created_count = 0
self.outgoing_count = 0
self.delay_received_count = 0
self.delay_send_count = 0
self.delay_timeout_count = 0
self.delay_success_count = 0
self.walk_attempt_count = 0
self.walk_success_count = 0
self.walk_failure_count = 0
self.invalid_response_identifier_count = 0
self.incoming_intro_count = 0
self.outgoing_intro_count = 0
if self._enabled:
self.success_dict.clear()
self.drop_dict.clear()
self.created_dict.clear()
self.delay_dict.clear()
self.outgoing_dict.clear()
self.walk_failure_dict.clear()
self.incoming_intro_dict.clear()
self.outgoing_intro_dict.clear()
class DispersyStatistics(Statistics):
def __init__(self, dispersy):
super(DispersyStatistics, self).__init__()
self._dispersy = dispersy
self.communities = None
self.start = self.timestamp = time()
# nr of bytes up/down and packets send/received as reported by endpoint
self.total_down = 0
self.total_up = 0
self.total_send = 0
self.total_received = 0
# size of the sendqueue
self.cur_sendqueue = 0
# nr of candidates introduced/stumbled upon
self.total_candidates_discovered = 0
# walk statistics
self.walk_attempt_count = 0
self.walk_success_count = 0
self.walk_failure_count = 0
self.walk_failure_dict = None
self.invalid_response_identifier_count = 0
self.incoming_intro_count = 0
self.incoming_intro_dict = None
self.outgoing_intro_count = 0
self.outgoing_intro_dict = None
self.attachment = None
self.endpoint_recv = None
self.endpoint_send = None
self.received_introductions = None
# list with {count=int, duration=float, average=float, entry=str} dictionaries. each entry
# represents a key from the attach_runtime_statistics decorator
self.runtime = None
self._enabled = None
self.msg_statistics = MessageStatistics()
self.enable_debug_statistics(__debug__)
self.update()
@property
def database_version(self):
return self._dispersy.database.database_version
@property
def lan_address(self):
return self._dispersy.lan_address
@property
def wan_address(self):
return self._dispersy.wan_address
@property
def connection_type(self):
return self._dispersy.connection_type
def enable_debug_statistics(self, enable):
if self._enabled != enable:
self._enabled = enable
self.msg_statistics.enable(enable)
dict_assigned_value = lambda: defaultdict(int) if enable else None
self.walk_failure_dict = dict_assigned_value()
self.incoming_intro_dict = dict_assigned_value()
self.outgoing_intro_dict = dict_assigned_value()
self.attachment = dict_assigned_value()
self.endpoint_recv = dict_assigned_value()
self.endpoint_send = dict_assigned_value()
# SOURCE:INTRODUCED:COUNT nested dictionary
self.received_introductions = defaultdict(lambda: defaultdict(int)) if enable else None
for community in self._dispersy.get_communities():
community.statistics.enable_debug_statistics(enable)
def are_debug_statistics_enabled(self):
return self._enabled
def update(self, database=False):
self.timestamp = time()
self.communities = [community.statistics for community in self._dispersy.get_communities()]
for community in self.communities:
community.update(database=database)
# list with {count=int, duration=float, average=float, entry=str} dictionaries. each entry
# represents a key from the attach_runtime_statistics decorator
self.runtime = [(statistic.duration, statistic.get_dict(entry=entry)) for entry, statistic in _runtime_statistics.iteritems() if statistic.duration > 1]
self.runtime.sort(reverse=True)
self.runtime = [statistic[1] for statistic in self.runtime]
def reset(self):
self.total_down = 0
self.total_up = 0
self.total_send = 0
self.total_received = 0
self.cur_sendqueue = 0
self.start = self.timestamp = time()
# walk statistics
self.walk_attempt_count = 0
self.walk_success_count = 0
self.walk_failure_count = 0
self.walk_failure_dict = None
self.invalid_response_identifier_count = 0
self.incoming_intro_count = 0
self.incoming_intro_dict = None
self.outgoing_intro_count = 0
self.outgoing_intro_dict = None
self.msg_statistics.reset()
if self.are_debug_statistics_enabled():
self.walk_failure_dict = defaultdict(int)
self.incoming_intro_dict = defaultdict(int)
self.outgoing_intro_dict = defaultdict(int)
self.attachment = defaultdict(int)
self.endpoint_recv = defaultdict(int)
self.endpoint_send = defaultdict(int)
self.received_introductions = defaultdict(lambda: defaultdict(int))
class CommunityStatistics(Statistics):
def __init__(self, community):
super(CommunityStatistics, self).__init__()
self._dispersy = community.dispersy
self._community = community
self.database_id = community.database_id
self.classification = community.get_classification()
self.cid = community.cid
self.mid = community.my_member.mid
self.hex_cid = community.cid.encode("HEX")
self.hex_mid = community.my_member.mid.encode("HEX")
self.database = dict()
self.total_candidates_discovered = 0
self.msg_statistics = MessageStatistics()
self.sync_bloom_new = 0
self.sync_bloom_reuse = 0
self.sync_bloom_send = 0
self.sync_bloom_skip = 0
self.dispersy_acceptable_global_time_range = self._community.dispersy_acceptable_global_time_range
self.dispersy_enable_candidate_walker = self._community.dispersy_enable_candidate_walker
self.dispersy_enable_candidate_walker_responses = self._community.dispersy_enable_candidate_walker_responses
self.enable_debug_statistics(self._dispersy.statistics.are_debug_statistics_enabled())
def increase_total_received_count(self, value):
self.msg_statistics.total_received_count += value
def increase_discovered_candidates(self, value=1):
self.total_candidates_discovered += value
self._dispersy.statistics.total_candidates_discovered += value
def increase_msg_count(self, category, name, value=1):
self.msg_statistics.increase_count(category, name, value)
self._dispersy.statistics.msg_statistics.increase_count(category, name, value)
def increase_delay_msg_count(self, category, value=1):
self.msg_statistics.increase_delay_count(category, value)
self._dispersy.statistics.msg_statistics.increase_delay_count(category, value)
@property
def acceptable_global_time(self):
return self._community.acceptable_global_time
@property
def global_time(self):
return self._community.global_time
@property
def candidates(self):
now = time()
return [(candidate.lan_address, candidate.wan_address, candidate.global_time,
candidate.get_member().mid if candidate.get_member() else None)
for candidate in self._community.candidates.itervalues()
if candidate.get_category(now) in [u'walk', u'stumble', u'intro']]
def enable_debug_statistics(self, enabled):
self.msg_statistics.enable(enabled)
def update(self, database=False):
if database:
self.database = dict(self._community.dispersy.database.execute(u"SELECT meta_message.name, COUNT(sync.id) FROM sync JOIN meta_message ON meta_message.id = sync.meta_message WHERE sync.community = ? GROUP BY sync.meta_message", (self._community.database_id,)))
else:
self.database = dict()
def reset(self):
self.total_candidates_discovered = 0
self.msg_statistics.reset()
class RuntimeStatistic(object):
def __init__(self):
self._count = 0
self._duration = 0.0
@property
def count(self):
" Returns the number of times a method was called. "
return self._count
@property
def duration(self):
" Returns the cumulative time spent in a method. "
return self._duration
@property
def average(self):
" Returns the average time spent in a method. "
return self._duration / self._count
def increment(self, duration):
" Increase self.count with 1 and self.duration with DURATION. "
assert isinstance(duration, float), type(duration)
self._duration += duration
self._count += 1
def get_dict(self, **kargs):
" Returns a dictionary with the statistics. "
return dict(count=self.count, duration=self.duration, average=self.average, **kargs)
_runtime_statistics = defaultdict(RuntimeStatistic)