使用 Python 请求测量网站加载时间 [英] Measure website load time with Python requests
问题描述
我正在尝试构建一个工具来测试我的互联网连接延迟,更具体地说是网站加载时间.我想使用 python requests 模块来加载部分.
I'm trying to build a tool for testing the delay of my internet connection, more specifically web site load times. I thought of using the python requests module for the loading part.
问题是,它没有内置功能来测量获得完整响应所需的时间.为此,我想我会使用 timeit
模块.
Problem is, it's got no built-in functionality to measure the time it took to get the full response. For this I thought I would use the timeit
module.
我不确定的是,如果我像这样运行 timeit:
What I'm not sure about is that if I run timeit like so:
t = timeit.Timer("requests.get('http://www.google.com')", "import requests")
我是真的在测量响应到达所用的时间,还是请求构建、发送、接收等所用的时间?我猜我可能会忽略执行时间,因为我正在测试具有很长延迟(约 700 毫秒)的网络?
I'm I really measuring the time it took the response to arrive or is it the time it takes for the request to be built, sent, received, etc? I'm guessing I could maybe disregard that excecution time since I'm testing networks with very long delays (~700ms)?
有没有更好的方法以编程方式执行此操作?
Is there a better way to do this programatically?
推荐答案
至于你的问题,应该是总时间
As for your question, it should be the total time for
- 创建请求对象的时间
- 发送请求
- 接收回复
- 解析响应(参见 Thomas Orozco 的评论)
测量单个请求加载时间的其他方法是使用 urllib:
Other ways to measure a single request load time is to use urllib:
nf = urllib.urlopen(url)
start = time.time()
page = nf.read()
end = time.time()
nf.close()
# end - start gives you the page load time
这篇关于使用 Python 请求测量网站加载时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!