Skip to content

Instantly share code, notes, and snippets.

@zheplusplus
Created January 16, 2015 06:58
Show Gist options
  • Save zheplusplus/2e33e253c0b877baa5c0 to your computer and use it in GitHub Desktop.
Save zheplusplus/2e33e253c0b877baa5c0 to your computer and use it in GitHub Desktop.
Redis profiling script (random keys)
import sys
import uuid
import random
from datetime import datetime
import redis
prefix = str(uuid.uuid4())
groups = 20
run_per_group = 1000
total = groups * run_per_group
rand_range = 20000
host = sys.argv[1]
port = int(sys.argv[2])
def randint():
return random.randint(0, rand_range - 1)
def run_multiple(f):
def wrapper():
for _ in xrange(groups):
r = redis.Redis(host=host, port=port)
for __ in xrange(run_per_group):
f(r, randint())
return wrapper
@run_multiple
def keys_rw(r, rn):
r.set('%s-set-%d' % (prefix, rn), 'S-%d' % randint())
r.setnx('%s-set-%d' % (prefix, rn), 'SN-%d' % randint())
r.incr('%s-incr-%d' % (prefix, rn))
r.get('%s-set-%d' % (prefix, rn))
r.get('%s-incr-%d' % (prefix, rn))
@run_multiple
def list_test(r, rn):
list_key = '%s-list-%d' % (prefix, rn)
for _ in xrange(4):
r.lpush(list_key, 'L-%d' % randint())
r.llen(list_key)
r.lrange(list_key, 0, 4)
@run_multiple
def hash_test(r, rn):
hash_key = '%s-hash-%d' % (prefix, rn)
for _ in xrange(3):
r.hset(hash_key, 'S-%d' % randint(), 'H-%d' % randint())
for _ in xrange(3):
r.hincrby(hash_key, 'I-%d' % randint())
r.hlen(hash_key)
def clear_data():
r = redis.Redis(host=host, port=port)
for i in xrange(rand_range):
r.delete(['%s-%s-%d' % (prefix, mid, i)
for mid in ['incr', 'set', 'list', 'hash']])
if __name__ == '__main__':
start = datetime.now()
keys_rw()
end = datetime.now()
print '%s,%s,%d,%s,%s,%f' % (prefix, 'keys', total, start, end, (end - start).total_seconds())
start = datetime.now()
list_test()
end = datetime.now()
print '%s,%s,%d,%s,%s,%f' % (prefix, 'list', total, start, end, (end - start).total_seconds())
start = datetime.now()
hash_test()
end = datetime.now()
print '%s,%s,%d,%s,%s,%f' % (prefix, 'hash', total, start, end, (end - start).total_seconds())
clear_data()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment