Linux 於 CLI 使用 CURL 想要測試看看網站的詳細連線速度,要怎麼做呢?
註:此篇「httpstat 分析 從 DNS查詢 到 下載完成各個階段的處理時間」也是使用 CURL 類似做法達成的,此篇是更簡化的版本。
CURL 於 CLI 顯示 TCP、SSL 的連線時間
CURL 要顯示詳細連線時間,可以使用下述命令:
- $ curl -o /dev/null -s -w "%{time_connect} + %{time_starttransfer} = %{time_total}\n" https://blog.longwin.com.tw
此篇文章作者將上述做點美化,下述摘錄自此篇:Timing Details With cURL – Joseph Scott
- vim ~/curl-format.txt
\n time_namelookup: %{time_namelookup}\n time_connect: %{time_connect} (TCP handshake)\n time_appconnect: %{time_appconnect} (SSL handshake)\n time_pretransfer: %{time_pretransfer}\n ¦time_redirect: %{time_redirect}\n time_starttransfer: %{time_starttransfer}\n ----------\n time_total: %{time_total}\n \n
- $ curl -w "@curl-format.txt" -so /dev/null https://blog.longwin.com.tw
- -w "@curl-format.txt" tells cURL to use our format file
- -o /dev/null redirects the output of the request to /dev/null
- -s tells cURL not to show a progress meter
上述的語法還是比較難記憶,寫成 Bash shell function 比較好用:
- vim ~/.bashrc
function curltime { # curl -o /dev/null -s -w "%{time_connect} + %{time_starttransfer} = %{time_total}\n" "$1" curl -w "@curl-format.txt" -so /dev/null "$1" }
- source ~/.bashrc
- curltime https://blog.longwin.com.tw
CURL 相關參數
CURL 除了上述的參數外,還有哪些參數可以使用呢?
- man curl # 搜尋 -w, --write-out <format>
- The variables available are:
- content_type:The Content-Type of the requested document, if there was any.
- filename_effective:The ultimate filename that curl writes out to. This is only meaningful if curl is told to write to a file with the --remote-name or --output option. It's most useful in combination with the --remote-header-name option.
- ftp_entry_path:The initial path curl ended up in when logging on to the remote FTP server.
- http_code:The numerical response code that was found in the last retrieved HTTP(S) or FTP(s) transfer. In 7.18.2 the alias response_code was added to show the same info.
- http_connect:The numerical code that was found in the last response (from a proxy) to a curl CONNECT request.
- local_ip:The IP address of the local end of the most recently done connection - can be either IPv4 or IPv6
- local_port:The local port number of the most recently done connection
- num_connects:Number of new connects made in the recent transfer.
- num_redirects:Number of redirects that were followed in the request.
- redirect_url:When an HTTP request was made without -L to follow redirects, this variable will show the actual URL a redirect would take you to.
- remote_ip:The remote IP address of the most recently done connection - can be either IPv4 or IPv6
- remote_port:The remote port number of the most recently done connection
- size_download:The total amount of bytes that were downloaded.
- size_header:The total amount of bytes of the downloaded headers.
- size_request:The total amount of bytes that were sent in the HTTP request.
- size_upload:The total amount of bytes that were uploaded.
- speed_download:The average download speed that curl measured for the complete download. Bytes per second.
- speed_upload:The average upload speed that curl measured for the complete upload. Bytes per second.
- ssl_verify_result:The result of the SSL peer certificate verification that was requested. 0 means the verification was successful.
- time_appconnect:The time, in seconds, it took from the start until the SSL/SSH/etc connect/handshake to the remote host was completed.
- time_connect:The time, in seconds, it took from the start until the TCP connect to the remote host (or proxy) was completed.
- time_namelookup:The time, in seconds, it took from the start until the name resolving was completed.
- time_pretransfer:The time, in seconds, it took from the start until the file transfer was just about to begin. This includes all pre-transfer
commands and negotiations that are specific to the particular protocol(s) involved. - time_redirect:The time, in seconds, it took for all redirection steps include name lookup, connect, pretransfer and transfer before the final transaction was started. time_redirect shows the complete execution time for multiple redirections.
- time_starttransfer:The time, in seconds, it took from the start until the first byte was just about to be transferred. This includes time_pre‐transfer and also the time the server needed to calculate the result.
- time_total:The total time, in seconds, that the full operation lasted. The time will be displayed with millisecond resolution.
- url_effective:The URL that was fetched last. This is most meaningful if you've told curl to follow location: headers.
相關網頁
- How do I measure request and response times at once using cURL?
- Timing HTTP Requests with cURL - overloaded.io
- Find the Time to First Byte Using Curl