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-8Import requestsimport datetimeimport timeimport threadingclassurl_request (): times=[] Error=[] def req (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)/ +#获取响应时间, Unit Ms Myreq.times.append (ResponseTime) #将响应时间写入数组ifR.status_code! = $: Myreq.error.append ("0")if__name__=='__main__': Myreq=url_request () Threads=[] StartTime=Datetime.datetime.now () print"Request Start time%s"%starttime Nub= -#设置并发线程数 ThinkTime=0.5#设置思考时间 forIinchRange1, nub+1): T= Threading. Thread (Target=myreq.req, args= (' A','http://m.ctrip.com/webapp/cpage/#mypoints')) Threads.append (t) forTinchthreads:time.sleep (thinktime) #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))) #计算数组的平均值, retain 3 decimal places print"Average Response Time%s Ms"%averagetime #打印平均响应时间 usetime= STR (Endtime-starttime) Hour= Usetime.split (':'). Pop (0) Minute= Usetime.split (':'). Pop (1) Second= Usetime.split (':'). Pop (2) TotalTime=float(Hour) * -* -+float(minute) * -+float(second) #计算总的思考时间 +Request time Print"Concurrent Processing%s"%nub #打印并发数 Print"Use total time %s s"% (totaltime-float(nub*thinktime)) #打印总共消耗的时间 Print"Fail Request%s"%myreq.error.count ("0") #打印错误请求数
 Request Start time 2015 -02 -10  18 : 24 : 14.316000  request End time  2015 -02 - 10  18 :  : 39.769000  average Response time  46.700   msconcurrent processing  Span style= "color: #800080;" >50  use total time  25.453   Sfail 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

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.