当前位置:网站首页>TCP Performance Analysis and optimization strategy

TCP Performance Analysis and optimization strategy

2020-11-10 17:23:45 InfoQ

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":" Network transmission "}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Propagation delay : The time it takes for a message to travel from the sender to the receiver , It's a function of the distance and speed of the signal "}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Transmission delay : The time required to transfer all bits in the message to the link , Is a function of message length and link rate "}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Processing delay : Processing group header 、 Time required to check bit errors and determine grouping targets "}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Queue delay : Waiting time for incoming packets to be processed "}]}]}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"CDN Shorten the distance , To speed up access "}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":" The last mile of delay "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" A large part of the delay is often spent in the last few kilometers , Because the way that the client connects to the public network and the access link are poor "}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"TCP"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c7/c740e71f444886f60f16eef29b8b0815.png","alt":"web-sync","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":" Time delay "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Every http It takes three handshakes to connect , From New York to London , Start once TCP Connect , It takes at least three handshakes 56ms, Sending packets to London requires 28ms, The response should be 28ms. You can see that the delay caused by three handshakes is very large "}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":" Congestion control "}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":" congestion collapse "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" The round trip time may have exceeded the maximum interruption interval for all hosts , So the corresponding host will produce more and more copies , Paralyzed the whole network , The buffer of the last switch node is filled , The extra groups must be deleted "}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":" flow control "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" By zooming in and out of the receiving window (rwnd) To control the amount of traffic sent ( Matching )"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/69/6984d9033ac74d83745072590e6f60bc.png","alt":"web-rwnd","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":" Slow start "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Through a dynamically variable congestion window (cwnd) Size to control the sending of traffic , The maximum data that the network can send is rwnd and cwnd The minimum value of "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/7d/7d1082df30d4198922780cfd5e3a9bc3.png","alt":"web-slowstart","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" As shown in figure shows , A request needs to go through 220ms To get the maximum rate . Because slow start limits available throughput , It's bad for the transfer of small files "}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Slow start restart : After the connection has been idle for a while , Reset congestion window to a safe default value . without doubt ,SSR For long period idle and burst request TCP Connections have a big impact , Suggest server Jin Yong SSR"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/22/22ff9c84b51b1f91a890c1d33bf7295d.png","alt":"web-cwnd","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":" Congestion prevention "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Slow start, each round trip will double the amount of data transferred , Until the flow control window of the receiver is exceeded or there is packet loss . At this time, the congestion prevention algorithm is connected "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/38/3874ce91ac4a2b71285c316b970c4417.png","alt":"web-defint","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":" Bandwidth delay product "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"BDP Represents the product of the capacity of a data link and its end-to-end delay , The result is the maximum amount of unconfirmed data in transit at any time "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" The sender and receiver send more than the maximum amount of unacknowledged data , Will stop and wait for each other ACK, This creates a data gap . To solve this problem, you should set the window large enough , Too small a window will limit the throughput of the connection . The minimum size of the window should be set to BDP"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":" The head of the line is blocked "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TCP Sequential delivery versus reliable delivery , If you lose a packet sometimes , Then the subsequent packets must wait until the data of the lost packet is retransmitted and received , To deliver to the application , This leads to a feeling of delayed delivery when reading data "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" In the case of applications that are not related to sequential and reliable delivery TCP It's not the best choice . For example, audio , If you lose a bag, you can insert a small gap in the audio , You can continue to process the package later , As long as the gap is small enough , Users don't notice , Waiting for packet loss may lead to unexpected audio output . Relatively speaking , The latter has a worse user experience ."}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":" tuning "}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":" reason "}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TCP Three handshakes add a whole round trip time ;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TCP Slow start will be applied to every new connection ;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TCP Traffic and congestion control will affect the throughput of all connections ;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TCP The throughput is controlled by the current congestion window size ."}]}]}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":" programme "}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Upgrade the server kernel to the latest version (Linux:3.2+);"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Make sure cwnd The size is 10;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Disable idle slow start ;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Make sure the startup window zooms ;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Reduce the transmission of redundant data ;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Compress the data to be transmitted ;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Put the server close to the user to reduce round trip time ;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Reuse established as much as possible TCP Connect ."}]}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"UDP"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":" Network address translation "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c8/c8dcb0a1f7a0af59f3040b675ebee82f.png","alt":"web-nat","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f3/f3fd4102285e9d629bbb62b6a8545b35.png","alt":"web-nataddr","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" These three addresses are only allowed to be owned by private networks , Public networks are not allowed to have these ip"}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":" Connectome timeout "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" transit UDP The routing , because UDP There is no concept of connection and termination , This leads to the intermediate route not knowing when to delete the connection state . To solve this problem , Routers clean up regularly , Once the routing state is cleared ,UDP It needs to be re established . The solution is to send it back and forth on a regular basis keep-alive grouping . According to reason TCP There is a definite connection state , The router should be able to grasp TCP Life cycle of , But routers don't do that , The same goes for TCP Set the action of timeout cleaning , This leads to a long period of inactivity TCP, There will be no end of the connection will be broken ."}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"P2P"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"STUN: Session Traversal Utilities for NAT It's an agreement , Can let the intranet application obtain an extranet ip And port ,STUN The server is set up on the public network "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/32/32cc35f4eb835c19ef8ed061d8d58479.png","alt":"web-stun","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Each intranet application uses STUN after , You can get an extranet ip,STUN Server pass keepalive Mode to keep the route without timeout , Each application can directly UDP Communication "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TURN: Traversal Using Relays around NAT. When the intranet cannot be used NAT when , have access to TURN The server , Application through TCP Connect TURN The server , The server transfers messages "}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"libjingle It's a Google implementation STUN/TURN/ICE Open source library ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"92% Time can be connected directly to (STUN);"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"8% Time to use a repeater (TURN)."}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ICE: Interactive Connectivity Establishment agreement , If you can connect directly to , If you can't connect directly, use STUN, If not, use TURN"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":" Design principles "}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Applications must tolerate various Internet path conditions ;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" The application should control the transmission speed ;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" The application should have congestion control for all traffic ;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Applications should use and TCP Similar bandwidth ;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" The application should prepare a retransmission counter based on packet loss ;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Applications should not send greater than path MTU Datagram ;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" The application should deal with datagram loss 、 Repeat and rearrange ;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" The application should be stable enough to support 2 More than minutes of delivery delay ;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" The application should support IPv4 UDP The checksum , Must support IPv6 The checksum ;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Applications can be used when needed keep-alive( Minimum spacing 15 second )."}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":" It is recommended to use WebRTC"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":" bandwidth / The relationship between latency and page load time "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/37/37325461ebfc6f0605fac1ba15cfab2d.png","alt":"web-pageload","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":" The reason for the delay "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TCP handshake / Traffic congestion control / Packet loss / The head of the team is congested "}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":" User experience measurement of various resources on the website "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" adopt Navigation Timing/User Timing/resource timing To measure "}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":" Browser optimization "}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Resource prefetching and prioritization "}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"DNS Pre parse "}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TCP Pre connect "}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Page pre rendering "}]}]}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":" How the server takes advantage of these optimizations "}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"CSS and JavaScript Important resources should appear in the document as soon as possible ;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" It should be delivered as soon as possible CSS, To remove rendering blocking and let JavaScript perform ;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Non critical JavaScript It should be postponed , To avoid blocking DOM and CSSOM structure ;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"HTML The document is parsed incrementally by the parser , This ensures that documents can be sent intermittently , To get the best performance "}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/dd/dd0c1330272079ddc30733b7813c861b.png","alt":"web-preload","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"http Optimize "}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Reduce DNS Inquire about "}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Reduce HTTP request "}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Use CDN"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" add to Expires Head and configure ETag label "}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Gzip resources "}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" avoid HTTP Redirect "}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Using persistent connections "}]}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"keep alive And the limitations of connection pooling "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" For each server ip, The client maintains a long connection pool , If there are multiple requests going to the server , The number of pools and connections exceeds , This forces the client to wait for the connection pool to be idle , And enable multiple socket It takes up a lot of system resources ."}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"http The limitations of the agreement "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Send each time http request , You have to add the head , And the head is not compressed , This directly leads to the possibility that the length of the head may exceed body The length of ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢

随机推荐