gpt4 book ai didi

php - 为什么 php5-fpm 发布请求很慢,而相同的 php-cli 代码/控制台 curl 工作得非常快?

转载 作者:IT王子 更新时间:2023-10-29 00:04:08 25 4
gpt4 key购买 nike

我正在使用虚拟方法向本地 api Web 服务(通过 LAN)执行 POST 请求,该方法本身运行速度非常快(不到一秒)。

问题是如果我使用 php5-fpm 执行 POST 请求(curl poststreams)需要很长时间。

如果我使用具有相同代码或控制台 curl 命令的 php-cli 脚本 - 它运行得非常快,就像一个魅力。

奇怪的事情: 如果我使用 system 从 php5-fpm 执行控制台 curl 命令,则执行请求需要很长时间。所以,没有办法作弊:(

我使用直接 ipv4 地址来消除 DNS 问题(我尝试定义 CURLOPT_IPRESOLVE 选项,但性能相同)。

如果我省略 curl CURLOPT_POSTFIELDS 选项,php5-fpm 中的请求也非常快。

我正在使用带有官方 php 5.6.9 包的 debian jessie。

那么,为什么 php5-fpm 会出现这个问题?

我的 curl php 代码:

$data = json_encode([
'id' => 1,
'method' => 'test',
'sid' => session_id(),
]);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://192.168.182.22');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'Content-type: application/json',
'Content-length: ' . strlen($data)
]);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$out = curl_exec($curl);
curl_close($curl);

curl获取信息输出(说明starttransfer_time很慢):

array(26) {
["url"]=>
string(22) "http://192.168.182.22/"
["content_type"]=>
string(24) "text/html; charset=UTF-8"
["http_code"]=>
int(200)
["header_size"]=>
int(532)
["request_size"]=>
int(188)
["filetime"]=>
int(-1)
["ssl_verify_result"]=>
int(0)
["redirect_count"]=>
int(0)
["total_time"]=>
float(10.285093)
["namelookup_time"]=>
float(3.2E-5)
["connect_time"]=>
float(0.000156)
["pretransfer_time"]=>
float(0.000181)
["size_upload"]=>
float(82)
["size_download"]=>
float(99)
["speed_download"]=>
float(9)
["speed_upload"]=>
float(7)
["download_content_length"]=>
float(-1)
["upload_content_length"]=>
float(82)
["starttransfer_time"]=>
float(10.285052)
["redirect_time"]=>
float(0)
["redirect_url"]=>
string(0) ""
["primary_ip"]=>
string(14) "192.168.182.22"
["certinfo"]=>
array(0) {
}
["primary_port"]=>
int(80)
["local_ip"]=>
string(14) "192.168.182.20"
["local_port"]=>
int(49286)
}

再次尝试使用流。它的工作速度也很慢(比 curl 快一点,但无论如何我可以得到 40-50 秒的延迟):

$url = 'http://192.168.182.22/';
$data = json_encode([
'id' => 1,
'method' => 'test',
'sid' => session_id(),
]);

$headers = [
'Content-type: application/json',
'Connection: close',
'Content-Length: ' . strlen($data)
];

$options = array(
'http' => array(
'header' => join("\r\n", $headers) . "\r\n",
'method' => 'POST',
'content' => $data,
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);

diff/etc/php5/fpm/php.ini/etc/php5/cli/php.ini没有明显区别:

303c303
< disable_functions = pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,
---
> disable_functions =
363c363
< expose_php = Off
---
> expose_php = On
393c393
< memory_limit = 256M
---
> memory_limit = -1
660c660
< post_max_size = 200M
---
> post_max_size = 8M
810c810
< upload_max_filesize = 200M
---
> upload_max_filesize = 2M

tcpdump 中的主要内容我可以看到不同 - php5-fpm 请求包括 ARP 请求。我在控制台 curl/php-cli 中看不到这样的内容。当远程 api 服务器想要返回响应时,可能会出现问题,但找不到如何执行此操作的方法?

(@Mircea 建议添加静态 arp 路由,我们添加了它们,ARP 请求从 tcpdump 中消失了,但仍然存在暂停)。

php5-fpm 的 tcpdump 输出:

    00:55:22.347822 IP 192.168.182.20.52659 > 192.168.182.22.80: Flags [S], seq 4210728690, win 29200, options [mss 1460,sackOK,TS val 301284147 ecr 0,nop,wscale 7], length 0
E..<..@.@..~...........P..........r............
..;3........
00:55:22.347898 IP 192.168.182.22.80 > 192.168.182.20.52659: Flags [S.], seq 2702388053, ack 4210728691, win 28960, options [mss 1460,sackOK,TS val 280740546 ecr 301284147,nop,wscale 7], length 0
E..<..@.@.M@.........P....+U......q .R.........
......;3....
00:55:22.347906 IP 192.168.182.20.52659 > 192.168.182.22.80: Flags [.], ack 1, win 229, options [nop,nop,TS val 301284147 ecr 280740546], length 0
E..4..@.@..............P......+V...........
..;3....
00:55:22.347942 IP 192.168.182.20.52659 > 192.168.182.22.80: Flags [P.], seq 1:206, ack 1, win 229, options [nop,nop,TS val 301284147 ecr 280740546], length 205
E.....@.@..............P......+V.....o.....
..;3....POST / HTTP/1.1
Host: 192.168.182.22
Content-Type: application/x-www-form-urlencoded
Content-Length: 82
Accept: */*

{"id":1,"method":"station.sync","params":[1],"sid":"1gn8f4mhctui9ki91bla6osnf4"}

00:55:22.347997 IP 192.168.182.22.80 > 192.168.182.20.52659: Flags [.], ack 206, win 235, options [nop,nop,TS val 280740547 ecr 301284147], length 0
E..4..@.@.c..........P....+V........2......
......;3
00:55:27.356575 ARP, Request who-has 192.168.182.22 tell 192.168.182.20, length 28
..........).4...............
00:55:27.356777 ARP, Reply 192.168.182.22 is-at 00:0c:29:46:99:bc, length 46
..........)F........).4.......................
00:55:36.742347 IP 192.168.182.22.80 > 192.168.182.20.52659: Flags [P.], seq 1:558, ack 206, win 235, options [nop,nop,TS val 280744145 ecr 301284147], length 557
E..a..@.@.a..........P....+V........a......
......;3HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Wed, 01 Jul 2015 21:55:36 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Content-Type, X-Requested-With
Access-Control-Allow-Methods: GET, POST, OPTIONS
p3p: CP=IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT

16
{"id":1,"result":true}
0


00:55:36.742374 IP 192.168.182.20.52659 > 192.168.182.22.80: Flags [.], ack 558, win 237, options [nop,nop,TS val 301287746 ecr 280744145], length 0
E..4..@.@..............P......-............
..IB....
00:55:36.742486 IP 192.168.182.20.52659 > 192.168.182.22.80: Flags [F.], seq 206, ack 558, win 237, options [nop,nop,TS val 301287746 ecr 280744145], length 0
E..4..@.@..............P......-............
..IB....
00:55:36.742559 IP 192.168.182.22.80 > 192.168.182.20.52659: Flags [F.], seq 558, ack 207, win 235, options [nop,nop,TS val 280744145 ecr 301287746], length 0
E..4..@.@.c..........P....-..........:.....
......IB
00:55:36.742566 IP 192.168.182.20.52659 > 192.168.182.22.80: Flags [.], ack 559, win 237, options [nop,nop,TS val 301287746 ecr 280744145], length 0
E..4..@.@..............P......-............
..IB....
00:55:41.744102 ARP, Request who-has 192.168.182.20 tell 192.168.182.22, length 46
..........)F..................................
00:55:41.744121 ARP, Reply 192.168.182.20 is-at 00:0c:29:04:34:c9, length 28
..........).4.......)F......

快速(控制台/php-cli)请求的 tcpdump 输出:

0:43:46.854062 IP 192.168.182.20.52527 > 192.168.182.22.80: Flags [S], seq 560731240, win 29200, options [mss 1460,sackOK,TS val 301110274 ecr 0,nop,wscale 7], length 0
E..<.j@.@............/.P!l.h......r............
............
00:43:46.854198 IP 192.168.182.22.80 > 192.168.182.20.52527: Flags [S.], seq 1849811122, ack 560731241, win 28960, options [mss 1460,sackOK,TS val 280566673 ecr 301110274,nop,wscale 7], length 0
E..<..@.@.M@.........P./nA..!l.i..q ...........
............
00:43:46.854216 IP 192.168.182.20.52527 > 192.168.182.22.80: Flags [.], ack 1, win 229, options [nop,nop,TS val 301110274 ecr 280566673], length 0
E..4.k@.@............/.P!l.inA.............
........
00:43:46.854265 IP 192.168.182.20.52527 > 192.168.182.22.80: Flags [P.], seq 1:233, ack 1, win 229, options [nop,nop,TS val 301110274 ecr 280566673], length 232
E....l@.@............/.P!l.inA.............
........POST / HTTP/1.1
User-Agent: curl/7.38.0
Host: 192.168.182.22
Accept: */*
Content-Length: 84
Content-Type: application/x-www-form-urlencoded

{"id":1, "sid": "1gn8f4mhctui9ki91bla6osnf4","method":"station.sync", "params":[10]}
00:43:46.854317 IP 192.168.182.22.80 > 192.168.182.20.52527: Flags [.], ack 233, win 235, options [nop,nop,TS val 280566673 ecr 301110274], length 0
E..4."@.@.R%.........P./nA..!l.Q....b......
........
00:43:46.891248 IP 192.168.182.22.80 > 192.168.182.20.52527: Flags [P.], seq 1:615, ack 233, win 235, options [nop,nop,TS val 280566682 ecr 301110274], length 614
E....#@.@.O..........P./nA..!l.Q...........
........HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Wed, 01 Jul 2015 21:43:46 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Content-Type, X-Requested-With
Access-Control-Allow-Methods: GET, POST, OPTIONS
p3p: CP=IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT

4f
{"id":1,"error":{"code":"SERVER_ERROR","message":"Station #10 is not running"}}
0


00:43:46.891273 IP 192.168.182.20.52527 > 192.168.182.22.80: Flags [.], ack 615, win 238, options [nop,nop,TS val 301110283 ecr 280566682], length 0
E..4.m@.@............/.P!l.QnA.............
........
00:43:46.891932 IP 192.168.182.20.52527 > 192.168.182.22.80: Flags [F.], seq 233, ack 615, win 238, options [nop,nop,TS val 301110283 ecr 280566682], length 0
E..4.n@.@............/.P!l.QnA.............
........
00:43:46.896944 IP 192.168.182.22.80 > 192.168.182.20.52527: Flags [F.], seq 615, ack 234, win 235, options [nop,nop,TS val 280566684 ecr 301110283], length 0
E..4.$@.@.R#.........P./nA..!l.R....`i.....
........
00:43:46.896954 IP 192.168.182.20.52527 > 192.168.182.22.80: Flags [.], ack 616, win 238, options [nop,nop,TS val 301110285 ecr 280566684], length 0
E..4.o@.@............/.P!l.RnA.............
........

最佳答案

我在自己的 FPM 实例(使用 Nginx)上注意到的一件事是我不会获得临时输出。我从来没有费心去调查过这个,因为它对我自己的使用需求从来都不重要(小的工作单元,输出很少)。

在您的情况下,也许响应正在快速发送但连接保持事件状态 - 导致收件人等待更多永远不会到达的数据。您可以通过调查服务器端日志记录从服务器的角度检查响应时间来检查这一点。

关于php - 为什么 php5-fpm 发布请求很慢,而相同的 php-cli 代码/控制台 curl 工作得非常快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31168584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com