Skip to content

Instantly share code, notes, and snippets.

@tangnotes
Created August 28, 2020 09:50
Show Gist options
  • Save tangnotes/c73e1d5e29c355de020323bdc1da7a86 to your computer and use it in GitHub Desktop.
Save tangnotes/c73e1d5e29c355de020323bdc1da7a86 to your computer and use it in GitHub Desktop.
import redis
import random
import string
import threading
host_ip='192.168.0.100'
host_port=6379
target_size = 1024 * 1024 # KB
def get_random_string(length):
letters = string.ascii_lowercase
result_str = ''.join(random.choice(letters) for i in range(length))
return result_str
def generate(s, r, size):
print("Start {}, size: {}".format(s, size))
for i in range(size):
key = get_random_string(10)
value = get_random_string(1024)
r.set(key, value)
print("shard {} done".format(s))
if __name__ == '__main__':
r = redis.Redis(host=host_ip, port=host_port, db=0)
r.set('test','test-stat')
r.get('test')
shards = 100
shard_size = int(target_size / shards)
if shard_size < 1024:
shards = 10
shard_size = int(target_size / shards)
print('Start...')
tasks = []
for s in range(shards):
t = threading.Thread(target=generate, args=(s, r, shard_size))
tasks.append(t)
t.start()
for t in tasks:
t.join()
print("task done", t.getName())
print('Done')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment