使用Python请求衡量网站加载时间 [英] Measure website load time with Python requests

查看:630
本文介绍了使用Python请求衡量网站加载时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个工具来测试我的Internet连接的延迟,更具体地说是网站加载时间.我想到了将python 请求模块用于加载部分.

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")

我实际上是在衡量响应到达所花费的时间,还是构建,发送,接收请求所花费的时间?我猜我可能会忽略执行时间,因为我正在测试延迟很长(〜700ms)的网络?

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?

推荐答案

对于您的问题,应该是

  1. 创建请求对象的时间
  2. 发送请求
  3. 接收响应
  4. 解析响应(请参阅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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆