-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiming.py
58 lines (51 loc) · 1.83 KB
/
timing.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
import time
import logging
import inspect
from django.core.mail import mail_admins
from django.core.cache import cache
from django.conf import settings
logger = logging.getLogger(__name__)
class GhettoTimer(object):
"""
This is a decoartor that provides timing at a function level.
"""
def __init__(self, orig_func):
self.__name__ = 'GhettoTimer'
self.orig_func = orig_func
def __call__(self, *args, **kwargs):
start_time = time.time()
response = self.orig_func(*args, **kwargs)
delta = time.time()-start_time
holy_shit = '\n=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=\
!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!\n'
msg = "Time to execute %s (%s): %s seconds" % (self.orig_func.__name__,
inspect.getsourcefile(self.orig_func),
round(delta, 4))
if delta > 1:
logger.critical('%s%s%s' % (holy_shit, msg, holy_shit))
if cache.get('has_it_been_a_minute_yet') == None and settings.DEBUG == False:
mail_admins('%s is slow as f*ck' % self.orig_func.__name__,
msg,
fail_silently=True)
cache.set('has_it_been_a_minute_yet', True, 60)
elif delta > 0.1:
logger.warning('%s%s%s' % (holy_shit, msg, holy_shit))
elif delta > 0.01:
logger.info(msg)
return response
#Use or extend GhettoTimer class
#tt = None
#def timer(desc = '', begin = False):
# return None
# global tt
# if begin:
# tt = None
# if not tt:
# tt = time()
# lapse = 0
# else:
# last = tt
# tt = time()
# lapse = tt - last
# print "%s %s" % (desc, lapse)
# return lapse