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

 更新时间:2015年05月26日 09:55:12   作者:小萝莉   我要评论
这篇文章主要介绍了python实现用于测试网站访问速率的方法,涉及Python中urllib2模块及时间的相关操作技巧,需要的朋友可以参考下

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

相关文章

  • Python将文本去空格并保存到txt文件中的实例

    Python将文本去空格并保存到txt文件中的实例

    今天小编就为大家分享一篇Python将文本去空格并保存到txt文件中的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • 利用Python爬虫给孩子起个好名字

    利用Python爬虫给孩子起个好名字

    每个人一生中都会遇到一件事情,在事情出现之前不会关心,但是事情一旦来临就发现它极其重要,并且需要在很短的时间内做出重大决定,那就是给自己的新生宝宝起个名字。下面这篇文章主要介绍了如何利用Python爬虫给孩子起个好名字,需要的朋友可以参考下。
    2017-02-02
  • Windows下用py2exe将Python程序打包成exe程序的教程

    Windows下用py2exe将Python程序打包成exe程序的教程

    这篇文章主要介绍了Windows下用py2exe将Python程序打包成exe程序的教程,文中主要针对Python3.x版本进行说明,需要的朋友可以参考下
    2015-04-04
  • Python之os操作方法(详解)

    Python之os操作方法(详解)

    下面小编就为大家带来一篇Python之os操作方法(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • python设置检查点简单实现代码

    python设置检查点简单实现代码

    这篇文章主要介绍了python设置检查点简单实现代码,需要的朋友可以参考下
    2014-07-07
  • python文件选择对话框的操作方法

    python文件选择对话框的操作方法

    这篇文章主要介绍了python文件选择对话框的操作方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-06-06
  • Python引用类型和值类型的区别与使用解析

    Python引用类型和值类型的区别与使用解析

    这篇文章主要介绍了Python引用类型和值类型的区别与使用解析,需要的朋友可以参考下
    2017-10-10
  • Windows系统配置python脚本开机启动的3种方法分享

    Windows系统配置python脚本开机启动的3种方法分享

    这篇文章主要介绍了Windows系统配置python脚本开机启动的3种方法分享,本文讲解了开始菜单启动项实现、开机脚本、通过一个服务调用该脚本三种方法,需要的朋友可以参考下
    2015-03-03
  • Python实现获取本地及远程图片大小的方法示例

    Python实现获取本地及远程图片大小的方法示例

    这篇文章主要介绍了Python实现获取本地及远程图片大小的方法,结合实例形式分析了Python使用PIL、urllib2及cStringIO模块获取本机或远程图片大小信息的相关操作技巧,需要的朋友可以参考下
    2018-07-07
  • python以环状形式组合排列图片并输出的方法

    python以环状形式组合排列图片并输出的方法

    这篇文章主要介绍了python以环状形式组合排列图片并输出的方法,涉及Python使用pil库操作图片的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-03-03

最新评论