使用curl命令測試延時

最近在研究如何測試代理服務器的速度以便選出最快的服務器,找了一圈發現其實系統自帶的curl命令就能實現這個功能。

curl命令的各個參數的介紹網上有很多,這裏只給出幾個常用的:
curl 不輸出任何信息,安靜模式,走代理:

curl -x socks5://127.0.0.1:1086 -m 10 -s -o /dev/null www.baidu.com

-m 整個訪問的超時時間

-s 安靜模式,不輸出連接速度等信息

-o path 輸出output到path
其中/dev/null是個特殊路徑:
/dev/null null是一個名叫null小桶的東西,如果命令的輸出不想要即想丟棄輸出的內容,既不想在標準輸出與不想重定向到某個文件,就可將命令的輸出重定向到/dev/null。這樣做的好處是不會因爲輸出的內容過多而導致文件大小不斷的增加。

-x 走代理:

 -x, --proxy [protocol://]host[:port] Use this proxy
     --proxy-anyauth Pick any proxy authentication method
     --proxy-basic   Use Basic authentication on the proxy
     --proxy-cacert <file> CA certificate to verify peer against for proxy
     --proxy-capath <dir> CA directory to verify peer against for proxy
     --proxy-cert <cert[:passwd]> Set client certificate for proxy
     --proxy-cert-type <type> Client certificate type for HTTPS proxy
     --proxy-ciphers <list> SSL ciphers to use for proxy
     --proxy-crlfile <file> Set a CRL list for proxy
     --proxy-digest  Use Digest authentication on the proxy
     --proxy-header <header/@file> Pass custom header(s) to proxy
     --proxy-insecure Do HTTPS proxy connections without verifying the proxy
     --proxy-key <key> Private key for HTTPS proxy
     --proxy-key-type <type> Private key file type for proxy
     --proxy-negotiate Use HTTP Negotiate (SPNEGO) authentication on the proxy
     --proxy-ntlm    Use NTLM authentication on the proxy
     --proxy-pass <phrase> Pass phrase for the private key for HTTPS proxy
     --proxy-pinnedpubkey <hashes> FILE/HASHES public key to verify proxy with
     --proxy-service-name <name> SPNEGO proxy service name
     --proxy-ssl-allow-beast Allow security flaw for interop for HTTPS proxy
     --proxy-tls13-ciphers <ciphersuite list> TLS 1.3 proxy cipher suites
     --proxy-tlsauthtype <type> TLS authentication type for HTTPS proxy
     --proxy-tlspassword <string> TLS password for HTTPS proxy
     --proxy-tlsuser <name> TLS username for HTTPS proxy
     --proxy-tlsv1   Use TLSv1 for HTTPS proxy

知道了這些,就成功了一半。

爲了統計延時,需要用到curl命令中自帶的一些宏定義。

time_namelookup:DNS 解析域名的時間

time_commect:client和server端建立TCP 連接的時間

time_starttransfer:從client發出請求;到web的server 響應第一個字節的時間

time_total:client發出請求;到web的server發送會所有的相應數據的時間

speed_download:下週速度 單位 byte/s

我們來完善一下上面的命令:

➜  ~ curl -o /dev/null -x socks5://127.0.0.1:1086 -s -w %{time_namelookup}---%{time_connect}---%{time_starttransfer}---%{time_total}---%{speed_download}"\n" www.baidu.com
0.000072---0.020414---0.587005---0.587225---4056.000

上面這條命令及返回結果可以這麼理解:

0.000072: DNS 服務器解析域名的時間單位是0.000072s

0.020414: client發出請求,到c/s 建立TCP 的時間;裏面包括DNS解析的時間

0.587005: client發出請求;到s響應發出第一個字節開始的時間;包括前面的2個時間

0.587225: client發出請求;到s把響應的數據全部發送給client;並關閉connect的時間

4056.000 :下載數據的速度

建立TCP連接到server返回client第一個字節的時間:0.587005s – 0.020414s = 0.566591s

server把響應數據發送給client的時間:0.587225s – 0.587005s = 0.00022s

當然,還有很多:

url_effective 
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. (Added in 7.25.1)

http_code http狀態碼,如200成功,301轉向,404未找到,500服務器錯誤等。(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. (Added in 7.12.4)

time_total 總時間,按秒計。精確到小數點後三位。 (The total time, in seconds, that the full operation lasted. The time will be displayed with millisecond resolution.)

time_namelookup DNS解析時間,從請求開始到DNS解析完畢所用時間。(The time, in seconds, it took from the start until the name resolving was completed.)

time_connect 連接時間,從開始到建立TCP連接完成所用時間,包括前邊DNS解析時間,如果需要單純的得到連接時間,用這個time_connect時間減去前邊time_namelookup時間。以下同理,不再贅述。(The time, in seconds, it took from the start until the TCP connect to the remote host (or proxy) was completed.)

time_appconnect 連接建立完成時間,如SSL/SSH等建立連接或者完成三次握手時間。(The time, in seconds, it took from the start until the SSL/SSH/etc connect/handshake to the remote host was completed. (Added in 7.19.0))

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 重定向時間,包括到最後一次傳輸前的幾次重定向的DNS解析,連接,預傳輸,傳輸時間。(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. (Added in 7.12.3))

time_starttransfer 開始傳輸時間。在client發出請求之後,Web 服務器返回數據的第一個字節所用的時間(The time, in seconds, it took from the start until the first byte was just about to be transferred. This includes time_pretransfer and also the time the server needed to calculate the result.)

size_download 下載大小。(The total amount of bytes that were downloaded.)

size_upload 上傳大小。(The total amount of bytes that were uploaded.)

size_header  下載的header的大小(The total amount of bytes of the downloaded headers.)

size_request 請求的大小。(The total amount of bytes that were sent in the HTTP request.)

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.)

content_type 就是content-Type,不用多說了,這是一個訪問我博客首頁返回的結果示例(text/html; charset=UTF-8)(The Content-Type of the requested document, if there was any.)

num_connects Number of new connects made in the recent transfer. (Added in 7.12.3)

num_redirects Number of redirects that were followed in the request. (Added in 7.12.3)

redirect_url When a HTTP request was made without -L to follow redirects, this variable will show the actual URL a redirect would take you to. (Added in 7.18.2)

ftp_entry_path The initial path libcurl ended up in when logging on to the remote FTP server. (Added in 7.15.4)

ssl_verify_result ssl認證結果,返回0表示認證成功。( The result of the SSL peer certificate verification that was requested. 0 means the verification was successful. (Added in 7.19.0))

當然,如果你覺得這麼顯示不容易看的話,還可以指定格式化文件:
創建如下文本文件:

➜  vim curl-time  
\n  
              http: %{http_code}\n  
               dns: %{time_namelookup}s\n  
          redirect: %{time_redirect}s\n  
      time_connect: %{time_connect}s\n  
   time_appconnect: %{time_appconnect}s\n  
  time_pretransfer: %{time_pretransfer}s\n  
time_starttransfer: %{time_starttransfer}s\n  
     size_download: %{size_download}bytes\n  
    speed_download: %{speed_download}B/s\n  
                  ----------\n  
        time_total: %{time_total}s\n  
\n 

然後利用-w參數指定格式化文件,最終效果:

curl -o /dev/null -x socks5://127.0.0.1:1086 -s -w "@curl-time"  www.google.com

                http: 200
                 dns: 0.000070s
            redirect: 0.000000s
        time_connect: 0.018721s
     time_appconnect: 0.000000s
    time_pretransfer: 0.018743s
  time_starttransfer: 0.274720s
       size_download: 13065bytes
      speed_download: 47509.000B/s
                    ----------
          time_total: 0.275102s
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章