Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import datetime
- from pytz import utc
- from random import randrange, choice, uniform
- import time
- import json
- number_of_records = 80000
- number_of_tests = 10
- collection = #change this
- start = datetime.datetime(2011,1,1,0,0,0, tzinfo=utc)
- end = datetime.datetime.now(utc)
- def get_random_time_between(start=start, end=end):
- delta = end - start
- int_delta = (delta.days * 24 * 60 * 60) + delta.seconds
- random_second = randrange(int_delta)
- return (start + datetime.timedelta(seconds=random_second))
- def get_random_user_id(start = 0 , end = 150):
- return randrange(start, end)
- def get_random_var():
- return choice(['a', 'b', 'c', 'd', 'e', 'f', 'g','h', 'i', 'j', 'k', 'l', 'm', 'n','o', 'p'])
- def get_num_closed():
- return uniform(0,3)
- def get_random_change():
- return uniform(-100,100)
- def simulate():
- print 'Simulating...'
- start = time.time()
- data_to_insert = []
- for each in range(number_of_records):
- data_to_insert.append({
- 'var1' : get_random_user_id(),
- 'va2' : True,
- 'var3' : False,
- 'var4' : randrange(0, 100),
- 'var5' : get_random_var(),
- 'var6' : get_num_closed(),
- 'var7' : get_random_change(),
- 'var8' : get_random_time_between()
- })
- collection.insert(data_to_insert)
- print time.time() - start
- def time_it(function, num_tests):
- print 'Testing:', function.__name__
- start = time.time()
- for i in range(num_tests):
- function()
- runtime = time.time() - start
- print "Average: ", runtime/float(num_tests)
- def test_mongo_read():
- var = list(collection.find())
- def test_json_read():
- with open('testfile.json', 'r') as output:
- test= json.load(output)
- def create_json_file():
- print 'Creating JSON file replica of collection...'
- data = list(collection.find())
- output = open('testfile.json', 'w+b')
- dthandler = lambda obj: obj.isoformat() if isinstance(obj, datetime.datetime) else None
- json.dump(data, output, default=dthandler)
- def run():
- #initialize
- simulate()
- create_json_file()
- time_it(test_mongo_read, number_of_tests)
- time_it(test_json_read, number_of_tests)
- if __name__ == '__main__':
- run()
Add Comment
Please, Sign In to add comment