Skip to content

Instantly share code, notes, and snippets.

@williamsjj
Created November 30, 2010 23:44
Show Gist options
  • Save williamsjj/722653 to your computer and use it in GitHub Desktop.
Save williamsjj/722653 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import multiprocessing as mp
import redis, os, time, uuid
WORKERS = 20
WORK_ATTEMPTS = 20
total_doc_list = []
def test_writes():
server = redis.Redis(host="localhost", port=6379, db=1)
doc_list = []
for i in range(WORK_ATTEMPTS):
_id = str(uuid.uuid4()) + "-" + str(os.getpid())
test_doc = {"enabled": True,
"co_name" : "Test Company",
"admin_groups" : ["group-1", "group-2"],
"received_access" : {"digitar.com" : {"managed_ns_group" : "it_admins",
"manager_ns_group" : "admins"}},
"delegated_access" : ["bobbledy.com", "virtualit.com"]}
# Test a new write
server.set(_id, test_doc)
doc_list.append(_id)
# Test update
test_doc["_id"] = _id
test_doc["admin_groups"] = ["group-3"]
server.set(_id, test_doc)
return doc_list
def test_reads(doc_list):
server = redis.Redis(host="localhost", port=6379)
for i in range(WORK_ATTEMPTS):
server.get(_id)
def update_write_time(result):
global total_doc_list
total_doc_list.append(result)
if __name__ == "__main__":
server = redis.Redis(host="localhost", port=6379)
# Bench writes
write_time = time.time()
pool = mp.Pool(processes=WORKERS)
for i in range(WORKERS):
pool.apply_async(test_writes, callback=update_write_time)
pool.close()
pool.join()
print "Write Time: %fs" % (time.time() - write_time)
# Bench reads
read_time = time.time()
pool = mp.Pool(processes=WORKERS)
for i in range(WORKERS):
pool.apply_async(test_reads, [total_doc_list[i]])
pool.close()
pool.join()
print "Read Time: %fs" % (time.time() - read_time)
server.flushdb()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment