HTTP职位的延迟来自哪里
Where is a delay in an HTTP POST coming from?
我正在使用cpp-netlib在异步模式下使用cpp-netlib在c 上开发Web服务。该服务需要响应消息主体中通过HTTP帖子发送的数据。我观察到收到大于1K的输入时的性能差,我想解决这个问题。
如果数据相对较小,小于1K,则服务器几乎立即接收数据。如果数据大于1K,则首先在将实际数据的第一大块显示给回调之前首先调用异步读取回调函数后大约有一个延迟。最初延迟后,随后的块几乎立即到达。
如何消除此延迟?它是使用卷曲来发布测试数据的工件吗?如何轻松测试卷曲的性能以发布数据?
您可以在GitHub上找到最小的源代码。这是我用来将数据发布到服务器的命令:
rcook$ curl -d @AsyncDaemon.h http://localhost:8787/foo
这是其输出的示例(注释):
rcook$ ./async_daemon
1431387368.321863: AsyncDaemon constructor
1431387368.322446: receive thread beginning
*** It's waiting for a connection here.
1431387371.536191: begin transaction 0 on thread 24050
1431387371.536237: transaction 0 constructor
1431387371.536273: received 1206 byte request for /foo from 127.0.0.1:49402
1431387371.536312: invoked asynchronous read
1431387371.536321: end transaction handler
1431387371.536335: begin asynchronous read callback on thread 24050
1431387371.536348: read 0 bytes
1431387371.536386: invoked asynchronous read
1431387371.536394: end asynchronous read callback
*** The asynchronous read callback is invoked quickly, but gets no data.
*** There is then a pause of just over one second before the asynchronous
*** read callback is invoked again.
1431387372.537203: begin asynchronous read callback on thread 24050
1431387372.537253: read 1024 bytes
1431387372.537307: invoked asynchronous read
1431387372.537317: end asynchronous read callback
*** There is no significant delay when reading the next chunk.
1431387372.537429: begin asynchronous read callback on thread 24050
1431387372.537469: read 182 bytes
1431387372.537478: finished reading the body
1431387372.537746: wrote response
1431387372.537763: transaction 0 destructor
1431387372.537772: end asynchronous read callback
*** The server is then killed with a keyboard interrupt.
^C1431387375.382186: terminating with signal 2
1431387375.382231: initiating shutdown
1431387375.382241: stopping server
1431387375.382363: server run finished
1431387375.382423: receive thread ending
1431387375.382522: AsyncDaemon destructor
您可以看到,在第一次调用异步读取回调之后(并收到数据的零字节,顺便说一句),它要求另一个输入。在这一点上,在该输入到达之前,有一个暂停仅一秒钟以上,从1431387371.536394到此示例中的143131387372.537203。那段时间是怎么回事?我如何消除延迟?
我已经在网上进行了一些研究,并进行了几项实验(与CPP-Netlib(无效),卷曲与libcurl(无效果)的同步与异步模式(无效)),但找不到答案。<<<<<<<<<<<<<<
更新:TCP转储
根据JXH的建议,我在样本交易期间运行了TCP转储:
00:28:01.304446 IP6 localhost.52265 > localhost.8787: Flags [S], seq 3956487146, win 43690, options [mss 65476,sackOK,TS val 395479802 ecr 0,nop,wscale 7], length 0
00:28:01.304461 IP6 localhost.8787 > localhost.52265: Flags [R.], seq 0, ack 3956487147, win 0, length 0
00:28:01.305014 IP localhost.49421 > localhost.8787: Flags [S], seq 1668603425, win 43690, options [mss 65495,sackOK,TS val 395479803 ecr 0,nop,wscale 7], length 0
00:28:01.305039 IP localhost.8787 > localhost.49421: Flags [S.], seq 4010788604, ack 1668603426, win 43690, options [mss 65495,sackOK,TS val 395479803 ecr 395479803,nop,wscale 7], length 0
00:28:01.305079 IP localhost.49421 > localhost.8787: Flags [.], ack 1, win 342, options [nop,nop,TS val 395479803 ecr 395479803], length 0
00:28:01.305185 IP localhost.49421 > localhost.8787: Flags [P.], seq 1:176, ack 1, win 342, options [nop,nop,TS val 395479803 ecr 395479803], length 175
00:28:01.305210 IP localhost.8787 > localhost.49421: Flags [.], ack 176, win 350, options [nop,nop,TS val 395479803 ecr 395479803], length 0
00:28:02.306555 IP localhost.49421 > localhost.8787: Flags [P.], seq 176:1382, ack 1, win 342, options [nop,nop,TS val 395480053 ecr 395479803], length 1206
00:28:02.306620 IP localhost.8787 > localhost.49421: Flags [.], ack 1382, win 1373, options [nop,nop,TS val 395480053 ecr 395480053], length 0
00:28:02.307223 IP localhost.8787 > localhost.49421: Flags [P.], seq 1:52, ack 1382, win 1373, options [nop,nop,TS val 395480053 ecr 395480053], length 51
00:28:02.307270 IP localhost.49421 > localhost.8787: Flags [.], ack 52, win 342, options [nop,nop,TS val 395480053 ecr 395480053], length 0
00:28:02.307494 IP localhost.8787 > localhost.49421: Flags [P.], seq 52:66, ack 1382, win 1373, options [nop,nop,TS val 395480053 ecr 395480053], length 14
00:28:02.307522 IP localhost.49421 > localhost.8787: Flags [.], ack 66, win 342, options [nop,nop,TS val 395480053 ecr 395480053], length 0
00:28:02.307765 IP localhost.8787 > localhost.49421: Flags [F.], seq 66, ack 1382, win 1373, options [nop,nop,TS val 395480053 ecr 395480053], length 0
00:28:02.307867 IP localhost.49421 > localhost.8787: Flags [F.], seq 1382, ack 67, win 342, options [nop,nop,TS val 395480053 ecr 395480053], length 0
00:28:02.307917 IP localhost.8787 > localhost.49421: Flags [.], ack 1383, win 1373, options [nop,nop,TS val 395480053 ecr 395480053], length 0
我对TCPDUMP的经验不是很有经验,但是看起来175个字节流到服务器(HTTP标头?),然后在延迟了一秒钟以上,1206字节流向服务器,然后是51字节块以最小的延迟,然后是服务器响应。
这告诉我,延迟是在客户端引入的,可能是在卷发中。有人知道为什么吗?
解决了问题,这要归功于@jxh建议的调试和诊断技术。
将 --trace - --trace-time
添加到curl命令中,揭示了curl正在花费那个神秘的第二个等待服务器返回100继续响应,然后再发送请求的其余部分:
01:31:44.043611 == Info: Connected to localhost (127.0.0.1) port 8787 (#0)
01:31:44.043726 => Send header, 175 bytes (0xaf)
0000: 50 4f 53 54 20 2f 66 6f 6f 20 48 54 54 50 2f 31 POST /foo HTTP/1
0010: 2e 31 0d 0a 55 73 65 72 2d 41 67 65 6e 74 3a 20 .1..User-Agent:
0020: 63 75 72 6c 2f 37 2e 33 35 2e 30 0d 0a 48 6f 73 curl/7.35.0..Hos
0030: 74 3a 20 6c 6f 63 61 6c 68 6f 73 74 3a 38 37 38 t: localhost:878
0040: 37 0d 0a 41 63 63 65 70 74 3a 20 2a 2f 2a 0d 0a 7..Accept: */*..
0050: 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 Content-Length:
0060: 31 32 30 36 0d 0a 43 6f 6e 74 65 6e 74 2d 54 79 1206..Content-Ty
0070: 70 65 3a 20 61 70 70 6c 69 63 61 74 69 6f 6e 2f pe: application/
0080: 78 2d 77 77 77 2d 66 6f 72 6d 2d 75 72 6c 65 6e x-www-form-urlen
0090: 63 6f 64 65 64 0d 0a 45 78 70 65 63 74 3a 20 31 coded..Expect: 1
00a0: 30 30 2d 63 6f 6e 74 69 6e 75 65 0d 0a 0d 0a 00-continue....
01:31:45.045626 == Info: Done waiting for 100-continue
01:31:45.045831 => Send data, 1206 bytes (0x4b6)
它是CPP-Netlib(至少在0.11.0版本)中的已知缺陷,它不支持发送卷曲期望的100个继续响应。
然后,解决方案变得令人信服,不要等待100继续响应。正如我在这里发现的那样,将-H 'Expect:'
添加到Curl命令行中可以做到这一点。因此,整个交易大约需要一毫秒。
由于我回答了自己的问题,所以我几个星期都不会接受答案,以给别人一个更好的贡献。
- 如何仅为一个函数添加延迟
- 如何在boost beast http请求中设置http头
- 在多个核心中处理一个HTTP请求
- SFML library: http request
- 以在Qt中的IF语句中设置时间延迟
- 模板化类中静态成员的延迟初始化
- 在 Windows 8/10 技术中完全实时的屏幕捕获,没有延迟
- 将自定义函数传递到基抽象类中以延迟执行
- 使用 Winsock2.h C++向不和谐 API 发送 HTTP 请求时出现问题
- 我希望改进或要求我目前的延迟/睡眠方法。C++
- 如何在 c++ 中延迟?
- 如何测量管道延迟?
- 通过单独的 tcp 流建立 http 连接
- 获取毫秒延迟的错误值
- 使用 winsock 接收 http 请求
- C++异常被捕获延迟,可能导致这种情况的原因是什么?
- 我的子类的属性是延迟初始化的
- 如何在 c++ 中通过 http 发送大型视频文件?
- 对于 http 请求,python 比 c++ 快吗?
- HTTP职位的延迟来自哪里