Created
January 30, 2014 22:04
-
-
Save kjoconnor/8720947 to your computer and use it in GitHub Desktop.
Find what percentage of memcache keys don't have expiries set
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import telnetlib | |
import time | |
from collections import Counter | |
hosts = [ | |
('localhost', 11211) | |
] | |
def get_keys(host, port, sample_ratio=.01, re_key_match=None): | |
t = telnetlib.Telnet(host, port) | |
t.write('stats items\n') | |
items = t.read_until('END').split('\r\n') | |
slabs = set([]) | |
slab_info = {} | |
expiries = Counter() | |
item_count = 0 | |
filtered_count = 0 | |
lowest_timestamp = int(time.mktime(time.gmtime())) + 10000 | |
for item in items: | |
parts = item.split(':') | |
if len(parts) == 3: | |
slab_id = int(parts[1]) | |
slabs.add(slab_id) | |
if parts[2].split(' ')[0] == 'number': | |
slab_info[slab_id] = {} | |
slab_info[slab_id]['items'] = int(parts[2].split(' ')[1]) | |
for slab in slabs: | |
sample_count = int(round(slab_info[slab]['items'] * sample_ratio)) | |
t.write('stats cachedump {0} {1}\n'.format(slab, sample_count)) | |
cache_lines = t.read_until('END').split('\r\n') | |
for line in cache_lines: | |
cache_parts = line.split(' ') | |
if cache_parts[0] == 'ITEM' and len(cache_parts) == 6: | |
if int(cache_parts[4]) < lowest_timestamp: | |
lowest_timestamp = int(cache_parts[4]) | |
if re_key_match: | |
if re_key_match.match(cache_parts[1]): | |
item_count += 1 | |
expiries[cache_parts[4]] += 1 | |
else: | |
filtered_count += 1 | |
else: | |
item_count += 1 | |
# Counter for expiries | |
expiries[cache_parts[4]] += 1 | |
print 'lowest_timestamp: %s' % lowest_timestamp | |
no_expiry_count = expiries[str(lowest_timestamp)] | |
expiry_ratio = (no_expiry_count / float(item_count)) * 100.0 | |
if filtered_count > 0: | |
print 'Filtered {0} keys due to regex fails'.format(filtered_count) | |
print 'Sample ratio of {0}% shows {1} ({2}%) of {3} sampled keys have '\ | |
'infinite expiries set'.format( | |
sample_ratio * 100, | |
no_expiry_count, | |
expiry_ratio, | |
item_count | |
) | |
print 'Most Common:' | |
print expiries.most_common(20) | |
t.close() | |
for host in hosts: | |
print 'Host: %s' % host[0] | |
# Match keys via an compiled re object | |
get_keys( | |
host[0], host[1], | |
re_key_match=re.compile('^mykeys-.+$') | |
) | |
# No key matching | |
# get_keys( | |
# host[0], | |
# host[1] | |
# ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment