原文地址: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 mixed with any number of variables. The format can be specified as a literal "string", or you can have curl read the format from a file with "@filename" and to tell curl to read the format from stdin you write "@-". The variables present in the output format will be substituted by the value or text that curl thinks fit, as described below. All variables are specified as %{variable_name} and to output a normal % you just write them as %%. You can output a newline by using \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 this option. The variables available are: content_type The Content-Type of the requested document, if there was any. filename_effective The ulti