本页内容

  • 使用 Curl 测试网站速度
  • 总结

网站加载速度或响应时间对任何站长都非常重要,因为它会影响搜索引擎排名和用户体验。因此,如果你是系统管理员或站长,测试网站速度并立即采取措施加快速度就很重要。有多种基于网页和命令行的工具可用于测试你的网站速度。

在本教程中,我们将向你展示如何使用 Linux 中的 curl 命令测试网站加载速度。

使用 Curl 测试网站速度

Curl 是一个简单而强大的工具,用于在服务器之间传输数据。它还可用于通过各种变量来测试响应时间。

让我们测试网站 https://www.example.com 的加载速度。

curl -s -w 'Testing Website Response Time for :%{url_effective}\n\nLookup Time:\t\t%{time_namelookup}\nConnect Time:\t\t%{time_connect}\nPre-transfer Time:\t%{time_pretransfer}\nStart-transfer Time:\t%{time_starttransfer}\n\nTotal Time:\t\t%{time_total}\n' -o /dev/null https://www.example.com

你应该得到如下输出:

Testing Website Response Time for :https://www.example.com/

Lookup Time:		0.511
Connect Time:		0.565
Pre-transfer Time:	0.565
Start-transfer Time:	0.726

Total Time:		0.727

下面简要说明每个选项:

  • time_connect - 显示从 curl 检测到连接到第一个字节到达所用的时间(以秒为单位)。
  • time_namelookup - 显示从开始到名称解析完成所用的时间(以秒为单位)。
  • time_pretransfer - 显示从开始到文件传输即将开始所用的时间(以秒为单位)。
  • time_starttransfer - 显示从 curl 检测到连接到第一个字节到达所用的时间(以秒为单位)。
  • time_total - 执行该操作所用的总时间(以秒为单位)。
  • -s - 不显示进度条。
  • -w - 用于定义输出中显示的内容。
  • -o - 用于将完整输出写入 /dev/null。

如果你的网站是 HTTPS,可以运行以下命令:

curl -s -w 'Testing Website Response Time for :%{url_effective}\n\nLookup Time:\t\t%{time_namelookup}\nConnect Time:\t\t%{time_connect}\nAppCon Time:\t\t%{time_appconnect}\nRedirect Time:\t\t%{time_redirect}\nPre-transfer Time:\t%{time_pretransfer}\nStart-transfer Time:\t%{time_starttransfer}\n\nTotal Time:\t\t%{time_total}\n' -o /dev/null https://www.example.com

你应该得到如下输出:

Testing Website Response Time for :https://www.example.com/

Lookup Time:		0.511
Connect Time:		0.564
AppCon Time:		0.724
Redirect Time:		0.000
Pre-transfer Time:	0.724
Start-transfer Time:	1.085

Total Time:		1.264

如果你不想每次都运行一长串命令,可以创建一个名为 curl_test.txt 的文件,包含所有必需的选项:

nano curl_test.txt

添加以下几行:

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

保存并关闭文件,然后运行以下命令:

curl -w "@curl_test.txt" -o /dev/null -s https://www.example.com

你应该得到如下输出:

time_namelookup:  0.013
time_connect:  0.056
time_appconnect:  0.160
time_pretransfer:  0.160
time_redirect:  0.000
time_starttransfer:  0.511
----------
time_total:  0.753

你只需将 www.example.com 替换为你的网站名称或 IP 地址即可测试网站速度。

总结

在上面的指南中,你学习了如何使用 Curl 命令测试网站加载速度。希望这能帮助你测试网站的速度。