劉亮谷
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# 11/18 如何分析 server 效能 1. 介紹網路 2. 流量分散 3. 討論大量IO、linux kernel server * linux system call - sendfile、splice * pipe 4. level、edge triggger * HTTP transactions 5. 做unit test,分別用perf測,注意IO 6. sockbuf(socket buffer linux) 7. server type 8. linux kernel server copy to user ## 網路基礎概念 ![](http://linux.vbird.org/linux_server/0110network_basic//osi_tcpip.gif) ## HTTP Transaction ### 介紹 Understanding how HTTP works and what each component in an HTTP transaction means, is key to interpreting the data collected by any monitoring service. HTTP 資料架構於 TCP 協議之上,這樣的作法可以保證資料傳遞的可靠性,並且將大量的 data requests 和 responses 分成網路可管理的區塊。 而其中 TCP 是一種"連接"導向的協定,這意味著當 client 與 server 開始對話時, TCP 協定將建立一個連結,通過該連結可以可靠地進行 HTTP 的數據傳輸;而當對話完成時,該連結會被關閉。 HTTP協議中的所有數據都以人類可讀的ASCII文本表示。 :::info 原文: HTTP data rides above the TCP protocol, which guarantees reliability of delivery, and breaks down large data requests and responses into network-manageable chunks. TCP is a “connection” oriented protocol, which means when a client starts a dialogue with a server the TCP protocol will open a connection, over which the HTTP data will be reliably transferred, and when the dialogue is complete that connection should be closed. All of the data in the HTTP protocol is expressed in human-readable ASCII text. ::: ---- ### Simple HTTP Transaction A simple HTTP transaction is one where the client makes a single request for HTTP content. ![](http://assetsblogfly2.catchpoint.com/wp-content/uploads/2010/09/simplehttp1.png) 1. DNS Lookup : 客戶端嘗試解析其送 request 的域名。 a. 客戶端向本地的 ISP (網際網路服務供應商( Internet Service Provider ) DNS server 發送DNS查詢的 request 。 b. DNS server 會 response 其 IP地址。 2. Connect: 客戶端使用目的網站的IP地址建立TCP連接。 a. 客戶端向目標伺服器發送 SYN 數據包(SYN packet) b. Web server 發送 SYN-ACK 數據包 c. 客戶端用 ACK 數據包進行回覆,結束三向 TCP 的連接建立。 >SYN packet is sent to indicate that a new connection is to be established. 3. Send: 客戶端將 HTTP request 送到 web server。 4. Wait: 客戶端等待伺服器回應請求。 a. Web server 處理請求、尋找資源,並將回應發送回客戶端。客戶端從 Web server 接收第一個數據包的第一個 byte,其中包含 HTTP Response headers 與 content。 5. Load:客戶端載入回應的內容。 a. Web server 發送第二個 TCP 段,並設置 PSH flag 。 b. 客戶端發送 ACK 。 (客戶端每接收兩個 segments 救回傳 ACK ) c. Web server 使用 HTTP_Continue 發送第三個 TCP segment。 6. Close:客戶端發送FIN數據包以關閉TCP連接。 ---- ### Serial HTTP Connection #### 簡介: 當多個 request 有順序的發送伺服器時,會發生 Serial HTTP Connection ,並且每個請求( request )都會建立一個新的連結( Connection )。 這種方式現在是比較少發生的,因為所有現代的瀏覽器都支援 parellel Connection 。但是,當瀏覽器或伺服器僅支持 HTTP 1.0,而不支持 Keep Alive(或HTTP 1.0 +),並且第一個請求是 blocking request時,Serial HTTP Connection 還是可能發生。 ![](http://assetsblogfly2.catchpoint.com/wp-content/uploads/2010/09/serialhttp1.png) * Request I 1. DNS Lookup: 客戶端嘗試解析其送 request 的域名。 a. 客戶端向本地的 ISP DNS server 發送DNS查詢的 request 。 b. DNS server 會 response 其 IP地址。 2. Connect: 客戶端使用目的網站的 IP 地址建立 TCP 連接(1)。 a. 客戶端向目標伺服器發送 SYN 數據包(SYN packet) b. Web server 發送 SYN-ACK 數據包 c. 客戶端用 ACK 數據包進行回覆,結束三向 TCP 的連接建立。 3. Send: 客戶端將 HTTP request 送到 web server。 4. Wait: 客戶端等待伺服器回應請求。 5. Load: 客戶端載入回應的內容。 * Request II 1. Connect : 客戶端使用目的網站的 IP 地址建立 TCP 連接(2)。 2. Send : 客戶端將 HTTP request 送到 web server。 3. Wait: 客戶端等待伺服器回應請求。 4. Load: 客戶端載入回應的內容。 ---- ### Persistent HTTP Transactions #### 簡介: Persistent Connection 允許瀏覽器/ HTTP 客戶端對同一主機名的不同請求使用相同的連接。 HTTP 1.1 協定本身就支援 Persistent Connection ,不需要增加其他的 HTTP header 。對於 HTTP 1.0 而言,Persistent Connection 需透過 Keep-Alive HTTP header 來達成。 ![](http://assetsblogfly2.catchpoint.com/wp-content/uploads/2010/09/persistent1.png) 1. DNS Lookup: 客戶端嘗試解析其送 request 的域名。 a. 客戶端向本地的 ISP DNS server 發送DNS查詢的 request 。 b. DNS server 會 response 其 IP地址。 2. Connect: 客戶端使用目的網站的 IP 地址建立 TCP 連接(1)。 a. 客戶端向目標伺服器發送 SYN 數據包(SYN packet) b. Web server 發送 SYN-ACK 數據包 c. 客戶端用 ACK 數據包進行回覆,結束三向 TCP 的連接建立。 3. Send: 客戶端將 HTTP request 送到 web server。 4. Wait: 客戶端等待伺服器回應請求。 5. Load: 客戶端載入回應的內容。 6. Send: 客戶端將 HTTP request 送到 web server。 7. Wait: 客戶端等待伺服器回應請求。 8. Load: 客戶端載入回應的內容。 ### 優勢 * 較少的CPU和記憶體的使用(由於同時開啟的連線的減少了) * 允許請求和應答的HTTP管線化 * 降低網路阻塞 (TCP連線減少了) * 減少了後續請求的延遲(無需再進行握手) * 報告錯誤無需關閉TCP連線 根據RFC 2616 (47頁),用戶用戶端與任何伺服器和代理伺服器之間不應該維持超過2個連結。代理伺服器應該最多使用2×N個持久連線到其他伺服器或代理伺服器,其中N是同時活躍的用戶數。這個指引旨在提高HTTP回應時間並避免阻塞。 ### 劣勢 >對於現在的廣泛普及的寬頻連線來說,Keep-Alive也許並不像以前一樣有用。web伺服器會保持連線若干秒(Apache中預設15秒),這與提高的效能相比也許會影響效能。 對於單個檔案被不斷請求的服務(例如圖片存放網站),Keep-Alive可能會極大的影響效能,因為它在檔案被請求之後還保持了不必要的連線很長時間。 by wikipedia. ---- ### Parallel HTTP Transactions #### 簡介 HTTP 1.1協定為瀏覽器提供了平行打開多個連結和執行 HTTP request 的能力。 (這也可以透過 HTTP1.0 與 Keep-Alive 辦到,但實作方式略有不同。) >不同在哪,需要再研究ㄧ下 [name=亮谷] 舉例來說,下圖為瀏覽器從 hostname.com 載入一個 HTML 頁面,其中包含兩個對 hostname.com 的圖像請求和兩個對 adserver.com 的請求。 ![](http://assetsblogfly2.catchpoint.com/wp-content/uploads/2010/09/parallelconnections1.png) 一旦第一請求完成後,則平行執行後續請求: ![](http://assetsblogfly2.catchpoint.com/wp-content/uploads/2010/09/table21.png) ![](https://i.imgur.com/I5to94l.png) ### HTTP pipelining #### 定義: * Parallel connections: Concurrent HTTP requests across ==multiple== TCP connections * Persistent connections: Reusing TCP connections to eliminate connect/close delays * Pipelined connections: Concurrent HTTP requests across a ==shared== TCP connection * Multiplexed connections: Interleaving chunks of requests and responses (experimental) ![](https://i.imgur.com/jMtqm74.png) ## linux kernel system call ### general I/O ![image alt](https://www.ibm.com/developerworks/cn/linux/l-cn-zerocopy2/image001.jpg) ### sendfile 在兩個 file descriptor 之間傳輸數據,不用 copy 。 但輸入的 file descriptor 必須是真正的文件, 輸出的 file descriptor 可以是 socket。 sendfile() 系統呼叫不需要將資料複製或者映射到應用程式的地址空間去,所以 sendfile() 只是適用於應用程式地址空間不需要對所訪問的資料進行處理的情況。 這個 sendfile 可以應用於,當 http 伺服器,直接把html原文件讀出來發送給客戶可對應的socket時。 ![image alt](https://www.ibm.com/developerworks/cn/linux/l-cn-zerocopy2/image003.jpg) ### splice 在兩個 file descriptor 之間傳輸數據,不用拷貝。但輸入和輸出文件描述符必須有一個是 pipe 。也就是說如果你需要從一個socket 傳輸數據到另外一個 socket,是需要使用 pipe 來做為中介。 ## level、edge triggger 在`man epoll`中,可以發現有很大的篇幅在討論 level 與 edge triggger , 其中提到一個例子如下。 1. 有一個 file descriptor 表示 pipe 的讀取端(rfd)在epoll 中被註冊。 2. pipe writer 在管道的寫入端寫入2 kB的資料。 3. 做完 epoll_wait(2) 的 system call 後,將回傳 rfd 作為 ready file descriptor。 4. pipe reader 從 rfd 讀取 1 kB 的數據。 5. 完成 epoll_wait(2)。 討論: 如果使用EPOLLET(邊緣觸發)的方式將 rfd 放入到 epoll 中,則有可能在( 5 )時 call 完 epoll_wait(2)後即停止,儘管文件輸入緩衝區中仍存在可用的資料;同時遠端的對應裝置可能預期會得到已經發送的資料請求的回覆。 其原因是邊緣觸發模式僅在監視的 file descriptor 發生更改時,傳遞事件。 因此,在步驟(5)中, caller 可能一直在等待已經存在於 input buffer 的某些資料。 ## 以 perf 進行 server profiling ![](http://www.brendangregg.com/perf_events/perf_events_map.png) ### pipe ![](https://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/images/Chapter3/3_22_PipeFileDescriptors.jpg) ### DNS server ## 附件 * 觀察所有的網路介面: `ifconfig` * list DNS servers used by my system ```shell nmcli device show <interfacename> | grep IP4.DNS ``` * morden web behavior ![image alt](http://www.webstepbook.com/supplements-2ed/slides/images/ajax_request.png) ## 待辦事項 - [ ] 利用 iperf 測試網路效能 - [ ] Wireshark觀測server 進行分析 - [ ] 研究 linux socket buffer - [ ] 讀完 HTTP: The Definitive Guide ## Reference * [perf Examples](http://www.brendangregg.com/perf.html#Events) * [Anatomy of an HTTP Transaction by catchpoint](http://blog.catchpoint.com/2010/09/17/anatomyhttp/) * [What are the disadvantage(s) of using HTTP pipelining?](http://stackoverflow.com/questions/14810890/what-are-the-disadvantages-of-using-http-pipelining) * [HTTP 連線管理](https://ihower.tw/blog/archives/1517) * [HTTP: The Definitive Guide Part 1](https://ihower.tw/blog/archives/1514) * [HTTP: The Definitive Guide](http://www.staroceans.org/e-book/O'Reilly%20-%20HTTP%20-%20The%20Definitive%20Guide.pdf) * [第十九章、主機名稱控制者: DNS 伺服器](http://linux.vbird.org/linux_server/0350dns.php) * [Optimizing Page Load Time](https://www.die.net/musings/page_load_time/) * [HTTP Archive (HAR) format](https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/HAR/Overview.html) * [Linux 中的零拷貝技術,第2 部分](https://www.ibm.com/developerworks/cn/linux/l-cn-zerocopy2/) * [Linux 的 splice 和sendfile系統調用](https://read01.com/eP8MRM.html)

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully