加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 16:58:57 所属栏目:PHP教程 来源:网络整理
导读:我正在分析我的 PHP网站的性能,并惊讶地发现瓶颈是 header 功能. 我正在运行PHP 5.3和Apache 2.4. 我用 ab 对两个简单文件进行了基准测试,发现第一个执行的phpinfo() – 比第二个调用头快得多. 第一个文件(能够以每秒1000多个请求运行): ?php phpinfo(); ?
我正在分析我的 PHP网站的性能,并惊讶地发现瓶颈是 header功能.

我正在运行PHP 5.3和Apache 2.4.

我用ab对两个简单文件进行了基准测试,发现第一个执行的phpinfo() – 比第二个调用头快得多.

第一个文件(能够以每秒1000多个请求运行):

<?php phpinfo(); ?>

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

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

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

C:workapache24bin>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:workapache24bin>ab -n 1000 -c 200 http://q.localhost/test.php
This is ApacheBench,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

对标头功能的简单调用可将性能降至每秒12个请求.这震惊了我.

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

解决方法

更新:2017年,我终于提出申请 a bug report.

我可以在我自己的机器上重现这种行为(在Apache 2.4.12上,使用PHP 5.6或PHP 7.0 – 两者的行为是相同的)并且在看到单个调用header()时分享你的“震惊”减少脚本的表现两个数量级.我还是不知道发生了什么事;绝对不应该发生.

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

>在PHP 5.4及更高版本上,使用http_response_code设置状态代码而不是标题.
>如果您处于被困在PHP 5.3上的可怕位置,请尝试在传递给标头的参数中使用小写的http而不是大写的HTTP.例如

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

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

如果你想知道,是的,这种行为是令人毛骨悚然的疯狂,甚至与我期望从PHP的低理智基线相比.我将再探讨一下,看看我是否能弄清楚发生了什么,但希望这至少会让你和其他遇到这个问题的人解决他们的即时性能问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读