原文地址:https://www.lujun9972.win/blog/2021/06/08/使用curl进行网站测速/index.html

网站访问可以分为下面几个阶段:

  • DNS 域名解析
  • 通过SSL协议交换密钥(HTTPS)
  • 与服务端创建 TCP 访问连接
  • 客户端发起请求
  • 服务端收到请求并准备回应内容
  • 服务端回复回应内容

当我们优化网站速度时通常要现确定哪个阶段是耗时大头。而令我感到惊奇的是,原来 curl 的 -w 选项可以让 curl 完成访问网站的操作后将各阶段的耗时情况(以及其他一些访问信息)输出到标准输出。

man curl 中关于 -w 选项的说明如下:

-w, --write-out <format>Make curl display information on stdout after a completed trans‐fer. The format is a string that may contain  plain  text  mixedwith  any  number of variables. The format can be specified as aliteral "string", or you can have curl read the  format  from  afile  with  "@filename" and to tell curl to read the format fromstdin you write "@-".The variables present in the output format will  be  substitutedby  the  value or text that curl thinks fit, as described below.All variables are specified as %{variable_name} and to output  anormal  % you just write them as %%. You can output a newline byusing \n, a carriage return with \r and a tab space with \t.NOTE: The %-symbol is a special symbol in the win32-environment,where  all  occurrences  of  %  must  be doubled when using thisoption.The variables available are:content_type   The Content-Type of the  requested  document,  ifthere was any.filename_effectiveThe  ultimate  filename  that curl writes out to.This is only meaningful if curl is told to  writeto  a  file  with  the  -O,  --remote-name or -o,--output option. It's most useful in  combinationwith  the -J, --remote-header-name option. (Addedin 7.26.0)ftp_entry_path The initial path curl ended up in when logging onto the remote FTP server. (Added in 7.15.4)http_code      The numerical response code that was found in thelast retrieved HTTP(S)  or  FTP(s)  transfer.  In7.18.2  the alias response_code was added to showthe same info.http_connect   The numerical code that was  found  in  the  lastresponse   (from  a  proxy)  to  a  curl  CONNECTrequest. (Added in 7.12.4)http_version   The  http  version  that  was  effectively  used.(Added in 7.50.0)local_ip       The  IP  address  of  the  local  end of the mostrecently done connection - can be either IPv4  orIPv6 (Added in 7.29.0)local_port     The  local  port number of the most recently doneconnection (Added in 7.29.0)num_connects   Number of new connects made in the recent  trans‐fer. (Added in 7.12.3)num_redirects  Number  of  redirects  that  were followed in therequest. (Added in 7.12.3)proxy_ssl_verify_resultThe result of the HTTPS proxy's SSL peer certifi‐cate verification that was requested. 0 means theverification was successful. (Added in 7.52.0)redirect_url   When an HTTP request was made without -L, --loca‐tion  to follow redirects (or when --max-redir ismet), this variable will show the  actual  URL  aredirect would have gone to. (Added in 7.18.2)remote_ip      The  remote  IP address of the most recently doneconnection - can be either IPv4 or IPv6 (Added in7.29.0)remote_port    The  remote port number of the most recently doneconnection (Added in 7.29.0)scheme         The URL scheme (sometimes called  protocol)  thatwas effectively used (Added in 7.52.0)size_download  The total amount of bytes that were downloaded.size_header    The total amount of bytes of the downloaded head‐ers.size_request   The total amount of bytes that were sent  in  theHTTP request.size_upload    The total amount of bytes that were uploaded.speed_download The average download speed that curl measured forthe complete download. Bytes per second.speed_upload   The average upload speed that curl  measured  forthe complete upload. Bytes per second.ssl_verify_resultThe  result of the SSL peer certificate verifica‐tion that was requested. 0 means the verificationwas successful. (Added in 7.19.0)time_appconnectThe  time,  in  seconds,  it  took from the startuntil the SSL/SSH/etc  connect/handshake  to  theremote host was completed. (Added in 7.19.0)time_connect   The  time,  in  seconds,  it  took from the startuntil the TCP connect  to  the  remote  host  (orproxy) was completed.time_namelookupThe  time,  in  seconds,  it  took from the startuntil the name resolving was completed.time_pretransferThe time, in seconds,  it  took  from  the  startuntil  the file transfer was just about to begin.This includes all pre-transfer commands and nego‐tiations that are specific to the particular pro‐tocol(s) involved.time_redirect  The time, in seconds, it took for all redirectionsteps including name lookup, connect, pretransferand transfer before  the  final  transaction  wasstarted.  time_redirect shows the complete execu‐tion time for multiple  redirections.  (Added  in7.12.3)time_starttransferThe  time,  in  seconds,  it  took from the startuntil the first byte was just about to be  trans‐ferred.  This  includes time_pretransfer and alsothe time  the  server  needed  to  calculate  theresult.time_total     The  total time, in seconds, that the full opera‐tion lasted.url_effective  The URL that was fetched last. This is most mean‐ingful  if  you've  told curl to follow location:headers.If this option is used several times, the last one will be used.

其中与时间相关的变量包括:

time_namelookup
DNS 解析时间,可以与 --resolve 选项配合寻找最快的DNS
time_connect
与服务端创建好 TCP 连接的时间,严格来说是客户端回复 ACK 的时间。我们可以通过 time_connect - time_namelookup 来大致推断网络延时。
time_appconnect
完成 SSL/TLS 设置的时间,此时客户端与服务端完成密钥交换,客户端准备发起请求
time_pretransfer
服务端收到请求的时间
time_starttransfer
服务端准备好回应内容的时间。
time_total
完成整个请求的所有时间
time_redirect
若请求经过多次重定向,那么这个包含直到最后一次请求开始所耗的时间。

下面这张从 cloudflare 偷来的例子可以很直观的看出每个变量的对应关系

curl -so /dev/null -w "dnslookup: %{time_namelookup} | connect: %{time_connect} | appconnect: %{time_appconnect} | pretransfer: %{time_pretransfer} | starttransfer: %{time_starttransfer} | total: %{time_total} | size: %{size_download}\n" https://www.zasag.mn

其中 -s 表示进入 silent 模式, -o /dev/null 表示不显示获取到的文件内容

结果为:

dnslookup: 1.510 | connect: 1.757 | appconnect: 2.256 | pretransfer: 2.259 | starttransfer: 2.506 | total: 3.001 | size: 53107

图示如下:

使用curl进行网站测速相关推荐

  1. python实现网站测速软件_网站测速插件是什么-和网站测速插件相关的问题-阿里云开发者社区...

    回2楼ivmmff的帖子 工作太忙了 一口气写不完 ......... 在这里写完了 在移动过去 ------------------------- Re网站加速指南-GoogleAnalytics- ...

  2. 香港服务器几种网站测速的方法

    现在有很多朋友租用香港服务器,但是在租用香港服务器之前,我们必须先测试一下服务器的访问速度. 而且,慢的网站速度也可以通过CDN来加速,如下: 因为香港服务器的访问速度对于网站来说非常重要,尤其是网站 ...

  3. 12个在线网站测速工具——web性能

    网站的访问速度对于一个网站非常重要,没有一个人会喜欢速度慢的网站.下面是12款在线测试网站的访问速度和性能的工具,在优化网站的时候很有用. Pingdom 测试网站每个元素的加载速度,比如html.图 ...

  4. 10个免费在线网站测速工具

    10个免费在线网站测速工具 In: 站长工具 By:MK 4十二2011 我们知道网站载入速度对用户体验非常重要,还是SEO优化内容的一个重要部分.借助在线网站测速工具,了解影响网站载入速度慢的根本原 ...

  5. 网站测速服务 查看自己网站在全球的打开速度

    网站打开速度是一个值得重视的问题,影响效率和心情.自从更换主题以来,不断对其进行优化,闲着没事也测试用各种服务进行网站访问加载速度测试.虽然很多开发工具都带有速度测试功能,但那毕竟只是本机测试嘛. 因 ...

  6. 13个免费网站测速服务

    访问速度对于一个网站或博客来说实在太重要了,据有关部门的估计:一般一个浏览者如果在5-8秒钟没有打开一个网站,那么该网站会丢失1/3的浏览用户.超过10秒,你的访客绝对会失去耐心!今天不讲如何改进访问 ...

  7. python实现网站测速软件_python实现网站友情链接查询与网站死链接查询的两个脚步...

    在前几天写的一建抓取网站所有链接的脚步往后衍生了以下的两个脚步,一个是查询网站友情链接,另一个是查询网站的死链.我这里只是初步实现了功能,还有很多地方需要优化,比如说查询友情链接脚步会存在带www与不 ...

  8. python 实现网站测速_python一键测试网速

    python一键测试网速 CentOS7.4 #git clone https://github.com/sivel/speedtest-cli.git #cd speedtest-cli/ #./s ...

  9. python 实现网站测速_技术|tespeed-测试网速的Python工具

    许多电脑呆子知道可以用speedtest.net测试网速,但是这个不能在测试中给你足够的控制.Linux用户喜欢在终端中输入命令来完成任务,至少我就是这样的. tespeed是一款有很多特性的pyth ...

最新文章

  1. deepin tim(wine)无法安装_利用HyperV虚拟机,如何在Win10上安装Deepin国产操作系统?深度好文!...
  2. 基于mimeTex的数学公式Webservice的部署和实现
  3. 如何从0到1构建一个稳定、高性能的Redis集群?(附16张图解)
  4. 基于RDKit探索DrugBank(demo)
  5. bzoj 1010: [HNOI2008]玩具装箱toy 2011-12-27
  6. 12. final修饰符
  7. python从标准输入读取数据_在PYTHON中如何从标准输入读取内容stdin
  8. 俄罗斯将封杀LinkedIn 推动个人数据本地化
  9. Redis(1) 简介以及linux环境下的安装
  10. 学习环境配置:Manjaro、MSYS2以及常见软件
  11. RFID开发利器 proxmark3
  12. struts2漏洞_Apache Struts2057远程代码执行漏洞复现
  13. GP学习整理(一)—Geoprocessing assembly and Geoprocessor managed assembly
  14. maven配置(myeclipse版)
  15. Java I/O总结——OutputStream
  16. oppo r11s鸿蒙固件,OPPO R11s官方固件rom刷机包_OPPO R11s系统升级更新包下载
  17. 【HAVENT原创】kubernetes docker 常用指令
  18. 云知声完成C轮系列融资13亿 多支国家背景基金参与
  19. 忘记了Excel工作表保护密码的解决办法
  20. 2021年海河英才计划天津落户天津最详细过程

热门文章

  1. MPLS VPN常见问题
  2. PCB单双面板打样工程费跨入30元时代!
  3. 耳下长包图片_【耳廓里长了个硬包图片】_表现_症状-大众养生网
  4. linux ssd iops测试,Linux系统硬盘的IOPS测试
  5. java 流程引擎_java工作流引擎Jflow父子流程demo
  6. 全志AXP209电源管理芯片介绍
  7. linux学习——linux中ls -ld 的意思
  8. 勿忘初心,保持饥渴的心态
  9. 快速入门 | 篇十七:运动控制器多轴插补运动指令的使用
  10. Harris Corner(Harris角检测)