PHP:200个文件是否包含太多的网站?

时间:2014-02-05 16:49:14

标签: php parsing include server-side-includes

我这里有一个定制的CMS(基于MVC,由其他人制作),它包含大量的200个文件(总源大小为896kb,峰值使用量为7MB)。

内存使用情况还可以,但是如果你问我,它的文件数量非常大。

例如,我的框架最多只包含20个文件(类)来生成页面。

我的问题是: 当大量用户访问该网站时,这会影响性能吗?压力很大吗?

我已经使用这个小的关闭脚本(放在index.php之上)测试了它:

// Measure script, very useful
function __getBytesShortNotation( $iBytes, $iDecimals = 0 )
{
    $sResult   = @max( 0, $iBytes );
    $iKiloByte = 1024;
    $iTeraByte = $iKiloByte*$iKiloByte*$iKiloByte*$iKiloByte;
    $iGigaByte = $iKiloByte*$iKiloByte*$iKiloByte;
    $iMegaByte = $iKiloByte*$iKiloByte;

    if( $sResult < $iKiloByte )
     { $sResult.='B'; }
    elseif( $sResult >= $iTeraByte )
     { $sResult = round( $sResult / $iTeraByte, $iDecimals ).'Tb'; }
    elseif( $sResult >= $iGigaByte )
     { $sResult = round( $sResult / $iGigaByte, $iDecimals ).'Gb'; }
    elseif( $sResult >= $iMegaByte )
     { $sResult = round( $sResult / $iMegaByte, $iDecimals ).'Mb'; }
    elseif( $sResult >= $iKiloByte )
     { $sResult = round( $sResult / $iKiloByte, $iDecimals ).'Kb'; }

   return $sResult;
}

function __includetest__()
{
  // get REAL usage
 $iMem = memory_get_usage( true );
 $iPeakMem = memory_get_peak_usage( true );
 $aIncludes = get_included_files();

 $iSize = 0;
 $s = null;
 $i = $iCount = count( $aIncludes );

 while( $i-- )
  { $file = $aIncludes[$i];
    $iSize+= filesize( $file ); 
    echo $file.' filesize '.filesize( $file )."\n";
  }

 include( 'sysutils.php' ); 
 echo __getBytesShortNotation( $iSize )." - $iSize Bytes \n"; 
 echo 'files: '. $iCount."\n";
 echo 'Real used memory: '.__getBytesShortNotation( $iMem ).' '.$iMem.' Bytes'."\n";
 echo 'Real peak memory: '.__getBytesShortNotation( $iPeakMem ).' '.$iPeakMem.' Bytes'."\n";

}; 

register_shutdown_function( '__includetest__' );   

编辑:做了一些研究 我稍微扩展了脚本(未在此处发布)并进行一些测试。我现在不在办公室,所以我稍后会发布CMS的结果。在这个测试中,像Wordpress和我自己的框架一样繁重的CMS。当我将Wordpress与此CMS进行比较时,Wordpress使用“仅”最多105个文件来包含。将它们与我自己的框架进行比较可能有点不公平,因为CMS是分离的,但在运行时MT中看到了明显的差异。我已经用缓存(APC)和没有缓存做了一些测试。结果如下:

* **自己的框架 - 分离的CMS(启用APC)

Files    : 30 files in 7 unique directories
TotalSize: 1Mb - 1107807 Bytes 

Functions: 1535 (internal)
Functions: 522 (user) + 1535 (internal) = 2057
Classes  : 167

Runtime  : 0.26 MT
Real used memory: 4Mb - 4456448 Bytes
Real peak memory: 5Mb - 4718592 Bytes

* **自己的框架 - 分离的CMS - 在办公室测试(启用APC)

Files    : 28 files in 7 unique directories
TotalSize: 1Mb - 1080506 Bytes 

Functions: 1708 (internal)
Functions: 520 (user) + 1708 (internal) = 2228
Classes  : 177

Runtime  : 0,48 MT
Real used memory: 7Mb - 6815744 Bytes
Real peak memory: 7Mb - 6815744 Bytes

* **自己的框架 - 分离的CMS(APC已禁用)

Files    : 30 files in 7 unique directories
TotalSize: 1Mb - 1107807 Bytes 

Functions: 1516 (internal)
Functions: 522 (user) + 1516 (internal) = 2038
Classes  : 166

Runtime  : 0.33 MT
Real used memory: 9Mb - 9699328 Bytes
Real peak memory: 9Mb - 9699328 Bytes

* ** Wordpress - 默认主题 - 未激活插件(启用APC)

Files    : 83 files in 6 unique directories
TotalSize: 3Mb - 2713670 Bytes 

Functions: 1535 (internal)
Functions: 2092 (user) + 1535 (internal) = 3627
Classes  : 200

Runtime  : 1.26 MT
Real used memory: 10Mb - 10485760 Bytes
Real peak memory: 10Mb - 10485760 Bytes

* ** Wordpress Admin - 未激活插件(启用APC)

Files    : 105 files in 8 unique directories
TotalSize: 3Mb - 3341902 Bytes 

Functions: 1535 (internal)
Functions: 2464 (user) + 1535 (internal) = 3999
Classes  : 207

Runtime  : 1.17 MT
Real used memory: 7Mb - 7077888 Bytes
Real peak memory: 7Mb - 7340032 Bytes

* ** Wordpress - 默认主题 - 未激活插件(禁用APC)

Files    : 83 files in 6 unique directories
TotalSize: 3Mb - 2713674 Bytes 

Functions: 1516 (internal)
Functions: 2092 (user) + 1516 (internal) = 3608
Classes  : 199

Runtime  : 1.22 MT
Real used memory: 12Mb - 12845056 Bytes
Real peak memory: 12Mb - 12845056 Bytes

* ** Wordpress Admin - 未激活插件(禁用APC)

Files    : 105 files in 8 unique directories
TotalSize: 3Mb - 3341902 Bytes 

Functions: 1516 (internal)
Functions: 2464 (user) + 1516 (internal) = 3980
Classes  : 206

Runtime  : 1.3 MT
Real used memory: 16Mb - 16777216 Bytes
Real peak memory: 16Mb - 17039360 Bytes

---继续 -

更新2 - 在办公室

* * MVC CMS - 首次运行*

Files    : 199 files in 82 unique directories
TotalSize: 1Mb - 1241778 Bytes 

Functions: 1708 (internal)
Functions: 1 (user) + 1708 (internal) = 1709
Classes  : 295

Runtime  : 12.55 MT
Real used memory: 6Mb - 6029312 Bytes
Real peak memory: 6Mb - 6029312 Bytes

* ** MVC CMS - 第二次测试

Files    : 199 files in 82 unique directories
TotalSize: 1Mb - 1241778 Bytes 

Functions: 1708 (internal)
Functions: 1 (user) + 1708 (internal) = 1709
Classes  : 295

Runtime  : 1.31 MT
Real used memory: 5Mb - 5242880 Bytes
Real peak memory: 5Mb - 5242880 Bytes

** **测试结束**

MVC CMS一些第一印象:

  • 很多(litte)文件199!
  • 很多目录82!
  • 慢第一个运行时
  • 首次运行时使用大量内存
  • 减少全局功能(不错)

那么什么可能是瓶颈,文件或目录或其他东西?

1 个答案:

答案 0 :(得分:1)

当然,每个include_oncerequire_once都有时间,内存使用和对服务器的i / o影响。像Symfony这样的框架通过将类组合到几个文件来降低数量。

您可以使用xhprof查看包含的丢失时间。要查看服务器影响,请尝试使用htop等工具运行apache ab和监视服务器。

[编辑]

快速调整 - 使用APC for PHP&lt; 5.4或Opcache适用于5.4及更高版本。