Skip to content

Commit 04582e2

Browse files
committed
Added CacheRealTimeModule
1 parent e04ec44 commit 04582e2

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

devserver/modules/cache.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class CacheSummaryModule(DevServerModule):
77
"""
88
Outputs a summary of cache events once a response is ready.
99
"""
10+
real_time = False
1011

1112
logger_name = 'cache'
1213

@@ -19,7 +20,7 @@ def process_init(self, request):
1920
self.old = dict((k, getattr(cache, k)) for k in self.attrs_to_track)
2021

2122
for k in self.attrs_to_track:
22-
setattr(cache, k, track(getattr(cache, k), 'cache'))
23+
setattr(cache, k, track(getattr(cache, k), 'cache', self.logger if self.real_time else None))
2324

2425
def process_complete(self, request):
2526
from devserver.utils.stats import stats
@@ -33,13 +34,18 @@ def process_complete(self, request):
3334
else:
3435
ratio = 100
3536

36-
self.logger.info('%(calls)s calls made with a %(ratio)d%% hit percentage (%(misses)s misses)' % dict(
37-
calls=calls,
38-
ratio=ratio,
39-
hits=hits,
40-
misses=misses,
41-
), duration=stats.get_total_time('cache'))
37+
if not self.real_time:
38+
self.logger.info('%(calls)s calls made with a %(ratio)d%% hit percentage (%(misses)s misses)' % dict(
39+
calls=calls,
40+
ratio=ratio,
41+
hits=hits,
42+
misses=misses,
43+
), duration=stats.get_total_time('cache'))
4244

4345
# set our attributes back to their defaults
4446
for k, v in self.old.iteritems():
4547
setattr(cache, k, v)
48+
49+
50+
class CacheRealTimeModule(CacheSummaryModule):
51+
real_time = True

devserver/utils/stats.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def __init__(self, *args, **kwargs):
1616
super(StatCollection, self).__init__(*args, **kwargs)
1717
self.reset()
1818

19-
def run(self, func, key, *args, **kwargs):
19+
def run(self, func, key, logger, *args, **kwargs):
2020
"""Profile a function and store its information."""
2121

2222
start_time = datetime.now()
@@ -50,6 +50,9 @@ def run(self, func, key, *args, **kwargs):
5050
if value is not None:
5151
row['hits'] += 1
5252

53+
if logger:
54+
logger.debug('%s("%s") %s (%s)', func.__name__, args[0], 'Miss' if value is None else 'Hit', row['hits'], duration=this_time)
55+
5356
return value
5457

5558
def reset(self):
@@ -88,12 +91,12 @@ def get_calls(self, key):
8891
stats = StatCollection()
8992

9093

91-
def track(func, key):
94+
def track(func, key, logger):
9295
"""A decorator which handles tracking calls on a function."""
9396
def wrapped(*args, **kwargs):
9497
global stats
9598

96-
return stats.run(func, key, *args, **kwargs)
99+
return stats.run(func, key, logger, *args, **kwargs)
97100
wrapped.__doc__ = func.__doc__
98101
wrapped.__name__ = func.__name__
99102
return wrapped

0 commit comments

Comments
 (0)