首页 > 学院 > 开发设计 > 正文

pythonmongodb压力测试脚本

2019-11-14 17:08:27
字体:
来源:转载
供稿:网友

 

$ pip install pymongo

  

 1 #!/usr/bin/env python 2 #coding=utf-8 3 #Author: Ca0Gu0 4  5 from pymongo import MongoClient 6 import datetime,time 7  8  9 10 class MongCli(object):11     12 13     def __init__(self, host="127.0.0.1", port=27017, user='pytest', passwd ='pytest123', database="pytest"):14 15         self.host = host16         self.port = port17         self.user = user18         self.passwd = passwd19         20         self.database = database21 22         client = MongoClient(self.host, self.port)23         client.the_database.authenticate(self.user, self.passwd, source=self.database)24                 25         self.db=client[self.database]26         self.posts = self.db.posts27 28     def t(self, args1=None, args2=None):29         30         c=datetime.datetime.now()31         PRint "%s|%s... |%s|Row:%s| %s" %(self.host, args1, args2, self.posts.count(), c.strftime("%Y-%m-%d %H:%M:%S"))32         return c33     34     35     36     def write(self,number=100):37         start = self.t(args1="start", args2="write")38         for i in range(number):39             post = {"author": "Mike"+str(i),40                      "text": "My first blog post!"+str(i),41                      "tags": ["mongodb", "python", "pymongo"],42                      "date": datetime.datetime.utcnow()}43             44          45             post_id = self.posts.insert_one(post).inserted_id46         end = self.t(args1="end", args2="write")47         print "Total write runtime: %ss" %str((end-start).seconds)48     49     def read(self):50         start = self.t(args1="start", args2="read")51         output = open("output.txt", 'w')52         for post in self.posts.find():53             try:54                 output.write(str(post)+"/n")55             except Exception,e:56                 print e57         output.close()58         end = self.t(args1="end", args2="read")59         print "Total read runtime: %ss" %str((end-start).seconds)60         print "----------Split--------"61         62         63 if __name__ == "__main__":64     f = MongCli(host="127.0.0.1", port=27017, user='admin', passwd ='admin', database="admin")65     f.write(20000)66     f.read()

 

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表