[英]python urllib2 timing
我想收集与Web请求的每个阶段需要多长时间相关的统计信息。 httplib
提供:
def run(self): conn = httplib.HTTPConnection('www.example.com') start = time.time() conn.request('GET', '/') request_time = time.time() resp = conn.getresponse() response_time = time.time() conn.close() transfer_time = time.time() self.custom_timers['request sent'] = request_time - start self.custom_timers['response received'] = response_time - start self.custom_timers['content transferred'] = transfer_time - start assert (resp.status == 200), 'Bad Response: HTTP %s' % resp.status
这些统计数据是否来自urllib2
等更高级别的界面? 是否有提供此类统计数据的高级图书馆?
time.time不是最可靠和最精确的。 您可以在python中使用timeIt模块进行分析。 http://docs.python.org/library/timeit.html这是一个使用timeit的代码片段
statmnt = 'print "Replace print with the snippet you want to profile"'
setup = 'print "Replace this line with some snippet specific imports"'
n = 1 #Number of times you want the timeit module to execute the statmnt
t = timeit.Timer(statmnt, setup)
qTime = t.timeit(n)
在您的情况下,您将为请求,响应和内容提供创建三个timeit对象。 有关模块时间的更多信息,请参阅文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.