使用python做簡單的介面效能測試

來源:互聯網
上載者:User

標籤:

思路:利用ruquest發送請求,利用多線程類比並發

下面直接上代碼:

#!/user/bin/env python#coding=utf-8import requestsimport datetimeimport timeimport threadingclass url_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)/1000 #擷取回應時間,單位ms        myreq.times.append(ResponseTime) #將回應時間寫入數組        if r.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#設定並發線程數    for i in range(1, nub):         t = threading.Thread(target=myreq.req, args=(‘12‘,‘http://m.ctrip.com/webapp/cpage/#mypoints‘))        threads.append(t)    for t in threads:        time.sleep(0.5) #設定考慮時間        #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))) #計算數組的平均值,保留3位小數    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)*60*60 + float(minute)*60 + float(second) #計算總的考慮時間+請求時間    print "Concurrent processing %s" %(nub-1) #列印並發數    print "use total time %s s" %(totaltime-(nub-1)*0.5) #列印總共消耗的時間    print "fail request %s" %myreq.error.count("0") #列印錯誤請求數

 

 

request start time 2015-02-10 18:24:14.316000request end time 2015-02-10 18:24:39.769000Average Response Time 46.700 msConcurrent processing 50use total time 25.453 s
fail request 1

 

還可以據此計算tps,也可以控制並發量迴圈找出符合回應時間要求的最大並發量,等等

使用python做簡單的介面效能測試

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.