程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 更多關於編程 >> python實現用於測試網站訪問速率的方法

python實現用於測試網站訪問速率的方法

編輯:更多關於編程

       這是python編寫的用於測試網站訪問速率的代碼片段,可以輸出打開某url的時間,訪問100次的平均時間,最大時間和最小時間等等

      ?

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 import urllib2 from datetime import * import time def Process(url,n): minSpan = 10.0 maxSpan = 0.0 sumSpan= 0.0 over1s = 0 for i in range(n): startTime = datetime.now() try: res = urllib2.urlopen(url,timeout=10) except: pass endTime = datetime.now() span = (endTime-startTime).total_seconds() sumSpan = sumSpan + span if span < minSpan: minSpan = span if span > maxSpan: maxSpan = span #超過一秒的 if span>1: over1s=over1s + 1 print(u'%s Spent :%s seconds'%(url,span)) print(u'requested:%s times,Total Spent:%s seconds,avg:%s seconds, max:%s seconds,min:%s seconds,over 1 secnod:%s times'%(n,sumSpan,sumSpan/n,maxSpan,minSpan,over1s)) print('n') if __name__=='__main__': Process('http://www.baidu.com',100)

      運行結果如下:

      ?

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.094 seconds http://www.baidu.com Spent :0.016 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.016 seconds http://www.baidu.com Spent :0.015 seconds http://www.baidu.com Spent :0.015 seconds http://www.baidu.com Spent :0.016 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.046 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.094 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.109 seconds http://www.baidu.com Spent :0.094 seconds http://www.baidu.com Spent :0.094 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.062 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.047 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.015 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.047 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.016 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.016 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.015 seconds http://www.baidu.com Spent :0.016 seconds http://www.baidu.com Spent :0.016 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.015 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.078 seconds http://www.baidu.com Spent :0.109 seconds http://www.baidu.com Spent :0.015 seconds http://www.baidu.com Spent :0.094 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.109 seconds http://www.baidu.com Spent :0.094 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.047 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds requested:100 times,Total Spent:3.67 seconds,avg:0.0367 seconds, max:0.109 seconds,min:0.015 seconds,over 1 secnod:0 times

      希望本文所述對大家的Python程序設計有所幫助。

    1. 上一頁:
    2. 下一頁:
    Copyright © 程式師世界 All Rights Reserved