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="127.0.0.1", 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="127.0.0.1", port=27017, user='pytest', passwd ='pytest123', database="pytest") $F.write (20000) theF.read ()
A MongoDB stress test written by Python