Python 寫的一個MongoDB壓力測試

來源:互聯網
上載者:User

Python 寫的一個MongoDB壓力測試

Python 寫的一個MongoDB壓力測試

$ pip install pymongo

#!/usr/bin/env python
#coding=utf-8
#Author: Ca0Gu0

from pymongo import MongoClient
import datetime,time

 

class MongCli(object):
   

    def __init__(self, host="127.0.0.1", port=27017, user='pytest', passwd ='pytest123', database="pytest"):

        self.host = host
        self.port = port
        self.user = user
        self.passwd = passwd
       
        self.database = database

        client = MongoClient(self.host, self.port)
        client.the_database.authenticate(self.user, self.passwd, source=self.database)
               
        self.db=client[self.database]
        self.posts = self.db.posts

    def t(self, args1=None, args2=None):
       
        c=datetime.datetime.now()
        print "%s|%s... |%s|Row:%s| %s" %(self.host, args1, args2, self.posts.count(), c.strftime("%Y-%m-%d %H:%M:%S"))
        return c
   
   
   
    def write(self,number=100):
        start = self.t(args1="start", args2="write")
        for i in range(number):
            post = {"author": "Mike"+str(i),
                    "text": "My first blog post!"+str(i),
                    "tags": ["mongodb", "python", "pymongo"],
                    "date": datetime.datetime.utcnow()}
           
       
            post_id = self.posts.insert_one(post).inserted_id
        end = self.t(args1="end", args2="write")
        print "Total write runtime: %ss" %str((end-start).seconds)
   
    def read(self):
        start = self.t(args1="start", args2="read")
        output = open("output.txt", 'w')
        for post in self.posts.find():
            try:
                output.write(str(post)+"\n")
            except Exception,e:
                print e
        output.close()
        end = self.t(args1="end", args2="read")
        print "Total read runtime: %ss" %str((end-start).seconds)
        print "----------Split--------"
       
       
if __name__ == "__main__":
    f = MongCli(host="127.0.0.1", port=27017, user='admin', passwd ='admin', database="admin")
    f.write(20000)
    f.read()

MongoDB 3.0 正式版發布下載 

CentOS編譯安裝MongoDB

CentOS 編譯安裝 MongoDB與mongoDB的php擴充

CentOS 6 使用 yum 安裝MongoDB及伺服器端配置

Ubuntu 13.04下安裝MongoDB2.4.3

MongoDB入門必讀(概念與實戰並重)

Ubunu 14.04下MongoDB的安裝指南

《MongoDB 權威指南》(MongoDB: The Definitive Guide)英文文字版[PDF]

Nagios監控MongoDB分區叢集服務實戰

基於CentOS 6.5作業系統搭建MongoDB服務

MongoDB 的詳細介紹:請點這裡
MongoDB 的:請點這裡

本文永久更新連結地址:

相關文章

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.