gpt4 book ai didi

php - 为什么 header() 函数非常慢?

转载 作者:可可西里 更新时间:2023-10-31 23:17:54 24 4
gpt4 key购买 nike

我在分析我的 PHP 网站的性能时惊讶地发现瓶颈是 header功能。

我在 PHP 5.3 和 Apache 2.4 上运行。

我用 ab 对两个简单文件进行了基准测试并发现第一个 - 执行 phpinfo() - 比第二个 - 调用 header 快得多。

第一个文件(能够以每秒超过 1000 个请求的速度运行):

<?php phpinfo(); ?>

第二个文件(每秒只能处理 12 个请求!):

<?php header('HTTP/1.1 200 OK'); ?>

完成第一个测试的 ab 输出:

C:\work\apache24\bin>ab -n 1000 -c 200 http://q.localhost/test.php
This is ApacheBench, Version 2.3 <$Revision: 1663405 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking q.localhost (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software: Apache/2.4.16
Server Hostname: q.localhost
Server Port: 80

Document Path: /test.php
Document Length: 69600 bytes

Server Software: Apache/2.4.16
Server Hostname: q.localhost
Server Port: 80

Document Path: /test.php
Document Length: 69600 bytes

Concurrency Level: 200
Time taken for tests: 0.984 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 69768000 bytes
HTML transferred: 69600000 bytes
Requests per second: 1015.82 [#/sec] (mean)
Time per request: 196.885 [ms] (mean)
Time per request: 0.984 [ms] (mean, across all concurrent requests)
Transfer rate: 69210.84 [Kbytes/sec] received

完成第二个测试的 ab 输出:

C:\work\apache24\bin>ab -n 1000 -c 200 http://q.localhost/test.php
This is ApacheBench, Version 2.3 <$Revision: 1663405 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking q.localhost (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests

Server Software: Apache/2.4.16
Server Hostname: q.localhost
Server Port: 80
Document Path: /test.php
Document Length: 0 bytes
Concurrency Level: 200
Time taken for tests: 80.099 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 168000 bytes
HTML transferred: 0 bytes
Requests per second: 12.48 [#/sec] (mean)
Time per request: 16019.840 [ms] (mean)
Time per request: 80.099 [ms] (mean, across all concurrent requests)
Transfer rate: 2.05 [Kbytes/sec] received

header 函数的简单调用可将性能降低到每秒 12 个请求。这让我震惊。

为什么 header() 函数这么慢,我可以在我的配置中更改什么来修复它吗?

最佳答案

更新:在 2017 年,我终于抽空提交了 a bug report .


我可以在我自己的机器上重现这个行为(在 Apache 2.4.12 上,使用 PHP 5.6 或 PHP 7.0 - 两者的行为是相同的)并分享你看到一次调用 时的“震惊” header() 将脚本的性能降低两个数量级。我仍然不知道发生了什么;这绝对不应该发生。

但是,我至少可以建议如何修复它:

  • 在 PHP 5.4 及更高版本上,使用 http_response_code设置状态代码而不是 header
  • 如果您陷入 PHP 5.3 的困境,请尝试在传递给 header 的参数中使用小写的 http 而不是大写的 HTTP。例如

    <?php header('http/1.1 200 OK'); ?>

    在我的机器上,这仍然会设置状态代码(如 the docs 注意,header 中用于设置状态代码的特殊情况逻辑是不区分大小写的)但会使性能问题消失。

如果您想知道,是的,即使与我期望从 PHP 获得的低健全基线相比,这种行为也非常疯狂。我将进行更多探索,看看我是否能弄清楚发生了什么,但希望这至少能让您和遇到此问题的任何其他人解决他们眼前的性能问题。

关于php - 为什么 header() 函数非常慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34367115/

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