-
Notifications
You must be signed in to change notification settings - Fork 0
/
timed.py
53 lines (46 loc) · 1.63 KB
/
timed.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
from functools import wraps
from time import time,sleep
import tornado.ioloop
class timed_avg(object):
def __init__(self, n=1):
self.elapsed = 0.0
self.count = 0
self.n = n
def __call__(self, f):
def wrapped(*args, **kwds):
start = tornado.ioloop.IOLoop.current().time()
result = f(*args, **kwds)
self.elapsed += tornado.ioloop.IOLoop.current().time() - start
self.count += 1
if self.count >= self.n:
print( "%s took %f s on average for %d calls" % (f.__name__, self.elapsed / self.count, self.count) )
self.elapsed = 0.0
self.count = 0
return result
return wrapped
class timed_avg_and_freq(object):
def __init__(self, n=1):
self.elapsed = 0.0
self.freq_start = None
self.count = 0
self.n = n
def __call__(self, f):
def wrapped(*args, **kwds):
start = tornado.ioloop.IOLoop.current().time()
if not self.freq_start:
self.freq_start = start
result = f(*args, **kwds)
now = tornado.ioloop.IOLoop.current().time()
self.elapsed += now - start
self.count += 1
if self.count >= self.n:
freq_elapsed = now - self.freq_start
print( "%s took %f s on average for %d calls (called every %f s) %.2f%% active" % (f.__name__, self.elapsed / self.count, self.count, freq_elapsed / self.count, 100.0*self.elapsed/freq_elapsed) )
self.elapsed = 0.0
self.count = 0
self.freq_start = start
return result
return wrapped
#@timed_avg(2)
#def fun(i):
# sleep(i)