Skip to content

Instantly share code, notes, and snippets.

@pbabik
Created November 3, 2015 20:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pbabik/2b4303c932997c5cbb3d to your computer and use it in GitHub Desktop.
Save pbabik/2b4303c932997c5cbb3d to your computer and use it in GitHub Desktop.
Key-value stores battle: Redis versus UnQLite
#!/usr/bin/python
# -*- coding: utf-8 -*-
import redis
import random
import json
import time
import hashlib
from unqlite import UnQLite
scores = {'UnQLite':0,'Redis':0}
print 'Preparing data...'
def seed():
kvpairs = {}
for i in range(65536):
md = hashlib.md5(str(i)).hexdigest()
k = 'test_'+md
v = json.dumps({'md':md,'ts':time.time(),'seq':i,'rand':random.random()})
kvpairs[k] = v
return kvpairs
test_data = seed()
test_keys = test_data.keys()
print 'Round one: inserting 65536 key-value-pairs'
print 'Fight!'
def redis_round_1():
start_time = time.time()
r = redis.StrictRedis(host='localhost', db=6)
for k,v in test_data.iteritems():
r.set(k,v)
end_time = time.time()
total_time = end_time - start_time
scores['Redis'] += total_time
print 'Redis: %s' % str(total_time)
redis_round_1()
def unqlite_round_1():
start_time = time.time()
db = UnQLite('/tmp/battle.udb')
for k,v in test_data.iteritems():
db[k] = v
end_time = time.time()
total_time = end_time - start_time
scores['UnQLite'] += total_time
print 'UnQLite: %s' % str(total_time)
unqlite_round_1()
print 'Round two: fetching random key from DB 32768 times'
print 'Fight!'
def redis_round_2():
start_time = time.time()
r = redis.StrictRedis(host='localhost', db=6)
for i in range(32768):
random_key = random.choice(test_keys)
data = json.loads(r.get(random_key))
end_time = time.time()
total_time = end_time - start_time
scores['Redis'] += total_time
print 'Redis: %s' % str(total_time)
redis_round_2()
def unqlite_round_2():
start_time = time.time()
db = UnQLite('/tmp/battle.udb')
for i in range(32768):
random_key = random.choice(test_keys)
data = json.loads(db[random_key])
end_time = time.time()
total_time = end_time - start_time
scores['UnQLite'] += total_time
print 'UnQLite: %s' % str(total_time)
unqlite_round_2()
print 'Round three: deleting random key from DB 32768 times'
print 'Fight!'
def redis_round_3():
start_time = time.time()
r = redis.StrictRedis(host='localhost', db=6)
keys_copy = random.sample(test_keys,32768)
for k in keys_copy:
r.delete(k)
end_time = time.time()
total_time = end_time - start_time
scores['Redis'] += total_time
print 'Redis: %s' % str(total_time)
redis_round_3()
def unqlite_round_3():
start_time = time.time()
db = UnQLite('/tmp/battle.udb')
keys_copy = random.sample(test_keys,32768)
for k in keys_copy:
db.delete(k)
end_time = time.time()
total_time = end_time - start_time
scores['UnQLite'] += total_time
print 'UnQLite: %s' % str(total_time)
unqlite_round_3()
def teardown():
r = redis.StrictRedis(host='localhost', db=6)
r.flushdb()
import os
os.unlink('/tmp/battle.udb')
teardown()
if scores['UnQLite'] < scores['Redis']:
print 'UnQLite wins'
else:
print 'Redis wins'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment