- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我在分析我的 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()
将脚本的性能降低两个数量级。我仍然不知道发生了什么;这绝对不应该发生。
但是,我至少可以建议如何修复它:
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/
我在让“@header”或任何其他@规则在ANTLR中工作时遇到麻烦。具有非常基本的语法,如下所示: grammar test; options { language = CSharp2;
我对来源和寄宿有疑问 我有一个ajax页面“Page A”,它将称为ajax提要“Page B” 我看到来自ajax调用的“页面B”的请求 header 具有源“http://mydomain.com
我在 pandas 中使用了数据透视表并获得了所需的数据框格式,但现在我有两行标题。数据透视表后的结果数据框如下: scenario Actual Plan
我在 pandas 中使用了数据透视表并获得了所需的数据框格式,但现在我有两行标题。数据透视表后的结果数据框如下: scenario Actual Plan
我想在主机将它们发送到网络之前修改数据包头(IP 头、TCP 头)。 例如,如果我使用 firefox 进行浏览,那么我想拦截所有来自 firefox 的数据包并修改 IP/TCP header ,然
我的 header 内容被包装到#header 中,但是当我设置边框显示结构时,它显示我的#header 的内容出现在#header 本身之后。可能是什么问题?这是我的代码: #header { bo
我是一名 Web 开发人员,使用过 PHP 和 .NET。有一年多的 Web 工作经验,我一直无法彻底了解浏览器缓存功能,希望这里的 Web Gurus 可以帮助我。我心中的问题是: 浏览器实际上是如
伙计们,我有一个问题,我不知道如何在一个 header 中连接多个 header ,我们称它为“主 header ”并使用该 header 中的函数,例如 // A.h #include class
我有一个包含 SOAP 消息的 XMLHTTPRequest。 我想添加用于标识消息并将由 C# Web 服务使用的 guid。 GUID 的目标是识别特定用户,并应护送所有用户请求以在服务器上进行身
我一直在阅读粘性标题,这是我目前所发现的。第一个粘性 header 效果很好,但是当它遇到第一个 header 时,我如何向上滚动第一个 header 并使第二个 header 卡住? http://
我想将当前基于 TableView 的数据网格转换为新的 UICollectionView 类。 这就是我当前的网格的样子: 我的网格有两个标题: 年份(2006a、2007a 等)和 类型(“收入”
我目前正在使用 Apollo 服务器。我正在尝试在响应 header 中设置一个属性。并且此属性是从客户端 graphQL 请求 header 中检索的。 我在网上查了一下。并看到了诸如使用插件或扩展
我的 Controller 的方法需要设置一个标题,例如X-Authorization .创建新对象( store Action )后,我执行转发以显示新创建的对象( show Action ): $
我正在研究一些关于 VLAN 的事情,发现了 VLAN 标签 和 header 。 如果我们有标准 802.3 以太网帧 的 MTU(1518 字节), header 802.3 中包含什么? 另外,
我是放心和 Java 的新手,我正在尝试做一个非常基本的测试来检查 API 的响应是否为 200 ok。 谁能告诉我我需要在下面的脚本中更改什么才能传递多个 header Id、Key 和 ConId
在我的项目中,我需要知道 zlib header 是什么样的。我听说它相当简单,但我找不到 zlib header 的任何描述。 例如,它是否包含魔数(Magic Number)? 最佳答案 zlib
我正在使用 JMeter 测试 HTTP 服务器,该服务器接受并验证 APIKey 并在成功时返回一个有时限的 token 。如果我有 token ,我想发送一个 token ;如果没有,我想发送一个
以太网 header 是什么样的? 是吗: 1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|
我们的应用程序支持 CORS 配置 header 。我在两个不同的主机上分别配置了 testApp。两种设置都相互独立工作。host1 上的应用程序配置有 CORS header Access-Con
tlhelp32.h 不包含 windows.h 本身是有原因的吗?我一直在与大量的编译器错误作斗争,因为我在包含 tlhelp32.h 之后包含了 windows.h。这是设计决定还是出于什么原因?
我是一名优秀的程序员,十分优秀!