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


Posted in Python onMay 26, 2015

这是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 相关文章推荐
python设置windows桌面壁纸的实现代码
Jan 28 Python
python操作数据库之sqlite3打开数据库、删除、修改示例
Mar 13 Python
wxpython中利用线程防止假死的实现方法
Aug 11 Python
python生成IP段的方法
Jul 07 Python
Python中的多行注释文档编写风格汇总
Jun 16 Python
Python实现购物车购物小程序
Apr 18 Python
python导入模块交叉引用的方法
Jan 19 Python
python分布式编程实现过程解析
Nov 08 Python
python os.path.isfile()因参数问题判断错误的解决
Nov 29 Python
Python爬虫之Spider类用法简单介绍
Aug 04 Python
详解python爬取弹幕与数据分析
Nov 14 Python
python 6种方法实现单例模式
Dec 15 Python
Python函数可变参数定义及其参数传递方式实例详解
May 25 #Python
Python易忽视知识点小结
May 25 #Python
Python中类型关系和继承关系实例详解
May 25 #Python
pymssql数据库操作MSSQL2005实例分析
May 25 #Python
python动态参数用法实例分析
May 25 #Python
Python文件去除注释的方法
May 25 #Python
python提取页面内url列表的方法
May 25 #Python
You might like
PHP概述.
2006/10/09 PHP
虚拟主机中对PHP的特殊设置
2006/10/09 PHP
也谈截取首页新闻 - 范例
2006/10/09 PHP
smarty section简介与用法分析
2008/10/03 PHP
在PHP中实现Javascript的escape()函数代码
2010/08/08 PHP
smarty模板中使用get、post、request、cookies、session变量的方法
2014/04/24 PHP
PHP各种异常和错误的拦截方法及发生致命错误时进行报警
2016/01/19 PHP
PHP查看SSL证书信息的方法
2016/09/22 PHP
深入浅出讲解:php的socket通信原理
2016/12/03 PHP
PHP多维数组排序array详解
2017/11/21 PHP
Laravel框架定时任务2种实现方式示例
2018/12/08 PHP
滚动图片效果 jquery实现回旋滚动效果
2013/01/08 Javascript
Jquery的hover方法让鼠标经过li时背景变色
2013/09/06 Javascript
jquery的live使用注意事项
2014/02/18 Javascript
JavaScript 对象字面量讲解
2016/06/06 Javascript
详解js中Json的语法与格式
2016/11/22 Javascript
JS变量及其作用域
2017/03/29 Javascript
详解微信小程序 相对定位和绝对定位
2017/05/11 Javascript
JS写XSS cookie stealer来窃取密码的步骤详解
2017/11/20 Javascript
angularJs中跳转到指定的锚点实例($anchorScroll)
2018/08/31 Javascript
vue中tab选项卡的实现思路
2018/11/25 Javascript
微信小程序事件对象中e.target和e.currentTarget的区别详解
2019/05/08 Javascript
VUEX采坑之路之获取不到$store的解决方法
2019/11/08 Javascript
Python Pandas 箱线图的实现
2019/07/23 Python
python使用openCV遍历文件夹里所有视频文件并保存成图片
2020/01/14 Python
深入解析HTML5的IndexedDB索引数据库
2015/09/14 HTML / CSS
Joie官方网上商店:购买服装和女装配饰
2018/06/05 全球购物
欧洲领先的火车票和大巴票预订平台:Trainline
2018/12/26 全球购物
天网面试题
2013/04/07 面试题
优秀团员个人的自我评价
2013/10/02 职场文书
大学考试作弊检讨书
2015/05/06 职场文书
初一年级组工作总结
2015/08/12 职场文书
学习习近平主席讲话心得体会
2016/01/20 职场文书
廉洁自律承诺书2016
2016/03/25 职场文书
mysql批量新增和存储的方法实例
2021/04/07 MySQL
python中取整数的几种方法
2021/11/07 Python