A MongoDB stress test written by Python

Source: Internet
Author: User
Tags mongoclient

1 #!/usr/bin/env python2 #Coding=utf-83 #author:ca0gu04 5  fromPymongoImportmongoclient6 ImportDatetime,time7 8 9 Ten classMongcli (object): One      A  -     def __init__(Self, host="", port=27017, user='pytest', passwd ='pytest123', database="pytest"): -  theSelf.host =Host -Self.port =Port -Self.user =User -SELF.PASSWD =passwd +          -Self.database =Database +  AClient =mongoclient (Self.host, Self.port) atClient.the_database.authenticate (Self.user, SELF.PASSWD, source=self.database) -                  -self.db=Client[self.database] -Self.posts =self.db.posts -  -     defT (self, args1=none, args2=None): in          -C=Datetime.datetime.now () to         Print "%s|%s ... | %s| row:%s| %s"% (Self.host, ARGS1, Args2, Self.posts.count (), C.strftime ("%y-%m-%d%h:%m:%s")) +         returnC -      the      *      $     defWrite (self,number=100):Panax NotoginsengStart = self.t (args1="Start", args2="Write") -          forIinchRange (number): thePost = {"author":"Mike"+Str (i), +                      "text":"My First Blog post!"+Str (i), A                      "Tags": ["MongoDB","python","Pymongo"], the                      "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) -      the     defRead (self): -Start = self.t (args1="Start", args2="Read")WuyiOutput = open ("output.txt",'W') the          forPostinchself.posts.find (): -             Try: WuOutput.write (str (POST) +"\ n") -             exceptexception,e: About                 Printe $ output.close () -End = SELF.T (args1="End", args2="Read") -         Print "Total Read Runtime:%ss"%str ((end-start). seconds) -         Print "----------Split--------" A          +          the if __name__=="__main__": -f = mongcli (host="", port=27017, user='pytest', passwd ='pytest123', database="pytest") $F.write (20000) theF.read ()

A MongoDB stress test written by Python

Related Article

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.