用Python實現的Web伺服器的壓力測試工具

來源:互聯網
上載者:User

花了點時間用Python寫了一個網頁抓取的工具,用來做網頁的伺服器壓力測試。也可以類比運行時的狀態。

# coding:gbkimport timeimport urllib2import threadingfrom Queue import Queuefrom time import sleep# 效能測試頁面PERF_TEST_URL = "http://localhost:8080/perf.jsp"# 配置:壓力測試#THREAD_NUM = 10# 並發線程總數#ONE_WORKER_NUM = 500# 每個線程的迴圈次數#LOOP_SLEEP = 0.01# 每次請求時間間隔(秒)# 配置:類比運行狀態THREAD_NUM = 100# 並發線程總數ONE_WORKER_NUM = 10000# 每個線程的迴圈次數LOOP_SLEEP = 0.5# 每次請求時間間隔(秒)# 出錯數ERROR_NUM = 0#具體的處理函數,負責處理單個任務def doWork(index):t = threading.currentThread()#print "["+t.name+" "+str(index)+"] "+PERF_TEST_URLtry:html = urllib2.urlopen(PERF_TEST_URL).read()except urllib2.URLError, e:print "["+t.name+" "+str(index)+"] "print eglobal ERROR_NUMERROR_NUM += 1#這個是背景工作處理序,負責不斷從隊列取資料並處理def working():t = threading.currentThread()print "["+t.name+"] Sub Thread Begin"i = 0while i < ONE_WORKER_NUM:i += 1doWork(i)sleep(LOOP_SLEEP)print "["+t.name+"] Sub Thread End"def main():#doWork(0)#returnt1 = time.time()Threads = []# 建立線程for i in range(THREAD_NUM):    t = threading.Thread(target=working, name="T"+str(i))    t.setDaemon(True)    Threads.append(t)for t in Threads:    t.start()for t in Threads:    t.join()print "main thread end"t2 = time.time()print "========================================"print "URL:", PERF_TEST_URLprint "任務數量:", THREAD_NUM, "*", ONE_WORKER_NUM, "=", THREAD_NUM*ONE_WORKER_NUMprint "總耗時(秒):", t2-t1print "每次請求耗時(秒):", (t2-t1) / (THREAD_NUM*ONE_WORKER_NUM)print "每秒承載請求數:", 1 / ((t2-t1) / (THREAD_NUM*ONE_WORKER_NUM))print "錯誤數量:", ERROR_NUMif __name__ == "__main__": main()
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.