Skip to content

Instantly share code, notes, and snippets.

@jbalogh
Created September 15, 2010 20:39
Show Gist options
  • Save jbalogh/581441 to your computer and use it in GitHub Desktop.
Save jbalogh/581441 to your computer and use it in GitHub Desktop.
crush.py generates a lot of load for redis
#!/usr/bin/env python
"""
./crush.py [host:port]
Runs a bunch of SADD and DELETE commands against Redis to see how it handles
high load. Do `./crush.py &` a bunch of times to generate a lot of
connections.
"""
import hashlib
import random
import sys
import threading
import time
import uuid
from redis import Redis
host = port = None
if len(sys.argv) == 2:
host, port = sys.argv[1].split(':')
redis = Redis(host or 'localhost', port and int(port) or 6379)
keys = [hashlib.md5(str(uuid.uuid4())).hexdigest()
for _ in xrange(10000)]
def timeit(f):
def dec():
start = time.time()
f()
return time.time() - start
return dec
@timeit
def sadd():
# Set a few keys in a bunch of sets.
pipe = redis.pipeline(transaction=False)
new = random.sample(keys, 10)
for key in random.sample(keys, 100):
for k in new:
pipe.sadd(key, k)
pipe.execute()
@timeit
def union():
# Do a few unions.
for x in range(10):
redis.sunion(random.sample(keys, 20))
@timeit
def delete():
redis.delete(*random.sample(keys, 50))
print '^C to kill it'
stayin_alive = True
def crusher():
count = a = u = d = 0
start = time.time()
while stayin_alive:
count += 1
if count % 50 == 0:
print count, '%.2f' % (time.time() - start)
print 'add: %.2f; union: %.2f; delete: %.2f' % (a, u, d)
a = u = d = 0
start = time.time()
a += sadd()
#u += union()
d += delete()
if __name__ == '__main__':
for x in range(10):
print 'Starting thread', x
t = threading.Thread(target=crusher)
t.start()
try:
while 1:
time.sleep(1000)
except KeyboardInterrupt:
print '\nKilling off threads...'
stayin_alive = False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment