花了點時間用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()