【问题标题】:Measuring page speed and network time with Python like HTML5 Navigation Timing does?像 HTML5 Navigation Timing 一样使用 Python 测量页面速度和网络时间?
【发布时间】:2014-10-27 17:15:57
【问题描述】:

我目前正在开发一个客户端,它应该通过它的实际响应时间(以及单独的网络时间)来确定服务器的健康状况。我遇到了a implementationnew HTML5 feature,它基本上提供了我感兴趣的所有指标:

我知道我可以使用 Selenium 和 PhantomJS 并仅解析输出,但出于性能和可扩展性的原因,我更喜欢 urllib 或 requests 解决方案,它将其分解为 DNS 请求、网络上的时间和服务器响应的实际时间。

【问题讨论】:

  • 首先,忘记所有 dom 事件,因为您没有解析文档,只是从服务器接收纯字节。
  • 是的,当然,如前所述,我只对 DNS、网络时间和服务器响应时间感兴趣。
  • 启动 here 用于 DNS 查找,here 用于连接时间。

标签: python html performance python-requests


【解决方案1】:

为了以@Andre Daniel 给出的参考为基础,此代码通过 DNS 获取域 IP,并打印出经过的时间。然后它会获取一个 URL,打印时间,并在删除同一 URL 的 DNS 查找时间后打印经过的时间。

来源

import socket, time, urllib, urlparse

URL = 'http://example.com/123'

print URL, 'timing:'

urlinfo = urlparse.urlparse(URL)

start = time.time()
ip = socket.gethostbyname( urlinfo.netloc )
dns_tm = time.time()-start
print 'DNS:\t\t{:.3f} seconds'.format( dns_tm )

start = time.time()
_data = urllib.urlopen(URL).read()
load_tm = time.time()-start
print 'load:\t\t{:.3f} seconds'.format( load_tm )
print 'w/o DNS:\t{:.3f} seconds'.format( load_tm-dns_tm )

输出

http://example.com/123 timing:
DNS:        0.054 seconds
load:       0.104 seconds
w/o DNS:    0.050 seconds

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-14
    • 1970-01-01
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多