Created
December 7, 2010 23:02
-
-
Save cgbystrom/732602 to your computer and use it in GitHub Desktop.
Simple Redis chat experiment
This file contains hidden or 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
# Monkey patching with redis-py didn't work out so well | |
#from gevent import monkey; monkey.patch_all() | |
import gevent | |
import redis | |
import time | |
import random | |
client = redis.Redis(host='localhost', port=6379, db=0) | |
client.flushdb() | |
chat_message = ['Hello Planet', 'How are you today?', 'I am fine.', 'I love meatballs', 'Ok, bye-bye'] | |
num_users = 20 | |
num_channels = int(num_users * 0.5) | |
# Time spent chatting before ending conversation | |
min_chat_time = 20 | |
max_chat_time = 60 | |
# Conversations to retry | |
max_retries = 2 | |
# Time spent idling between sending chat messages | |
min_idle_time = 5 | |
max_idle_time = 20 | |
# Max chat messages to store | |
chat_backlog = 50 | |
channels = [] | |
for i in xrange(num_channels): | |
channels.append('chat_channel_%d' % i) | |
requests = {} | |
total_requests = 0 | |
def log_call(): | |
global total_requests | |
t = int(time.time()) | |
requests.setdefault(t, 0) | |
requests[t] += 1 | |
total_requests += 1 | |
def show_stats(): | |
while True: | |
rps = requests.setdefault(int(time.time()) - 1, 0) | |
print "Total %d Requests/sec %d" % (total_requests, rps) | |
gevent.sleep(1) | |
def username(id): return 'username_%d' % id | |
def chat_user(user_id, channel, retries): | |
if retries > max_retries: | |
return | |
# Get all messages to display | |
messages = client.lrange(channel, 0, chat_backlog - 1); log_call() | |
# Add ourselves to channel list | |
client.sadd(channel + "_list", username(user_id)); log_call() | |
# Get all users in channel | |
client.smembers(channel + "_list"); log_call() | |
end_time = time.time() + random.randrange(min_chat_time, max_chat_time) | |
while (end_time > time.time()): | |
client.lpush(channel, random.choice(chat_message)); log_call() | |
client.ltrim(channel, 0, chat_backlog - 1); log_call() | |
gevent.sleep(random.randrange(min_idle_time, max_idle_time)) | |
# Remove ourselves to channel list | |
client.srem(channel + "_list", username(user_id)); log_call() | |
retries += 1 | |
chat_user(user_id, channel, retries) | |
gevent.spawn(show_stats) | |
print "Spawning %d users..." % num_users | |
spawned_users = [] | |
for user_id in xrange(num_users): | |
spawned_users.append(gevent.spawn(chat_user, user_id, random.choice(channels), 0)) | |
print "Waiting for all users to finish..." | |
gevent.joinall(spawned_users) | |
print "Test completed" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment