php输出缓冲区如何工作(使用$_SERVER['REQUEST_TIME']计算脚本执行时间的问题)
我真的不知道如何表达标题,所以我尽力了。
我们有一个计算最终脚本(基于网络)执行时间的问题。
我们做了一个简单的操作
if(time()-$_SERVER['REQUEST_TIME']>X){
logMe();
}
,发现有时脚本的执行时间会超过 X 秒。
我们 100% 确信这不是 mysql、memcached、sphinx 或任何其他常见罪魁祸首的问题。请...假设不是“我们这边的东西”阻碍了它。
我们甚至在任何繁重的处理完成之前在脚本中添加了这个简单的执行时间检查,并且仍然有一些命中。主要是远方的海外客户。
所以我认为这一定与输出缓冲区有关。
所以问题如下:
在php + apache中,输出缓冲区是如何工作的?假设您有以下情况:
[ 10KB HTML Body Head ]
[ Mysql query #1 ]
[ 50KB HTML Body ]
[ Mysql query #2 ]
[ 20KB HTML Body Footer ]
在此示例中,假设您有一个最大速度为 2KB/s 的客户端连接。
所以在最理想的情况下,他需要5秒才能收到“HTML Body Head”。
这是否意味着执行“Mysql query #1”需要5秒?
我想你明白了。客户端连接速度慢是否会影响处理脚本所需的时间。
进一步:任何 php.ini 设置是否会影响这一点,flush()/ob_flush() 会影响吗?
谢谢。
i really didnt know how to word the title so i did the best i could.
we have an issue where we calculate final script (web based) execution time.
we do a simple
if(time()-$_SERVER['REQUEST_TIME']>X){
logMe();
}
we find that sometimes the script is shown to take longer then X seconds to execute.
we are 100% sure this is not an issue with mysql, memcached, sphinx or any of the other usual culprits. please... just assume that its not 'something on our end' holding it up.
we even added this simple exec time check way way up in the script, before any heavy processing is done and there are still some hits. mainly far far away overseas clients.
so i am thinking this must be related to the output buffer somehow.
so the question is as follows:
in php + apache, how does the output buffer work? lets say you have the following:
[ 10KB HTML Body Head ]
[ Mysql query #1 ]
[ 50KB HTML Body ]
[ Mysql query #2 ]
[ 20KB HTML Body Footer ]
in this example, imagine you have a client connection that maxes out at 2KB/s.
so in the most ideal situation, it would take him 5 seconds to recieve the "HTML Body Head".
does that mean it will take 5 seconds before "Mysql query #1" is executed?
i think you get the idea. does a slow client connection affect how long it takes for a script to be processed.
further more: do any php.ini settings affect this, and does flush()/ob_flush()?
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PHP 将以最大可能的速度执行,如果必须缓冲输出,则脚本将不会暂停,直到缓冲区清空。它会继续填充缓冲区,直到脚本结束。
你的“X”值有多大? $_SERVER['REQUEST_TIME'] 来自 Apache 本身,是命中到来的时间。Apache 中可能会暂停,等待子进程可用于服务请求。在等待资源打开时,PHP 中可能会出现暂停(例如,不再有可用的数据库句柄,因此请等待一个资源释放)。然后数据库本身执行和从磁盘检索数据时可能会出现延迟,等等......
PHP will execute at maximum possible speed, and if output has to be buffered, the script will NOT pause until the buffer empties. It'll just keep filling up the buffer until the script ends.
How big is your 'X' value? $_SERVER['REQUEST_TIME'] comes from Apache itself, and is when the hit came in. There may be a pause in Apache to wait for a child process to become available to service the request. There can be pauses in PHP while waiting for a resource to open up (e.g. no more free database handles, so wait for one to free up). Then there can be delays for the database itself to execute and retrieve data off disk, etc...
PHP 是服务器端,因此脚本执行时间不依赖于客户端连接。
PHP 通常在脚本完成后输出。
Flush 可用于长时间运行的脚本在该脚本尚未完成时进行输出。
客户端仍然必须下载该页面。
当您的客户端速度为 2KB/s 时,除了升级其连接之外,不要认为您可以做很多事情 :P
PHP is serverside so the script execution time doesn't depend on the clients connection.
PHP usually ouputs once the script(s) has finished.
Flush can be used for long running script to output while this script hasn't finished yet.
Still the client have to download the page.
Don´t think there is a lot you can do when you have clients with 2KB/s other than upgrading their connection :P