码迷,mamicode.com
首页 > 数据库 > 详细

python 写的一个mongodb压力测试

时间:2015-06-26 00:12:21      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:

 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 = host
16         self.port = port
17         self.user = user
18         self.passwd = passwd
19         
20         self.database = database
21 
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.posts
27 
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 c
33     
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_id
46         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 e
57         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=pytest, passwd =pytest123, database="pytest")
65     f.write(20000)
66     f.read()

 

python 写的一个mongodb压力测试

标签:

原文地址:http://www.cnblogs.com/caoguo/p/4601163.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!