A mongodb Stress Test Written in python and a mongodb stress test

Source: Internet
Author: User
Tags mongoclient

A mongodb Stress Test Written in python and a mongodb stress test

 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='pytest', passwd ='pytest123', database="pytest")65     f.write(20000)66     f.read()

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.