MongoDB效能測試與Python測試代碼

來源:互聯網
上載者:User

最近參與公司一個項目,計劃對線上平台的大規模查詢做到快速響應,預估資料總量大概在2-3億條,資料庫並發量大概每秒1500,一年後並發大概3000每秒,經過在Redis和mongodb之間艱難的選擇之後,決定使用mongodb,主要看中其平行擴充能力和GridFS上的Map/Reduce。預估項目完成上線後,高峰時段每秒並發查詢在1500-3000之間。
其實我個人是比較喜歡Redis的,其並發查詢能力和超越memcached的速度都很令人心動,不過其持久化和叢集擴充性不太適合業務需要,所以最後還是選擇了mongodb。
下面是對mongodb測試的代碼和結果。雖然公司用的一水的CentOS,但是由於我是FreeBSD的支援者,所以在FreeBSD和CentOS上都測試了一下結果。
寫庫程式是網上抄來的,查詢程式是自己寫的。
寫庫程式
#!/usr/bin/env python

from pymongo import Connection
import time,datetime

connection = Connection('127.0.0.1', 27017)
db = connection['hawaii']

#時間記錄器
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.userinfo
        for x in range(num):
                post = {"_id" : str(x),
                        "author": str(x)+"Mike",
                        "text": "My first blog post!",
                        "tags": ["mongodb", "python", "pymongo"],
                        "date": datetime.datetime.utcnow()}
                posts.insert(post)

if __name__ == "__main__":
#設定迴圈500萬次
           num = 5000000
           insert(num)
查詢程式
#!/usr/bin/env python

from pymongo import Connection
import time,datetime
import random

connection = Connection('127.0.0.1', 27017)
db = connection['hawaii']

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 randy():
                rand = random.randint(1,5000000)
                return rand

@func_time
def mread(num):
                find = db.userinfo
                for i in range(num):
                                rand = randy()
#隨機數查詢
                                find.find({"author": str(rand)+"Mike"})

if __name__ == "__main__":
#設定迴圈100萬次
                num = 1000000
                mread(num)
刪除程式
#!/usr/bin/env python

from pymongo import Connection
import time,datetime

connection = Connection('127.0.0.1', 27017)
db = connection['hawaii']

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 remove():
        posts = db.userinfo
        print 'count before remove:',posts.count();
        posts.remove({});
        print 'count after remove:',posts.count();

if __name__ == "__main__":
        remove()

結果集

插入500萬 隨機數查詢100萬 刪除500萬 CPU佔用
CentOS 394s 28s 224s 25-30%
FreeBSD 431s 18s 278s 20-22%


CentOS插入和刪除勝出;FreeBSD發揮了UFS2的優勢,讀取勝出。由於是作為查詢服務器使用,所以讀取速度快是個優勢,不過我不是領導,我說了不算,最終還是得CentOS。
在測試中,一直使用mongostat監控,從並發數量上,兩個系統差不多。還測試了插入並發查詢,不過結果差不多,大概並發的相加和都是15000-25000每秒。效能還是很不錯的。
不過確實大資料量下插入效能下降比較嚴重,CentOS測試了5000萬資料插入,耗時接近2小時。大概是6300多秒。比起500萬資料插入速度,差不多慢了50%。不過查詢速度還算差不多。
測試結果供需要者做個參考。
不過,這個測試不是太公平。FreeBSD配置要差一點。
CentOS 16G記憶體,Xeon5606 兩顆8核。Dell品牌機。
FreeBSD 8G記憶體,Xeon5506 一顆4核。攢的沒牌子1U。
如果相同環境下,我覺得還是FreeBSD效能會比較好一點。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.