使用CURL檢測Clinet側發起的HTTP請求各階段時間

來源:互聯網
上載者:User

標籤:使用curl檢測clinet側發起的http請求各階段時間

前言

上周,我方的一個客戶回函,訪問我們的介面,平均耗時在2s以上。但是我方對請求進入,和請求返回,整個過程都有監控,我方的耗時基本在50ms以內,非常快。

後來瞭解到,客戶從廣東訪問到我方上海,公網來訪問我方。那麼就建議去檢測,DNS耗時,TCP建立的耗時等。理論上,長距離的公網,網路延遲就非常高。遂建議使用CURL去檢查。結果果然如猜想,在TCP建立的耗時就很久。

進入正題,這篇文章主要介紹使用CURL檢測Client端發起的HTTP請求,各個階段的時間。


第一、HTTP請求的過程介紹

一個HTTP請求,涉及多個階段

1、DNS解析網域名稱

2、請求從Clinet路由至Server,Clinet與Server建立TCP串連

3、如果使用了HTTPS,還涉及SSL串連的建立

4、server開始準備資料

開始邏輯計算、調後端介面、查資料庫緩衝等

5、server開始傳遞資料

資料準備完成,開始給client傳資料

6、資料轉送完畢

7、整個過程可能還涉及多次重新導向



第二、關於CURL的介紹

CURL是利用URL文法在命令列方式下工作的開來源資料傳輸工具。

支援:DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, Telnet and TFTP. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, HTTP/2, cookies, user+password authentication (Basic, Plain, Digest, CRAM-MD5, NTLM, Negotiate and Kerberos), file transfer resume, proxy tunneling 等

最新版的curl穩定版為7.55.1(截止20170817)

原始碼:https://github.com/curl/curl




第三:用CURL檢測Clinet側發起的HTTP請求各階段時間,簡要說明

650) this.width=650;" src="https://s1.51cto.com/wyfs02/M00/9E/C1/wKioL1mVjoSRgA_ZAAGP7R3orEs850.png-wh_500x0-wm_3-wmp_4-s_3719930826.png" title="curl2.png" style="float:none;" alt="wKioL1mVjoSRgA_ZAAGP7R3orEs850.png-wh_50" />

1、TCP建立串連的耗時:CONNECT-NAMELOOKUP

2、建立TCP串連到server返回client第一個位元組的時間:

STARTTRANSFER-CONNECT

3、SERVER處理資料的時間:

可以用STARTTRANSFER - PRETRANSFER計算得到

4、CLIENT接收資料的耗時(開始接收至接收完成):

TOTAL-STARTTRANSFER



第四、例子:

curl -o /dev/null -s -w time_namelookup:"\t"%{time_namelookup}"\n"time_connect:"\t\t"%{time_connect}"\n"time_appconnect:"\t"%{time_appconnect}"\n"time_pretransfer:"\t"%{time_pretransfer}"\n"time_starttransfer:"\t"%{time_starttransfer}"\n"time_total:"\t\t"%{time_total}"\n"time_redirect:"\t\t"%{time_redirect}"\n"  https://www.yqb.com/

650) this.width=650;" src="https://s5.51cto.com/wyfs02/M02/9E/C1/wKioL1mVjobzldy3AAEPmtIEJNQ613.png-wh_500x0-wm_3-wmp_4-s_2512627456.png" title="curl3.png" style="float:none;" alt="wKioL1mVjobzldy3AAEPmtIEJNQ613.png-wh_50" />

1、DNS解析耗時:0.008s

2、TCP建立串連的耗時:0.059-0.008=0.051s

3、SSL握手完成耗時:0.228-0.059=0.169s

169ms,多了一層SSL還是很耗時的

4、server處理資料的時間:0.287-0.228=0.059

59ms,說明伺服器處理很快

5、總體的耗時:0.228s

6、整個過程沒有redirect,所以redirect的耗時為0



再舉一例:

650) this.width=650;" src="https://s4.51cto.com/wyfs02/M02/00/12/wKiom1mVjomxw8_qAACQJsxBKgc809.png-wh_500x0-wm_3-wmp_4-s_3139750192.png" title="curl4.png" style="float:none;" alt="wKiom1mVjomxw8_qAACQJsxBKgc809.png-wh_50" />

伺服器處理資料的耗時:2.305-0.014=2.291s

這就說明server端的效能是值得研究的。

對於server端而言,有需要分析它的耗時:

防火牆->負載平衡->應用->緩衝和DB

需要深入去分析這個時間消耗在哪個環節,有針對性的最佳化。




第五、詳細說明

NAMELOOKUP:從開始計算,網域名稱解析完成的耗時

CURLINFO_NAMELOOKUP_TIME. The time it took from the start until the name resolving was completed.

CONNECT:從開始計算,TCP建立完成的耗時

CURLINFO_CONNECT_TIME. The time it took from the start until the connect to the remote host (or proxy) was completed.

APPCONNECT:從開始計算,應用程式層(SSL,在TCP之上的應用程式層)串連/握手完成的耗時

CURLINFO_APPCONNECT_TIME. The time it took from the start until the SSL connect/handshake with the remote host was completed. (Added in in 7.19.0)

PRETRANSFER:從開始計算,準備開始傳輸資料的耗時

CURLINFO_PRETRANSFER_TIME. The time it took from the start until the file transfer is just about to begin. This includes all pre-transfer commands and negotiations that are specific to the particular protocol(s) involved.

STARTTRANSFER:從開始計算,開始傳輸資料的耗時(libcurl接收到第一個位元組)

CURLINFO_STARTTRANSFER_TIME. The time it took from the start until the first byte is received by libcurl.

TOTAL:總的耗時

CURLINFO_TOTAL_TIME. Total time of the previous request.

REDIRECT:整個過程重新導向的耗時,如果整個過程沒有重新導向,這個時間為0

CURLINFO_REDIRECT_TIME. The time it took for all redirection steps include name lookup, connect, pretransfer and transfer before final transaction was started. So, this is zero if no redirection took place.

另:python也有一個pycurl模組,大家可以嘗試。



參考:

https://curl.haxx.se/libcurl/c/curl_easy_getinfo.html


本文出自 “H2O's營運&開發路” 部落格,轉載請與作者聯絡!

使用CURL檢測Clinet側發起的HTTP請求各階段時間

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.