Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP5中readfile()函数的性能_Php_Apache_Magento_Nginx_Readfile - Fatal编程技术网

PHP5中readfile()函数的性能

PHP5中readfile()函数的性能,php,apache,magento,nginx,readfile,Php,Apache,Magento,Nginx,Readfile,我在浏览器中下载开始的速度(不是下载速度,而是单击下载链接和下载显示在浏览器中之间的时间)方面遇到了一些问题 我已经尽可能地简化了我的代码 $filepath = $this->_resourceFile; ob_start(); $start = microtime(true); readfile($filepath); $end = microtime(true) - $start; Mage::log('Time taken to serve file was '.$end);

我在浏览器中下载开始的速度(不是下载速度,而是单击下载链接和下载显示在浏览器中之间的时间)方面遇到了一些问题

我已经尽可能地简化了我的代码

$filepath = $this->_resourceFile;
ob_start(); 
$start = microtime(true);
readfile($filepath);
$end = microtime(true) - $start; 
Mage::log('Time taken to serve file was '.$end);
(您将看到这是Magento的一部分,我对Magento提供可下载产品文件的方式进行了大量编辑,以尝试加快速度。这在大多数情况下都有效,但我仍然遇到问题)

我放置的日志如下所示

2014-10-23T14:19:15+00:00 DEBUG (7): Time taken to serve file was 0.067724943161011
2014-10-23T14:24:48+00:00 DEBUG (7): Time taken to serve file was 0.069678068161011
2014-10-23T14:25:58+00:00 DEBUG (7): Time taken to serve file was 0.080054998397827
2014-10-23T14:26:28+00:00 DEBUG (7): Time taken to serve file was 0.067059993743896
2014-10-23T14:27:27+00:00 DEBUG (7): Time taken to serve file was 0.068262100219727
2014-10-23T14:30:08+00:00 DEBUG (7): Time taken to serve file was 0.066760063171387
2014-10-23T14:36:34+00:00 DEBUG (7): Time taken to serve file was 90.616213083267
2014-10-23T14:45:11+00:00 DEBUG (7): Time taken to serve file was 0.065201997756958
2014-10-23T14:45:22+00:00 DEBUG (7): Time taken to serve file was 0.073112010955811
2014-10-23T14:46:56+00:00 DEBUG (7): Time taken to serve file was 87.481207132339
2014-10-23T14:47:44+00:00 DEBUG (7): Time taken to serve file was 36.195640087128
2014-10-23T14:47:48+00:00 DEBUG (7): Time taken to serve file was 0.067437887191772
2014-10-23T14:48:01+00:00 DEBUG (7): Time taken to serve file was 0.068021059036255
2014-10-23T14:48:06+00:00 DEBUG (7): Time taken to serve file was 0.028129100799561
2014-10-23T14:48:13+00:00 DEBUG (7): Time taken to serve file was 0.067390918731689
2014-10-23T14:49:00+00:00 DEBUG (7): Time taken to serve file was 0.065808057785034
2014-10-23T14:49:06+00:00 DEBUG (7): Time taken to serve file was 0.06847882270813
2014-10-23T14:49:48+00:00 DEBUG (7): Time taken to serve file was 0.063234090805054
2014-10-23T14:50:03+00:00 DEBUG (7): Time taken to serve file was 0.059723138809204
2014-10-23T14:50:08+00:00 DEBUG (7): Time taken to serve file was 0.068203926086426
2014-10-23T14:50:13+00:00 DEBUG (7): Time taken to serve file was 0.065500974655151
2014-10-23T14:50:16+00:00 DEBUG (7): Time taken to serve file was 0.064054012298584
2014-10-23T14:50:20+00:00 DEBUG (7): Time taken to serve file was 0.068597078323364
2014-10-23T14:50:24+00:00 DEBUG (7): Time taken to serve file was 0.026658058166504
2014-10-23T14:51:47+00:00 DEBUG (7): Time taken to serve file was 0.065510988235474
2014-10-23T14:51:56+00:00 DEBUG (7): Time taken to serve file was 0.06929087638855
2014-10-23T14:52:00+00:00 DEBUG (7): Time taken to serve file was 0.026684999465942
2014-10-23T14:52:32+00:00 DEBUG (7): Time taken to serve file was 0.067147016525269
2014-10-23T14:55:55+00:00 DEBUG (7): Time taken to serve file was 0.067998886108398
你会看到在0.06秒的等待中,有90秒的等待,87秒的等待,还有36秒的等待。是什么导致了这些尖峰


该站点运行在Linux服务器上,使用apache和nginx,使用PHP5.3.3。这可能是服务器负载问题吗?我认为用户带宽几乎与延迟无关,对吗?

因此,经过大量调查,似乎是服务器问题

服务器在第一次访问文件时将其存储在RAM中,以加快将来的访问速度。正是将文件复制到RAM中所花费的时间导致了延迟——一旦文件进入RAM,就会立即提供服务


我们已经更改了服务器上的IO资源设置,初始下载时间现在减少到6秒,而不是90秒。

在需要较长时间的文件中有什么不同吗?键入、大小、e.t.c。如果您将PHP与Apache一起使用,请阅读
X-Sendfile
。如果您将PHP与Nginx一起使用,那么
X-Accel-Redirect
@Jim-不太可能,尽管它们不是同一个文件(大约3个文件的混合体),但上面所有的日志记录都是使用大小约为90mb的MP3创建的。我无法使用X-SendFile或X-Accel-Redirect,因为我正在为我的文件设置自定义标题,似乎在几次下载之后,该文件在一定时间内保持一致的速度,就像它被缓存一样。有人有什么建议吗?