Skip to content

Instantly share code, notes, and snippets.

@peterbe
Created January 8, 2018 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterbe/09225ef40fbe1da67254d2a1b1e6e1e1 to your computer and use it in GitHub Desktop.
Save peterbe/09225ef40fbe1da67254d2a1b1e6e1e1 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
import time
import redis
import random
import sys
r = redis.Redis(host='redis-store')
NUM_ENTRIES = 1_000_000
MAX_VAL = 12_000_000
buckets = int(sys.argv[1])
assert buckets > 0 and buckets < 10_000
r.flushall()
time.sleep(1) # give it a chance to settle
# print("DBSIZE before", r.dbsize())
assert r.dbsize() == 0, r.dbsize()
t0 = time.time()
p = r.pipeline()
hmsets = {}
for i in range(0, NUM_ENTRIES):
value = random.randint(0, MAX_VAL)
bucket = int(i / buckets)
if bucket not in hmsets:
hmsets[bucket] = {}
hmsets[bucket][i] = value
if i and not i % (NUM_ENTRIES // 10):
p = r.pipeline()
for name, mapping in hmsets.items():
r.hmset(name, mapping)
p.execute()
hmsets = {}
# print(format(i, ','))
# one final clear out
p = r.pipeline()
for name, mapping in hmsets.items():
r.hmset(name, mapping)
p.execute()
t1 = time.time()
# print("THE WHOLE THING TOOK", t1 - t0, "SECONDS")
# get size
size = int(r.info()['used_memory'])
mb = size / 1024 / 1024
# print('{} bytes, {:.2f} MB'.format(size, mb))
print('MEMORY SIZE {:.2f} MB'.format(mb))
print("DBSIZE after ", r.dbsize())
print('KEYS PER BUCKET ', int(NUM_ENTRIES / r.dbsize()))
import csv
with open('redisbuckets.csv', 'a') as f:
writer = csv.writer(f)
writer.writerow([buckets, mb])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment