fasoulas
2/1/2016 - 11:13 AM

Mongo Perf Testing using Python

Mongo Perf Testing using Python

import time
import pymongo
m = pymongo.MongoClient('mongodb://localhost:27017/')
db = m.slh

def testFullCollectionRetrieval (db, collection):
	"internal function to test average execution time"
	iterations = 1000
	totalTime = 0
	i = 0
	while (i <= iterations): 
		start = time.time()
		results = db[collection].find()
		#print results.count()
		end = time.time()

		executionTime = (end - start) * 1000 
		totalTime = totalTime + executionTime
		i = i + 1
	return totalTime / iterations
collections = db.collection_names(include_system_collections=False)

def testSingleResult(db,collection, json):
	"internal function to test average execution time"
	iterations = 1000
	totalTime = 0
	i = 0
	while (i <= iterations): 
		start = time.time()
		results = db[collection].find(json).count()
		end = time.time()

		executionTime = (end - start) * 1000 
		totalTime = totalTime + executionTime
		i = i + 1
	return totalTime / iterations

def testSingleHotelRetrieval(db, collection, json):
	return testSingleResult(db,collection, json)
	
def testSingleCuisineRetrieval(db, collection, json):
	return testSingleResult(db,collection, json)
	
def testSingleHotelReviewRetrieval(db, collection, json):
	return testSingleResult(db,collection, json)
	
print "Testing full collection retrieval"
print
for current in collections:
	print "Average time for collection:\t"+ current + "\t"+ str(testFullCollectionRetrieval(db, current)) + "\tms"; 
print
print "Average time for retrieval of 1 Hotel with code:HUADLTM\t" +str(testSingleHotelRetrieval(db,"Hotels",{"HotelCode":"HUADLTM"})) + "\tms";  
print
print "Average time for retrieval of 1 Cuisine with code:RU\t" +str(testSingleCuisineRetrieval(db,"Cuisines",{"Code":"RU"})) + "\tms";  
print
print "Average time for retrieval of 1 HotelReview with code:HUDUBLS\t" +str(testSingleHotelReviewRetrieval(db,"Cuisines",{"HotelCode":"HUDUBLS"})) + "\tms";