python 使用Python进行Mongo Perf测试

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 使用Python进行Mongo Perf测试相关的知识,希望对你有一定的参考价值。

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";  

以上是关于python 使用Python进行Mongo Perf测试的主要内容,如果未能解决你的问题,请参考以下文章

从最新到最旧的python对Mongo列表进行排序[重复]

Python脚本监控mysql数据库,Python脚本监控mongo数据库

将 mongo 与 FLASK 和 python 一起使用

用Python构建一个PE文件

用Python构建一个PE文件

用Python构建一个PE文件