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

时间:2015-12-19 03:56:05

标签: php apache performance

我正在分析我的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()函数这么慢,我的配置中有什么可以更改来解决它吗?

1 个答案:

答案 0 :(得分:2)

更新:2017年,我终于开始提交a bug report

我可以在我自己的机器上重现这种行为(在Apache 2.4.12上,使用PHP 5.6或PHP 7.0 - 两者的行为相同)并且在看到{{1}的单个调用时分享您的“震惊”将脚本的性能降低两个数量级。我还是不知道发生了什么事;肯定不应该正在发生。

但是,我至少可以建议如何解决它:

  • 在PHP 5.4及更高版本上,使用http_response_code设置状态代码而不是header()
  • 如果您处于陷入PHP 5.3的可怕位置,请尝试在传递给header的参数中使用小写http而不是大写{{} 1}}。 e.g。

    header

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

如果你想知道,是的,这种行为是令人烦恼的,甚至与我对PHP的低理智基线相比。我将探索更多内容,看看我是否能弄清楚发生了什么,但希望这至少会让你和其他遇到此问题的人解决他们的即时性能问题。