日常工作过程中,我们需要对网站的并发量、响应时间进行测试,这种测试通常是对网站平台的
测试,其原理是:客户端发出一个请求,服务端收到这个请求给予相应的响应,从收到请求到做出响
应的这一段时间称为系统的响应时间,而系统在不同的负载,即不同的用户访问数的情况下,这个响
应时间是不相同的。实际应用中能这些点的第三方软件很多,但不是本文的重点,所以本文中,我们
利用pytho n脚本来编写一段简单的代码来实现网站并发量和响应时间的测试。
总体而言这样的测试实质上也是一次性能测试,结合测试的类型来看,所有的性能测试都可以从
两个维度划分:一种是基于用户行为模拟的性能测试,例如本文将要介绍的“使用python对网站进行
测试”;另一种抽象程度更高的构造数据包级的测试,关于这种类型的测试,在后续的博文中,有
机会的我们再举例说明。
一、测试网站的响应时间
测试网页的响应时间,代码如下:
import threading
import urllib
from time import sleep,ctime
def getHtml(func,url):
try:
print 'start request: %s,%s' %(func,ctime())
page = urllib.urlopen(url)
html = page.read()
print 'receive response:%s,%s' %(func,ctime())
return html
except:
print 'error……'
return []
if __name__== '__main__':
url = "http://p_w_picpath.baidu.com/channel?c=%E7%BE%8E%E5%A5%B3#%E7%BE%8E%E5%A5%B3"
for i in range(1,11):
print '%s : request'%i
test = getHtml(i,url)
print '----------------------------------------------'
运行程序,结果如下:
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
1 : request
start request: 1,Sun May 03 13:29:41 2015
receive response:1,Sun May 03 13:29:41 2015
----------------------------------------------
2 : request
start request: 2,Sun May 03 13:29:41 2015
receive response:2,Sun May 03 13:29:42 2015
----------------------------------------------
3 : request
start request: 3,Sun May 03 13:29:42 2015
receive response:3,Sun May 03 13:29:42 2015
----------------------------------------------
4 : request
start request: 4,Sun May 03 13:29:42 2015
receive response:4,Sun May 03 13:29:43 2015
----------------------------------------------
上面是一个很粗糙的代码片段,但如果将程序的输出信息写到一个文件中,并写一个相应数据分
析统 计代码,我们就能实现一个测试网页访问时间的代码。
二、使用多线程实现网页并非访问
import threading
import urllib
from time import sleep,ctime
def getHtml(func,url):
try:
print 'start request: %s,%s' %(func,ctime())
page = urllib.urlopen(url)
html = page.read()
print 'receive response:%s,%s' %(func,ctime())
return html
except:
print 'error……'
return []
if __name__== '__main__':
url = "http://www.baidu.com/"
threads = []
list = range(1,11)
for i in list:
t = threading.Thread(target=getHtml,args=(i,url))
threads.append(t)
print '---------------------start -------------------------'
for j in list:
print '%s : request'%j
threads[j-1].start()
for k in list:
threads[k-1].join()
print '---------------------over -------------------------'
程序运行结果如下所示:
---------------------start -------------------------
1 : request
start request: 1,Sun May 03 15:16:54 20152 : request
start request: 2,Sun May 03 15:16:54 20153 : request
4 : requeststart request: 3,Sun May 03 15:16:54 2015
start request: 4,Sun May 03 15:16:54 20155 : request
start request: 5,Sun May 03 15:16:54 20156 : request
start request: 6,Sun May 03 15:16:54 20157 : request
start request: 7,Sun May 03 15:16:54 20158 : request
9 : requeststart request: 8,Sun May 03 15:16:54 2015
10 : requeststart request: 9,Sun May 03 15:16:54 2015
start request: 10,Sun May 03 15:16:54 2015
receive response:3,Sun May 03 15:16:54 2015
receive response:7,Sun May 03 15:16:54 2015
receive response:2,Sun May 03 15:16:54 2015
receive response:5,Sun May 03 15:16:54 2015
receive response:1,Sun May 03 15:16:54 2015
receive response:4,Sun May 03 15:16:54 2015
receive response:6,Sun May 03 15:16:54 2015
receive response:10,Sun May 03 15:16:54 2015
receive response:9,Sun May 03 15:16:55 2015
receive response:8,Sun May 03 15:16:55 2015
---------------------over -------------------------
由于是多线程,程序的运行结果,显得比较混乱,在具体计算一个请求和对应的响应时间时,我们可
以根据请求的start request后面的seq号和receive response后面的seq来匹配对应的响应数据包,例如
receive response:2,Sun May 03 15:16:54 2015 seq =2与start request: 2,Sun May 03 15:16:54 20153 :
request中的seq =2相等,那么receive response:2是start request: 2的响应事务,同理可计算出全部的
请求对应的响应时间,最后根据一定的规则来生成所需的报告数据。
转载于:https://blog.51cto.com/857768/1641397