Skip to content

Instantly share code, notes, and snippets.

@stuntgoat
Created May 13, 2012 05:44
Show Gist options
  • Save stuntgoat/2685207 to your computer and use it in GitHub Desktop.
Save stuntgoat/2685207 to your computer and use it in GitHub Desktop.
zlib and Redis tests with Python
setup_compress = """
for i in xrange(10000):
r.hset(str(i), str(i), zlib.compress(10 * "my name is mud", 1))"""
setup_normal = """
for i in xrange(10000):
r.hset(str(i), str(i), 10 * "my name is mud")"""
setup = """
import os
import redis
import zlib
import time
r = redis.Redis(host='localhost', port=6379, db=0)
"""
teardown = """
import redis
r = redis.Redis(host='localhost', port=6379, db=0)
r.flushall()
r.flushdb()
"""
update_compressed = """
keys = r.keys("*")
for i in xrange(10000):
value = zlib.decompress(r.hget(str(i), str(i)))
value += "{'dict_example': 'instead, let's appended some data as a dict'}"
r.hset(str(i), str(i), zlib.compress(value, 1))"""
update_normal = """
keys = r.keys("*")
for i in xrange(10000):
value = r.hget(str(i), str(i))
value += "{'dict_example': 'instead, let's appended some data as a dict'}"
r.hset(str(i), str(i), value)"""
multi_update_normal = """
keys = r.keys("*")
for i in xrange(10000):
value = r.hget(str(i), str(i))
value += "hi"
r.hset(str(i), str(i), value)"""
multi_update_compressed = """
keys = r.keys("*")
for i in xrange(10000):
value = zlib.decompress(r.hget(str(i), str(i)))
value += "hi"
r.hset(str(i), str(i), zlib.compress(value, 1))"""
# NORMAL HSET
In [23]: timeit.Timer(stmt=setup_normal, setup=setup).timeit(number=1)
Out[23]: 0.8443021774291992
In [24]: timeit.Timer(stmt=teardown).timeit(number=1)
Out[24]: 0.01998305320739746
In [25]: timeit.Timer(stmt=setup_normal, setup=setup).timeit(number=1)
Out[25]: 0.817255973815918
In [26]: timeit.Timer(stmt=teardown).timeit(number=1)
Out[26]: 0.016926050186157227
In [27]: timeit.Timer(stmt=setup_normal, setup=setup).timeit(number=1)
Out[27]: 0.8345811367034912
In [28]: timeit.Timer(stmt=teardown).timeit(number=1)
Out[28]: 0.030422210693359375
In [29]: timeit.Timer(stmt=setup_normal, setup=setup).timeit(number=1)
Out[29]: 0.8166131973266602
# COMPRESSED HSET
In [36]: timeit.Timer(stmt=setup_compress, setup=setup).timeit(number=1)
Out[36]: 0.9279201030731201
In [37]: timeit.Timer(stmt=teardown).timeit(number=1)
Out[37]: 0.010682106018066406
In [38]: timeit.Timer(stmt=setup_compress, setup=setup).timeit(number=1)
Out[38]: 0.9192800521850586
In [39]: timeit.Timer(stmt=teardown).timeit(number=1)
Out[39]: 0.010504007339477539
In [40]: timeit.Timer(stmt=setup_compress, setup=setup).timeit(number=1)
Out[40]: 0.9327700138092041
In [41]: timeit.Timer(stmt=teardown).timeit(number=1)
Out[41]: 0.011174917221069336
In [42]: timeit.Timer(stmt=setup_compress, setup=setup).timeit(number=1)
Out[42]: 0.9304389953613281
In [43]: timeit.Timer(stmt=teardown).timeit(number=1)
Out[43]: 0.011123895645141602
# UPDATE COMPRESSED
In [64]: timeit.Timer(stmt=setup_compress, setup=setup).timeit(number=1)
Out[64]: 0.9193120002746582
In [65]: timeit.Timer(stmt=update_compressed, setup=setup).timeit(number=1)
Out[65]: 1.901003122329712
In [66]: timeit.Timer(stmt=teardown).timeit(number=1)
Out[66]: 0.018635988235473633
In [67]: r.keys("*")
Out[67]: []
In [68]: timeit.Timer(stmt=setup_compress, setup=setup).timeit(number=1)
Out[68]: 0.9307420253753662
In [69]: timeit.Timer(stmt=update_compressed, setup=setup).timeit(number=1)
Out[69]: 1.9021191596984863
In [70]: timeit.Timer(stmt=teardown).timeit(number=1)
Out[70]: 0.01803898811340332
In [71]: r.keys("*")
Out[71]: []
In [72]: timeit.Timer(stmt=setup_compress, setup=setup).timeit(number=1)
Out[72]: 0.9173710346221924
In [73]: timeit.Timer(stmt=update_compressed, setup=setup).timeit(number=1)
Out[73]: 1.8699748516082764
In [74]: timeit.Timer(stmt=teardown).timeit(number=1)
Out[74]: 0.017473936080932617
In [75]: r.keys("*")
Out[75]: []
# UPDATE NORMAL
In [80]: timeit.Timer(stmt=update_normal, setup=setup).timeit(number=1)
Out[80]: 1.7522640228271484
In [81]: timeit.Timer(stmt=teardown).timeit(number=1)
Out[81]: 0.026376962661743164
In [82]: timeit.Timer(stmt=setup_normal, setup=setup).timeit(number=1)
Out[82]: 0.828575849533081
In [83]: timeit.Timer(stmt=update_normal, setup=setup).timeit(number=1)
Out[83]: 1.816329002380371
In [84]: timeit.Timer(stmt=teardown).timeit(number=1)
Out[84]: 0.021139860153198242
In [85]: timeit.Timer(stmt=setup_normal, setup=setup).timeit(number=1)
Out[85]: 0.8124969005584717
In [86]: timeit.Timer(stmt=update_normal, setup=setup).timeit(number=1)
Out[86]: 1.7448439598083496
In [87]: timeit.Timer(stmt=teardown).timeit(number=1)
Out[87]: 0.020651817321777344
In [88]: timeit.Timer(stmt=setup_normal, setup=setup).timeit(number=1)
Out[88]: 0.8113970756530762
In [89]: timeit.Timer(stmt=update_normal, setup=setup).timeit(number=1)
Out[89]: 1.730395793914795
In [90]: timeit.Timer(stmt=teardown).timeit(number=1)
Out[90]: 0.02049708366394043
# MULTIPLE UPDATE NORMAL
In [106]: timeit.Timer(stmt=multi_update_normal, setup=setup).timeit(number=50)
Out[106]: 87.1301679611206
In [107]: timeit.Timer(stmt=teardown).timeit(number=1)
Out[107]: 0.02513909339904785
In [108]: r.keys("*")
Out[108]: []
In [109]: timeit.Timer(stmt=setup_normal, setup=setup).timeit(number=1)
Out[109]: 0.8083679676055908
In [110]: timeit.Timer(stmt=multi_update_normal, setup=setup).timeit(number=50)
Out[110]: 87.08604502677917
# MULTIPLE UPDATE COMPRESSED
In [126]: timeit.Timer(stmt=setup_compress, setup=setup).timeit(number=1)
Out[126]: 0.9181180000305176
In [127]: timeit.Timer(stmt=multi_update_compressed, setup=setup).timeit(number=50)
Out[127]: 92.22424387931824
In [128]: timeit.Timer(stmt=teardown).timeit(number=1)
Out[128]: 0.013030052185058594
In [129]: timeit.Timer(stmt=setup_compress, setup=setup).timeit(number=1)
Out[129]: 0.9096369743347168
In [130]: timeit.Timer(stmt=multi_update_compressed, setup=setup).timeit(number=50)
Out[130]: 93.12042212486267
@stuntgoat
Copy link
Author

Early April 2011 MacBook Pro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment