MongoDB是一個基於分佈式文件存儲的數據庫開源項目。由C++語言編寫。旨在爲WEB應用提供可護展的高性能數據存儲解決方案。它比純noSQL數據庫查詢功能強悍,比關係數據庫更面向集合。python
測試的硬件:酷睿雙核3.0G,2G內存,7200轉普通硬盤,Ubuntu10.04 32bit,MongoDB1.61git
安裝&啓動github
longhao@aliyun:~$ sudo apt-get install mongodbmongodb
#添加一個so,不然啓動不了mongodb數據庫
longhao@aliyun:~$ sudo ln -s /usr/lib/xulrunner-devel-1.9.2.8/lib/libmozjs.so /usr/lib/libmozjs.so多線程
#啓動app
longhao@aliyun:~$ sudo service mongodb start分佈式
安裝python庫post
到github上去下載mongodb到python driver或者 git clone git://github.com/mongodb/mongo-python-driver.git pymongo性能
longhao@aliyun:~$ tar zxvf pymongo.*.tar.gz
longhao@aliyun:~$ cd pymongo
longhao@aliyun:~$ python setup.py install
編寫測試代碼
#!/usr/bin/env python
from pymongo import Connection
import time,datetime
connection = Connection('127.0.0.1', 27017)
db = connection['testdb']
'''
test program execute time,use python decorator!
'''
def func_time(func):
def _wrapper(*args,**kwargs):
start = time.time()
func(*args,**kwargs)
print func.__name__,'run:',time.time()-start
return _wrapper
@func_time
def insert(num):
posts = db.posts
for x in range(num):
post = {"author": str(x)+"Mike",
"text": "My first blog post!",
"tags": ["mongodb", "python", "pymongo"],
"date": datetime.datetime.utcnow()}
posts.insert(post)
@func_time
def find():
posts = db.posts
print posts.find_one()
@func_time
def remove():
posts = db.posts
print 'count before remove:',posts.count();
posts.remove({});
print 'count after remove:',posts.count();
if __name__ == "__main__":
num = 1000000
insert(num)
find()
remove()
測試結果
longhao@aliyun:~$ python mongodb.py
insert run: 211.037979126
{u'date': datetime.datetime(2010, 8, 31, 12, 59, 30, 456000), u'text': u'My first blog post!', u'_id': ObjectId('4c7cfcb28cb52a3eec00002e'), u'author': u'46Mike', u'tags': [u'mongodb', u'python', u'pymongo']}
find run: 0.0865120887756
count before remove: 1000000
count after remove: 0
remove run: 9.01437902451
測試顯示向數據庫中插入100W條數據耗時211.038秒,刪除100W條數據耗時9.014秒,數據庫文件總大小爲1G(這個和保持的對象大小有關係)
遇到到問題:
1:數據庫文件大小在清空庫後不能自動更改;
2:單線程測試的結果,多線程效率會怎樣還須要測試!