Skip to content

Instantly share code, notes, and snippets.

@dragnot
Created April 3, 2017 13:27
Show Gist options
  • Save dragnot/656f6227fba3da506237e9ae2b2326d5 to your computer and use it in GitHub Desktop.
Save dragnot/656f6227fba3da506237e9ae2b2326d5 to your computer and use it in GitHub Desktop.
Just a sample gisl
import random
__author__ = 'dragnot'
# file two.py
import redis
import threading
#
# this benchmark is all about getting items from the db and displaying counting each viewed item.
# it should initialize the redis with X amount of items and start loading items randomly. when item is selected, the code should fetch that item and add to the counter
# every 10 sec ( or so ) it should display the top viewed 10 items
pool = redis.ConnectionPool(host='localhost', port=6379, db=0)
number_of_items = 10000
db = redis.Redis(connection_pool=pool)
page_rank_set_name = "page_rank"
page_name_prefix = "page_"
def init_games():
db.flushdb()
for x in xrange(1,number_of_items):
db.set( page_name_prefix + str(x),"page content" + str(x))
# print str(my_server.dbsize())
def create_workload():
threads = []
for n in range(2):
thread = threading.Thread(target=get_item())
thread.start()
threads.append(thread)
# to wait until all three functions are finished
print "Waiting..."
for thread in threads:
thread.join()
print "Complete."
resultarray = db.zrange(page_rank_set_name,-5,-1)
print resultarray
for key in resultarray:
print db.zscore(page_rank_set_name,key)
def get_item():
dbx = redis.Redis(connection_pool=pool)
for x in xrange(1,10000):
current_key = random.randint(1,number_of_items - 1)
pipe = dbx.pipeline()
pipe.get(page_name_prefix + str(current_key))
pipe.zincrby(page_rank_set_name,page_name_prefix + str(current_key))
item_name = pipe.execute()
if item_name[0] is None:
print "item not found " + str(current_key)
if __name__ == "__main__":
# this will set the pages in the site
init_games()
# run a thread that populize data on the site
create_workload()
else:
print("one.py is being imported into another module")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment