用xdebug的函數跟蹤功能測試網站性能

相信做php開發的朋友很多都認識xdebug,它確實是php開發過程中檢查錯誤與性能分析的好工具。本章將介紹它的一個蠻不錯的功能:函數跟蹤。它可以根據程序在實際運行時的執行順序,跟蹤記錄所有函數的執行時間,以及函數調用時的上下文,包括實際參數和返回值。 

要開啓xdebug的函數跟蹤功能,需要在php.ini中做一些設置。設置如下:
extension=php_xdebug.dll
[xdebug]
xdebug.auto_trace=on
xdebug.trace_options=1
xdebug.trace_output_dir="E:\web\server\tmp\xdebug\trace"
xdebug.trace_output_name=trace.%c

配置好了時候,重啓Apache,然後執行頁面就會在E:\web\server\tmp\xdebug\trace目錄下生成報告文件。 

打開報告文件,格式類似如下:
0.0348     379272    -> dirname() E:\web\host\mysite.com\framework\Controller.php:27
0.0395     655712    -> require(E:\web\host\mysite.com\framework\smarty\Smarty.class.php) E:\web\host\mysite.com\framework\Controller.php:27    
0.0396     657784    -> defined() E:\web\host\mysite.com\framework\smarty\Smarty.class.php:37 
0.0396     657824    -> define() E:\web\host\mysite.com\framework\smarty\Smarty.class.php:38 
0.0397     657840    -> defined() E:\web\host\mysite.com\framework\smarty\Smarty.class.php:45 
0.0398     657864    -> dirname() E:\web\host\mysite.com\framework\smarty\Smarty.class.php:46
0.0399     657864    -> define() …………………………………………………………………………………………………………………………..………………………………………………………………………………………………………………………….. 
0.0206     300960    -> require_once(E:\web\host\mysite.com\app\protected\models\Test.php) 
0.0209     300672    -> is_resource() E:\web\host\mysite.com\framework\Model.php:63    
0.0209     300672    -> Model->_connect() E:\web\host\mysite.com\framework\Model.php:63  
0.0210     301776    -> PDO->__construct() E:\web\host\mysite.com\framework\Model.php:71
0.0253     299736    -> Test->selectData() E:\web\host\mysite.com\app\protected\controllers\DefaultController.php:25   
0.0254     299896    -> Model->select() E:\web\host\mysite.com\app\protected\models\Test.php:43
 

從報告中,可以看到require(E:\web\host\mysite.com\framework\smarty\Smarty.class.php)花了0.0047s,另外Test->selectData()函數花了0.0043秒,這兩個函數花的時間遠遠高於其他函數所花的時間,好了,有了這些報告,我們就知道可以從哪些地方優化網站性能了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章