Last active
August 29, 2015 14:18
-
-
Save dougkeen/4587ef7d03fcb31ce7cc to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/local/bin/python | |
import sys | |
import zlib | |
import random | |
import time | |
import itertools | |
def sizeof_fmt(num, suffix='B'): | |
for unit in ['','K','M','G','T','P','E','Z']: | |
if abs(num) < 1024.0: | |
return "%3.2f%s%s" % (num, unit, suffix) | |
num /= 1024.0 | |
return "%.2f%s%s" % (num, 'Y', suffix) | |
if __name__ == "__main__": | |
if len(sys.argv) != 2: | |
# Remember, script name is always the first arg | |
print "This script expects a single, integer list size argument" | |
sys.exit(0) | |
list_size = int(sys.argv[1]) | |
pairs = [(str(random.randint(100000,9999999)), ''.join([random.choice('0123456789abcdef') for _ in itertools.repeat(None, 8)])) for _ in itertools.repeat(None, list_size)] | |
jsonified_pairs = ['"{}":"{}"'.format(id, ver) for (id, ver) in pairs] | |
json = '{{"requireFullList":true,"knownRooms":[{{{0}}}]}}'.format(','.join(jsonified_pairs)) | |
# print json | |
print 'Size before compression: {}'.format(sizeof_fmt(len(json))) | |
t1 = time.clock() | |
compressed_json = zlib.compress(json, 9) | |
t2 = time.clock() | |
print 'Size after compression: {}'.format(sizeof_fmt(len(compressed_json))) | |
print 'Time to compress: {0:7.2f} ms'.format((t2-t1) * 1000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment