用xdebug 分析系统瓶颈
下载xdebug
安装
1.linux
下载源码包http://xdebug.org/download.php
tar -zvxf xdebug.tgz
进入解压的目录
/usr/local/php/bin/phpize(可能你的系统会没有安装phpize,那就安装吧)
./configure --enable-xdebug
make
make install
修改配置文件
zend_extension = /usr/lib/php5/20090626/xdebug.so xdebug.profiler_enable = on 开启性能的监控 xdebug.auto_trace = on 开启函数的跟踪会在trace_output_dir目录下生成日志文件 xdebug.trace_output_dir = /var/tmp/xdebug xdebug.trace_output_name = trace.%c xdebug.profiler_output_dir = /var/tmp/xdebug
php5.3之后不论是否是线程安全的都是用zend_extension,如果是之前的版本线程安全的版本用zend_extension_ts如果是非线程安全的zend_extensio
trace_output_dir文件内容
TRACE START [2012-10-28 18:49:37] 0.0001 629576 -> {main}() /var/www/myproject/load.php:0 0.0002 634840 -> require(/var/www/myproject/view.php) /var/www/myproject/load.php:2 0.0002 640064 -> require(/var/www/myproject/cache.php) /var/www/myproject/load.php:3 0.0002 640440 -> CView->__construct() /var/www/myproject/load.php:42 0.0002 640816 -> dirname() /var/www/myproject/view.php:16 0.0003 640856 -> queryData() /var/www/myproject/load.php:43 0.0004 641352 -> mysql_connect() /var/www/myproject/load.php:5 0.0035 641632 -> mysql_query() /var/www/myproject/load.php:6 0.0043 641624 -> mysql_select_db() /var/www/myproject/load.php:7 0.0044 641640 -> mysql_query() /var/www/myproject/load.php:11 0.0045 642296 -> mysql_fetch_array() /var/www/myproject/load.php:13 0.0045 644616 -> mysql_fetch_array() /var/www/myproject/load.php:13 0.0046 646936 -> mysql_fetch_array() /var/www/myproject/load.php:13 0.0047 649256 -> mysql_fetch_array() /var/www/myproject/load.php:13 0.0047 651576 -> mysql_fetch_array() /var/www/myproject/load.php:13 0.0047 653896 -> mysql_fetch_array() /var/www/myproject/load.php:13 0.0048 656216 -> mysql_fetch_array() /var/www/myproject/load.php:13 0.0048 658536 -> mysql_fetch_array() /var/www/myproject/load.php:13 0.0048 660856 -> mysql_fetch_array() /var/www/myproject/load.php:13 0.0048 663240 -> mysql_fetch_array() /var/www/myproject/load.php:13 0.0048 665560 -> mysql_fetch_array() /var/www/myproject/load.php:13 0.0049 667880 -> mysql_fetch_array() /var/www/myproject/load.php:13 0.0049 670200 -> mysql_fetch_array() /var/www/myproject/load.php:13 0.0049 670200 -> mysql_close() /var/www/myproject/load.php:16 0.0082 669296 -> CView->assign() /var/www/myproject/load.php:43 0.0082 669376 -> is_array() /var/www/myproject/view.php:21 0.0082 669376 -> is_array() /var/www/myproject/view.php:23 0.0082 669376 -> is_null() /var/www/myproject/view.php:23 0.0085 669736 -> CView->fetch() /var/www/myproject/load.php:44 0.0086 669816 -> extract() /var/www/myproject/view.php:36 0.0087 672120 -> ob_start() /var/www/myproject/view.php:37 0.0087 714648 -> include(/var/www/myproject/view/users.php) /var/www/myproject/view.php:38 0.0089 714872 -> ob_get_contents() /var/www/myproject/view.php:39 0.0089 720504 -> ob_end_clean() /var/www/myproject/view.php:40 0.0092 16744 TRACE END [2012-10-28 18:49:37]
第一列显示的是时间,黑体的记录是话费来比较多时间的代码,可以看出数据查询花来比较多。
profiler_output_dir文件的内容
fl=php:internal
78 fn=php::mysql_fetch_array
79 13 2
80
81 fl=php:internal
82 fn=php::mysql_fetch_array
83 13 1
84
85 fl=php:internal
86 fn=php::mysql_fetch_array
87 13 1
88
89 fl=php:internal
90 fn=php::mysql_fetch_array
91 13 1
92
93 fl=php:internal
94 fn=php::mysql_fetch_array
95 13 0
96
97 fl=php:internal
98 fn=php::mysql_close
99 16 1024
100
101 fl=/var/www/myproject/load.php
102 fn=queryData
103 4 263
104 cfl=php:internal
105 cfn=php::mysql_connect
106 calls=1 0 0
107 5 2317
108 cfl=php:internal
109 cfn=php::mysql_query
110 calls=1 0 0
111 6 120
112 cfl=php:internal
113 cfn=php::mysql_select_db
这个文件可以通过工具kcachegrind来分析
我们可以看到数据库操作话费的时间最多占据来87.81ms,其中又一链接数据库话费的最多41.62ms
还可以以百分比的方式查看。