亲宝软件园·资讯

展开

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

小萝莉 人气:0
想了解python实现用于测试网站访问速率的方法的相关内容吗,小萝莉在本文为您仔细讲解python测试网站访问速率的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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程序设计有所帮助。

加载全部内容

相关教程
猜你喜欢
用户评论