PHP执行慢分析工具xdebug + webgrind

Linux33阅读模式

经常碰到用户PHP网站打开速度慢,运维环境配置的锅?PHP代码的锅?当你发现程序变慢,但有时我们不知道具体是哪块代码执行慢(排除环境问题),或者哪个方法占用了太长的执行时间,这时我们就需要一个调试工具,来帮我们记录程序执行过程中的一些具体信息,包括类、方法名、执行时间、次数等信息,今天介绍的工具是xdebug以及他的结果分析展示工具webgrind。

xdebug安装

  1. cd /root/oneinstack/src
  2. wget https://xdebug.org/files/xdebug-2.5.5.tgz
  3. tar xzf xdebug-2.5.5.tgz
  4. cd xdebug-2.5.5
  5. /usr/local/php/bin/phpize
  6. ./configure --with-php-config=/usr/local/php/bin/php-config
  7. make && make install

php加载xdebug模块

  1. cat > /usr/local/php/etc/php.d/ext-xdebug.ini << EOF
  2. [xdebug]
  3. zend_extension=xdebug.so
  4. xdebug.trace_output_dir=/tmp/xdebug
  5. xdebug.profiler_output_dir = /tmp/xdebug
  6. xdebug.profiler_enable = On
  7. xdebug.profiler_enable_trigger = 1
  8. EOF

创建xdebug目录

  1. mkdir /tmp/xdebug
  2. chown www.www /tmp/xdebug

webgrind安装

webgrind是php代码,依赖PHP环境, 将webgrind代码拷贝到你的apache或者nginx的web路径下,可以正常访问.

  1. cd /data/wwwroot/www.example.com
  2. git clone https://github.com/jokkedk/webgrind.git
  3. chown -R www.www /data/wwwroot/www.example.com
  4. vim /data/wwwroot/www.example.com/webgrind/config.php
  5. static $storageDir = '/tmp/webgrind';
  6. static $profilerDir = '/tmp/xdebug';

创建webgrind目录

  1. mkdir /tmp/webgrind
  2. chown www.www /tmp/webgrind

分析

然后运行你的php项目,这时xdebug会在/tmp/xdebug/下生成一个 cachegrind.out.* 的文件。在页面打开webgrind首页。

http://www.example.com/webgrind/index.php

PHP执行慢分析工具xdebug + webgrind
  • Invocation Count:函数调用次数
  • Total Self Cost:函数本身花费的时间
  • Total Inclusive Cost:包含内部函数花费的时间

Mon Dec 4 20:08:55 CST 2017

 
  • 本文由 yeho 发表于 2017-12-04
  • 转载请务必保留本文链接:https://linuxeye.com/457.html
  • webgrind
  • xdebug
Linux

Nginx反向代理永久性缓存

Nginx缓存简介 Nginx缓存方式有两种: 永久性的缓存:这种缓存若不手动删除,该缓存文件会一直生效,因此,永久缓存只是用于缓存网站中几乎不会更改的内容; 临时缓存:这种缓存是根据请求连接进行哈希...
Linux

Consul集群配置

consul原理 上图是官网提供的一个事例系统图,图中的Server是consul服务端高可用集群,Client是consul客户端。consul客户端不保存数据,客户端将接收到的请求转发给响应的Se...
    • 楚狂人
      楚狂人

      最服你们这些搞技术的,动不动就自己搞工具,厉害炸了

      • 请输入您的QQ号
        请输入您的QQ号

        能否问下大神,网站最底端的那个代码高亮工具是怎么弄得呢?

      匿名

      发表评论

      匿名网友
      确定

      取消

      拖动滑块以完成验证