2相对于http的性能优势

2相对于http的性能优势

本文介绍了与单个请求相比,http/2相对于http的性能优势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http/2比http好得多.如果必须进行多个http调用,这将非常有帮助.但是,一次通话有什么明显的好处吗?

http/2 is much better than http for websites. It's very helpful if you have to make multiple http calls. But is there any significant benefit for a single call?

推荐答案

有一般注意事项和特定注意事项.

There are general considerations and specific considerations.

一般考虑的是,HTTP/2是一种二进制协议,与HTTP/1.1相比,它更易于实现,并且遇到的情况更少.例如,需要解析HTTP/1.1标头而无需事先知道标头名称长度和标头值长度的事实.
另一个示例是HTTP/1.1解析器是否需要支持过时的行折叠在标题中.
HTTP/1.1标头解析中还有许多其他特殊情况,它们增加了HTTP/1.1解析器的额外复杂性,而HTTP/2则不存在这种复杂性,因为它是二进制协议且依赖于HPack.

The general considerations is that HTTP/2, being a binary protocol, is much easier to implement and has many less corner cases than HTTP/1.1.For example, the fact that HTTP/1.1 headers needs to parsed without knowing in advance header name length and header value length.
Another example is whether the HTTP/1.1 parser needs to support obsolete line folding in headers.
There are many many other corner cases in HTTP/1.1 header parsing that add additional complexity to the HTTP/1.1 parser that are just absent in HTTP/2 because it's a binary protocol and relies on HPack.

对于特定的考虑因素,有两种情况:一个请求也会触发连接的打开,一个请求是在已经打开的连接上进行的.

As for the specific considerations, there are two cases: a request that triggers also the opening of the connection, and a request that is made on an already opened connection.

在第一种情况下,HTTP/2相对于HTTP/1.1还要执行其他处理.
断开TCP连接打开和TLS握手(对于HTTP/1.1和HTTP/2来说是相同的),HTTP/2要求在第一个请求之前发送一个序言(服务器必须对此进行回复),并且这需要一个很少的额外处理.
客户序言和请求可以一起发送;否则,该请求会导致额外的往返延迟,从而等待前言答复.

In the first case HTTP/2 has additional processing to do with respect to HTTP/1.1.
TCP connection opening and TLS handshake being out of the picture (they are the same for HTTP/1.1 and HTTP/2), HTTP/2 requires sending a preface (to which the server must reply) before the first request, and this requires a tiny bit of additional processing.
The client preface and the request may be sent together; otherwise the request incurs in an additional roundtrip latency, waiting for the preface reply.

在对连接的第一个请求上,HTTP/2没有HPack状态,因此需要构建. HTTP/1.1每次都必须从头开始解析标头(一个字符一个字符).我没有数据,但是我认为HTTP/2在这里有一点优势.

Upon the first request on a connection, HTTP/2 has no HPack state so that needs to be built. HTTP/1.1 has to parse the headers every time from scratch (char by char). I don't have data, but I think HTTP/2 has a slight edge here.

第二种情况是,在一个已经打开的连接上发送请求,可能对HTTP/2有利,因为HPack现在已经缓存"了许多标头,因此它们的解析基本上简化为查找-而对于HTTP/1.1,标头解析必须针对所有请求一遍又一遍地完成.这也意味着后续的HTTP/2请求比其HTTP/1.1请求要小得多:将约400字节的HTTP/1.1请求压缩为约10字节的HTTP/2请求.
对于单个请求,减少并不重要,因为两个请求都适合单个MTU.但是,发送许多请求时,游戏可能会改变.

The second case, sending a request on an already opened connection, is probably favorable to HTTP/2 due to the fact that HPack has now "cached" a lot of headers, and so their parsing is basically reduced to just a lookup - whereas for HTTP/1.1 the header parsing must be done over and over for all requests.This also means that subsequent HTTP/2 requests are much smaller than their HTTP/1.1 counterpart: a ~400 bytes HTTP/1.1 request is compressed down to a ~10 bytes HTTP/2 request.
For a single request this reduction does not play an important role as both requests will fit a single MTU; however, the game may change when many requests are being sent.

数据下载(从服务器到客户端)对于HTTP/2来说开销很小,因为每个DATA帧都有9个八位位组的开销,而HTTP/1.1中可能不存在(对于已知内容长度的下载).在16 KiB的数据上,最小开销通常为9个字节(默认HTTP/2最大帧大小).HTTP/2中的数据下载也受流量控制,如果客户端的智能/速度不足以将WINDOW_UPDATE帧发送到服务器,则可能会阻止数据下载.

Data download (from server to client) has a slight overhead for HTTP/2 because each DATA frame has a 9 octets overhead that may be absent in HTTP/1.1 (for downloads of known content length). The minimum overhead is typically 9 bytes over 16 KiB of data (the default HTTP/2 max frame size).Data download in HTTP/2 is also subject to flow control, which may stall data download if the client is not smart/fast enough to send WINDOW_UPDATE frames to the server.

最后一个考虑因素是关于将数据上传到服务器的请求.配置服务器以扩大会话和流流控制窗口非常重要,因为默认值很小,并且会严重损害数据上传性能.

The last consideration is about requests that uploads data to the server.It is important that the server is configured to enlarge the session and stream flow control window, because the default values are really small and will hurt data upload performance a lot.

总而言之,我认为对于网络上的单个请求没有显着"的好处.
例如,在 Jetty 中(免责声明,我是提交者),因此在Java中,请求处理位于我的笔记本电脑上大约10s-100s微秒.
也许HTTP/1.1或HTTP/2的速度快或慢几微秒是有区别的,但是网络延迟会完全消除这点,因此您不会发现HTTP/1.1和HTTP/1.1发出的请求之间有任何区别.用HTTP/2制成的响应时间很短.

In summary, I don't think that for a single request over the network there are "significant" benefits.
For example in Jetty (disclaimer, I am a committer) and therefore in Java, request processing is in the order of 10s-100s microseconds on my laptop.
Maybe there is a difference in that HTTP/1.1 or HTTP/2 is few microseconds faster or slower, but this will be completely obliterated by the network latency, so that you won't see any difference between a request made with HTTP/1.1 and one made with HTTP/2 in terms of response time.

当您说"HTTP请求"时,可能有太多变量,必须对特定请求进行基准测试才能获得答案,并且如果您更改请求,更改请求数量,那么答案可能会有所不同. ,如果您更改请求中是否包含内容,或者更改响应内容的长度等.

There are so many variable that you can have when you say "HTTP request" that you have to benchmark a specific request to get an answer, and that answer may vary if you vary the request, if you vary the number of requests, if you vary whether the request has content or not, if you vary the response content length, etc.

这篇关于与单个请求相比,http/2相对于http的性能优势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 03:59