CDN 除了尝试快速交付您的内容(缩短延迟)之外,还可以通过不直接公开来帮助您保护/保护您的来源,查看这篇文章以了解其他好处:什么是 CDN?
关于您的测试,涉及许多因素,例如,所有新内容 ( MISS
) 由于尚未缓存,因此总是需要更多时间来提供服务,该内容已被预取和缓存 ( HIT
)。
您可以首先检查标题,例如在终端中运行:
$ curl -I https://immortal.run/img/immortal.png
你可能会看到这样的输出:
HTTP/2 200
date: Fri, 17 Aug 2018 07:51:20 GMT
content-type: image/png
content-length: 6757
set-cookie: __cfduid=d0be8792ec1e81d223eaa9e05b780a8fa1534492280; expires=Sat, 17-Aug-19 07:51:20 GMT; path=/; domain=.immortal.run; HttpOnly
last-modified: Thu, 07 Jun 2018 20:20:47 GMT
access-control-allow-origin: *
expires: Fri, 17 Aug 2018 11:51:20 GMT
cache-control: public, max-age=14400
x-github-request-id: 47B0:1A82:37CB24D:4D9A0A9:5B752825
cf-cache-status: HIT
accept-ranges: bytes
strict-transport-security: max-age=15552000; includeSubDomains; preload
x-content-type-options: nosniff
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
server: cloudflare
cf-ray: 44ba8e10cb6597aa-FRA
注意标题:
cf-cache-status: HIT
当资源被缓存并从 CDN 提供时,HIT
如果不是,它可能是MISS
,在这里您可以看到适用于 Cloudflare 的更多可能响应的列表:https: //support.cloudflare.com/hc/en-us /articles/200168266-What-do-the-various-Cloudflare-cache-responses-HIT-Expired-etc-mean-
现在要测试您可以使用的资源加载速度有多快curl
,根据您的 shell,您可能希望将下一个函数添加curl_time()
到其中~/.profile
, ~/.zshrc
或者~/.bashrc
curl_time() {
curl -o /dev/null -Ls -w " \
time_namelookup: %{time_namelookup}\n \
time_connect: %{time_connect}\n \
time_appconnect: %{time_appconnect}\n \
time_pretransfer: %{time_pretransfer}\n \
time_redirect: %{time_redirect}\n \
time_starttransfer: %{time_starttransfer}\n \
----------\n \
time_total: %{time_total}\n" "$1"
}
然后试试这样的东西:
$ curl_time https://immortal.run/img/immortal.png
time_namelookup: 0.133057
time_connect: 0.144885
time_appconnect: 0.200092
time_pretransfer: 0.200299
time_redirect: 0.000000
time_starttransfer: 0.416685
----------
time_total: 0.418580
在我随后的请求中,我得到了更快的交付time_total: 0.093495
:
$ curl_time https://immortal.run/img/immortal.png
time_namelookup: 0.004583
time_connect: 0.019833
time_appconnect: 0.067715
time_pretransfer: 0.067839
time_redirect: 0.000000
time_starttransfer: 0.091393
----------
time_total: 0.093495
如果只是想获得 total_time repeditely 你可以试试这个:
$ for i in {1..3}; \
curl -sL -w "%{time_total}\n" -o /dev/null https://immortal.run/img/immortal.png
分析标头和响应时间是检查 CDN 行为方式的良好起点。