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 相关文章推荐
Win10下Python环境搭建与配置教程
Nov 18 Python
python enumerate函数的使用方法总结
Nov 15 Python
Python反射用法实例简析
Dec 22 Python
python实现猜单词小游戏
May 22 Python
使用memory_profiler监测python代码运行时内存消耗方法
Dec 03 Python
python 定时器,轮询定时器的实例
Feb 20 Python
python实现PID算法及测试的例子
Aug 08 Python
python3.7实现云之讯、聚合短信平台的短信发送功能
Sep 26 Python
详解如何用python实现一个简单下载器的服务端和客户端
Oct 28 Python
python实现连续变量最优分箱详解--CART算法
Nov 22 Python
使用Python第三方库pygame写个贪吃蛇小游戏
Mar 06 Python
解决python中显示图片的plt.imshow plt.show()内存泄漏问题
Apr 24 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
SONY SRF-22W(33W)的电路分析和维修案例
2021/03/02 无线电
在windows iis5下安装php4.0+mysql之我见
2006/10/09 PHP
php中使用preg_match_all匹配文章中的图片
2013/02/06 PHP
一款简单实用的php操作mysql数据库类
2014/12/08 PHP
php实现window平台的checkdnsrr函数
2015/05/27 PHP
Yii全局函数用法示例
2017/01/22 PHP
javascript中类的定义及其方式(《javascript高级程序设计》学习笔记)
2011/07/04 Javascript
jquery submit ie6下失效的原因分析及解决方法
2013/11/15 Javascript
禁用Enter键表单自动提交实现代码
2014/05/22 Javascript
JavaScript动态修改网页元素内容的方法
2015/03/21 Javascript
js模仿php中strtotime()与date()函数实现方法
2015/08/11 Javascript
js带前后翻页的图片切换效果代码分享
2015/09/08 Javascript
Linux下为Node.js程序配置MySQL或Oracle数据库的方法
2016/03/19 Javascript
AngulerJS学习之按需动态加载文件
2017/02/13 Javascript
Angular中封装fancyBox(图片预览)遇到问题小结
2017/09/01 Javascript
基于JavaScript实现瀑布流布局
2018/08/15 Javascript
微信小程序开发之左右分栏效果的实例代码
2019/05/20 Javascript
vue el-table实现自定义表头
2019/12/11 Javascript
webpack 动态批量加载文件的实现方法
2020/03/19 Javascript
JavaScript onclick事件使用方法详解
2020/05/15 Javascript
python中将阿拉伯数字转换成中文的实现代码
2011/05/19 Python
Selenium元素的常用操作方法分析
2018/08/10 Python
python+pyqt5实现KFC点餐收银系统
2019/01/24 Python
由面试题加深对Django的认识理解
2019/07/19 Python
django 实现手动存储文件到model的FileField
2020/03/30 Python
iPython pylab模式启动方式
2020/04/24 Python
英国领先的运动物理治疗供应公司:Vivomed
2018/07/14 全球购物
Nike瑞士官网:Nike CH
2021/01/18 全球购物
作为网站管理者应当如何防范XSS
2014/08/16 面试题
九年级化学教学反思
2014/01/28 职场文书
2014年两会学习心得体会
2014/03/10 职场文书
乔迁之喜主持词
2014/03/27 职场文书
员工廉洁自律承诺书
2014/05/26 职场文书
公司户外活动总结
2014/07/04 职场文书
初一军训感言
2015/08/01 职场文书
python OpenCV学习笔记
2021/03/31 Python