Created
June 12, 2012 07:16
-
-
Save marconi/2915849 to your computer and use it in GitHub Desktop.
Redis index by RPUSH test
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 redis | |
import gevent | |
import itertools | |
from gevent import monkey | |
monkey.patch_socket() | |
QUEUE_NAME = 'queue' | |
redis_db = redis.StrictRedis() | |
redis_db.delete(QUEUE_NAME) | |
def atomic_push(db, name, item): | |
pipe = db.pipeline() | |
pipe.rpush(name, item) | |
pipe.llen(name) | |
_, len = pipe.execute() | |
return len - 1 | |
def test_push(name, subjects): | |
db = redis.StrictRedis() | |
results = {} | |
for item in subjects: | |
results[item] = atomic_push(db, name, item) | |
return results | |
subjects1 = ['u', 'v', 'w', 'x', 'y', 'z'] | |
subjects2 = ['k', 'l', 'm', 'n', 'a', 'b', 'c', 'd', 'e', 'f'] | |
subjects3 = ['o', 'p', 'q', 'r', 's', 't', 'g', 'h', 'i', 'j'] | |
jobs = [gevent.spawn(test_push, QUEUE_NAME, subjects1), | |
gevent.spawn(test_push, QUEUE_NAME, subjects2), | |
gevent.spawn(test_push, QUEUE_NAME, subjects3)] | |
gevent.joinall(jobs) | |
results = {} | |
for job in jobs: | |
results.update(job.value) | |
print "Total subjects: %s" % str(len(subjects1) + len(subjects2) + len(subjects3)) | |
print "Total results: %s" % str(len(results.items())) | |
right = 0 | |
wrong = 0 | |
for i, val in enumerate(redis_db.lrange(QUEUE_NAME, 0, -1)): | |
if results[val] == i: | |
right += 1 | |
else: | |
wrong += 1 | |
print "Total right: %s" % str(right) | |
print "Total wrong: %s" % str(wrong) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment