Skip to content

Instantly share code, notes, and snippets.

@playpauseandstop
Created October 26, 2012 09:30
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save playpauseandstop/3957836 to your computer and use it in GitHub Desktop.
Save playpauseandstop/3957836 to your computer and use it in GitHub Desktop.
CouchBase vs. Memcached vs. Redis vs. Redis Pipelined
import eventlet
eventlet.monkey_patch()
import os
import time
from random import choice, randint
from string import digits, letters
import pylibmc
from couchbase import Couchbase
from redis import Redis
CONNECTIONS = {
'couchbase': lambda: couchbase_connection(),
'memcached': lambda: memcached_connection(),
'redis': lambda: Redis(),
'redis-pipe': lambda: Redis(unix_socket_path='/tmp/redis.sock'),
}
DRIVER = os.environ.get('DRIVER', 'redis')
THREADS = int(os.environ.get('THREADS', 1000))
def benchmark(thread):
conn = CONNECTIONS[DRIVER]()
number = randint(100, 999)
start = time.time()
for _ in xrange(number):
key = 'key-{:d}-{:d}'.format(thread, number)
value = random_string()
if DRIVER == 'redis-pipe':
pipe = conn.pipeline()
pipe.set(key, value)
pipe.get(key)
pipe.delete(key)
got_value = pipe.execute()[1]
else:
conn.set(*set_args(key, value))
got_value = get_value(conn.get(key))
conn.delete(key)
assert value == got_value, '{!r} != {!r}'.format(value, got_value)
return time.time() - start, number * 3
def couchbase_connection():
connection = Couchbase('localhost:8091', 'admin', 'admin')
return connection['default']
def get_value(value):
if DRIVER == 'couchbase':
return value[-1]
return value
def memcached_connection():
return pylibmc.Client(
['127.0.0.1'],
behaviors={'ketama': True, 'tcp_nodelay': True},
binary=True
)
def random_string(length=None):
length = length or 256
return u''.join((choice(digits + letters) for _ in xrange(length)))
def set_args(key, value):
if DRIVER == 'couchbase':
return (key, 0, 0, value)
return (key, value)
if __name__ == '__main__':
pool = eventlet.GreenPool(THREADS)
start = time.time()
timers = []
total = 0
for timer, counter in pool.imap(benchmark, range(THREADS)):
print('{} operations completed in {:.4f} s'.format(counter, timer))
timers.append(timer)
total += counter
timer = time.time() - start
print
print('In total, operations: {}, time: {:.4f} s'.format(total, timer))
print('Requests pre second: {:.0f}'.format(total / timer))
print('Min: {:.4f}, max: {:.4f}'.format(min(timers), max(timers)))
$ DRIVER=couchbase THREADS=10 make benchmark
env/bin/python ./benchmark.py
324 operations completed in 2.1607 s
2256 operations completed in 10.7558 s
1068 operations completed in 5.7570 s
2037 operations completed in 9.5393 s
2493 operations completed in 11.0871 s
2910 operations completed in 12.1000 s
2961 operations completed in 12.1318 s
2193 operations completed in 10.3703 s
582 operations completed in 3.3844 s
714 operations completed in 4.2700 s
In total, operations: 17538, time: 17.0281 s
Operations per second: 1030
Min: 2.1607, max: 12.1318
$ DRIVER=memcached THREADS=10 make benchmark
env/bin/python ./benchmark.py
1188 operations completed in 0.2297 s
2076 operations completed in 0.3998 s
2712 operations completed in 0.5299 s
447 operations completed in 0.0839 s
384 operations completed in 0.0735 s
1203 operations completed in 0.2275 s
2406 operations completed in 0.4798 s
1185 operations completed in 0.2804 s
2661 operations completed in 0.6639 s
2049 operations completed in 0.3964 s
In total, operations: 16311, time: 3.3722 s
Operations per second: 4837
Min: 0.0735, max: 0.6639
$ DRIVER=redis THREADS=10 make benchmark
env/bin/python ./benchmark.py
1407 operations completed in 3.1365 s
2745 operations completed in 4.9258 s
2214 operations completed in 4.3205 s
2769 operations completed in 4.9632 s
2688 operations completed in 4.8802 s
435 operations completed in 1.1270 s
2778 operations completed in 4.9811 s
780 operations completed in 1.9118 s
1233 operations completed in 2.8041 s
2964 operations completed in 5.1189 s
In total, operations: 20013, time: 5.1346 s
Operations per second: 3898
Min: 1.1270, max: 5.1189
$ DRIVER=redis-pipe THREADS=10 make benchmark
env/bin/python ./benchmark.py
1752 operations completed in 2.4244 s
1509 operations completed in 2.2151 s
2484 operations completed in 2.9213 s
1257 operations completed in 1.9654 s
2919 operations completed in 3.1169 s
2850 operations completed in 3.3568 s
891 operations completed in 1.5258 s
477 operations completed in 0.8401 s
678 operations completed in 1.4902 s
2793 operations completed in 3.5280 s
In total, operations: 17610, time: 3.5362 s
Operations per second: 4980
Min: 0.8401, max: 3.5280
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment