python实现用于测试网站访问速率的方法

这是python编写的用于测试网站访问速率的代码片段,可以输出打开某url的时间,访问100次的平均时间,最大时间和最小时间等等

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)

运行结果如下:

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程序设计有所帮助。

Posted in 未分类

发表评论