Idea: Using Ruquest to send requests, using multithreading to simulate concurrency
Directly below the code:
#!/user/bin/env python#Coding=utf-8ImportRequestsImportdatetimeImport TimeImportThreadingclassurl_request (): times=[] Error= [] defreq (self,appid,url): Myreq=url_request () headers= {'user-agent':'mozilla/5.0 (Linux; Android 4.2.1; En-us; Nexus 4 build/jop40d) applewebkit/535.19 (khtml, like Gecko) chrome/18.0.1025.166 Mobile safari/535.19'} payload= {'AppID': AppID,'Currenturl': URL} r= Requests.post ("Http://xx.xxx.com/WeiXinJSAccessToken/json/WeChatJSTicket", Headers=headers,data=payload) ResponseTime=float (r.elapsed.microseconds)/1000#Get response time, Unit MsMyreq.times.append (ResponseTime)#Write response time to an array ifR.status_code!=200: Myreq.error.append ("0")if __name__=='__main__': Myreq=url_request () Threads=[] StartTime=Datetime.datetime.now ()Print "Request Start time%s"%starttime Nub= 51#set number of concurrent threads forIinchRange (1, nub): t= Threading. Thread (Target=myreq.req, args= (' A','http://m.ctrip.com/webapp/cpage/#mypoints')) Threads.append (t) forTinchThreads:time.sleep (0.5)#Set think Time #print "thread%s"%t #打印线程T.setdaemon (True) T.start () t.join () Endtime=Datetime.datetime.now ()Print "Request End Time%s"%Endtime Time.sleep (3) Averagetime="{:. 3f}". Format (float (sum (myreq.times))/float (Len (myreq.times)))#calculates the average of an array, preserving 3 decimal places Print "Average Response Time%s Ms"%averagetime#Print average response timeUsetime = str (Endtime-starttime) Hour= Usetime.split (':'). Pop (0) minute= Usetime.split (':'). Pop (1) Second= Usetime.split (':'). Pop (2) TotalTime= Float (hour) *60*60 + float (minute) *60 + float (second)#Calculate total think time + request time Print "Concurrent Processing%s"% (nub-1)#number of print concurrency Print "Use total time %s s"% (totaltime-(nub-1) *0.5)#total time spent in printing Print "Fail Request%s"%myreq.error.count ("0")#number of print error requests
Request Start time 2015-02-10 18:24:14.3160002015-02-10 18:24:39.76900046.700 25.453 S
Fail Request 1
You can also calculate the TPS on this basis, or you can control the concurrency cycle to find the maximum concurrency that meets the response time requirements, and so on
Using Python to do simple interface performance testing