Using Python to do simple interface performance testing

Source: Internet
Author: User

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

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.