python編寫的用於測試網站訪問速率的源碼片斷

將作工程過程當中比較經常使用的一些內容片斷作個記錄,以下內容段是關於python編寫的用於測試網站訪問速率的片斷的內容,應該能對各朋友有所用處。

def Process(url,n):
    minSpan = 10.0
    maxSpan = 0.0
    sumSpan= 0.0
    over1s = 0
    for i in range(n):
        startTime = datetime.datetime.now()
        try:
            res = urllib2.urlopen(url,timeout=10)
        except:
            pass
        endTime = datetime.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__':  複製代碼
                                                                                                                                          
相關文章
相關標籤/搜索