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 登录网站详解及实例
Apr 11 Python
python的unittest测试类代码实例
Dec 07 Python
python pygame实现2048游戏
Nov 20 Python
PyTorch 1.0 正式版已经发布了
Dec 13 Python
python实现一个简单的udp通信的示例代码
Feb 01 Python
Python爬取数据保存为Json格式的代码示例
Apr 09 Python
详解python中的线程与线程池
May 10 Python
使用Python制作表情包实现换脸功能
Jul 19 Python
Python调用ffmpeg开源视频处理库,批量处理视频
Nov 16 Python
python 基于PYMYSQL使用MYSQL数据库
Dec 24 Python
Autopep8的使用(python自动编排工具)
Mar 02 Python
python读取pdf格式文档的实现代码
Apr 01 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 has encountered an Access Violation at 7C94BD02解决方法
2009/08/24 PHP
Php Image Resize图片大小调整的函数代码
2011/01/17 PHP
zend framework框架中url大小写问题解决方法
2014/08/19 PHP
PHP利用超级全局变量$_POST来接收表单数据的实例
2016/11/05 PHP
8个超棒的学习 jQuery 的网站 推荐收藏
2011/04/02 Javascript
js为鼠标添加右击事件防止默认的右击菜单弹出
2013/07/29 Javascript
js获得地址栏?问号后参数的方法
2013/08/08 Javascript
JS获取当前网页大小以及屏幕分辨率等
2014/09/05 Javascript
JS判断浏览器是否支持某一个CSS3属性的方法
2014/10/17 Javascript
js实现动画特效的文字链接鼠标悬停提示的方法
2015/03/02 Javascript
JavaScript中length属性的使用方法
2015/06/05 Javascript
简述AngularJS相关的一些编程思想
2015/06/23 Javascript
JavaScript设计模式经典之工厂模式
2016/02/24 Javascript
用js控件div的滚动条,让它在内容更新时自动滚到底部的实现方法
2016/10/27 Javascript
使用JS代码实现俄罗斯方块游戏
2018/08/03 Javascript
详解Vue2 添加对scss的支持
2019/01/02 Javascript
vue cli 3.0 搭建项目的图文教程
2019/05/17 Javascript
JS实现从对象获取对象中单个键值的方法示例
2019/06/05 Javascript
React Ant Design树形表格的复杂增删改操作
2020/11/02 Javascript
在nodejs中创建child process的方法
2021/01/26 NodeJs
[38:31]完美世界DOTA2联赛PWL S3 Magma vs GXR 第一场 12.13
2020/12/17 DOTA
Python:Scrapy框架中Item Pipeline组件使用详解
2017/12/27 Python
对tf.reduce_sum tensorflow维度上的操作详解
2018/07/26 Python
浅谈python在提示符下使用open打开文件失败的原因及解决方法
2018/11/30 Python
Python3+selenium实现cookie免密登录的示例代码
2020/03/18 Python
python 监控服务器是否有人远程登录(详细思路+代码)
2020/12/18 Python
如何用python批量调整视频声音
2020/12/22 Python
自荐书范文
2013/12/08 职场文书
个人工作主要事迹
2014/05/08 职场文书
金融管理专业求职信
2014/07/10 职场文书
个人租房协议书范本
2014/09/30 职场文书
12.4全国法制宣传日活动总结
2014/11/01 职场文书
中班下学期幼儿评语
2014/12/30 职场文书
2015年村计划生育工作总结
2015/04/28 职场文书
Go 通过结构struct实现接口interface的问题
2021/10/05 Golang