日常工作过程中,我们需要对网站的并发量、响应时间进行测试,能测试这些点的第三方软件很 多,这不是本文的重点,本文中,我们利用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://image.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 ---------------------------------------------- 上面是一个很粗糙的代码片段,但如果将程序的输出信息写到一个文件中,并写一个相应数据分 析统 计代码,我们就能实现一个测试网页访问时间的代码。
时间: 2024-10-15 11:44:59