作为PHP程序员我们或多或少都了解或使用过xdebug。此文章记录安装和配置xdebug,以及如何使用它来分析php程序。

我的机器环境: macphp

安装 xdebug

推荐使用 pecl 安装扩展

pecl install xdebug

注:可以选择版本, https://pecl.php.net/package/xdebug

启用和配置

如果不知道php的配置文件,可以用 php --ini 查看

启用 xdebug,添加一个独立的 ext-xdebug.ini 到 php 的配置目录

[xdebug]
zend_extension="xdebug.so"
# profiler功能的开关,默认值0,如果设为1,则每次请求都会生成一个性能报告文件。
# xdebug.profiler_enable=0
# 默认值是0,如果设为1 则当我们的请求中包含 XDEBUG_PROFILE 参数时才会生成性能报告文件
xdebug.profiler_enable_trigger=1
# 分析文件保存目录, 默认是 /var/tmp
xdebug.profiler_output_dir="/var/tmp/xdebug"

现在使用 php -m 能看到 xdebug,并且使用 php --ri xdebug 能看到配置项都生效就ok了。

访问并生成分析文件

使用 fpm 要重启,扩展才能生效

现在我们可以访问php项目,记得在请求参数带上 XDEBUG_PROFILE, 类似 http://127.0.0.1:5671/my?XDEBUG_PROFILE

ok, 现在xdebug应该已经在我们配置的分析文件保存目录产生了文件, 类似

% ll /var/tmp/xdebug
-rw-r--r-- 1 user group 48K 4 29 13:40 cachegrind.out.37947

生成的分析文件直接查看是难以阅读的。所以我们要使用分析查看工具

分析查看工具

使用qcachegrind

要查看分析图表,需要安装 graphviz:

brew install graphviz

安装qcachegrind

brew install qcachegrind

在命令行里使用 qcachegrind 打开它,然后通过它打开要分析的文件。

也可以在命令后直接跟要分析的文 qcachegrind /var/tmp/xdebug/cachegrind.out.37947

使用webgrind

下载: https://github.com/jokkedk/webgrind

要查看分析图表,同样需要安装 graphviz.

分析的时候 "Call Graph" 功能用不了

  • 方法 1
sudo ln -s /usr/local/bin/dot /usr/bin/dot
  • 方法 2 修改 config.php
    /**
* Path to graphviz dot executable
*/
static $dotExecutable = '/usr/local/bin/dot';
// static $dotExecutable = '/usr/bin/dot';

运行:

php -S 0.0.0.0:80 index.php

现在,你可以访问: http://localhost

使用xdebug对php做性能分析调优的更多相关文章

  1. mysql性能瓶颈分析、性能指标、指标搜集方法与性能分析调优工具

    本文主要讲解mysql的性能瓶颈分析.性能指标.性能指标信息的搜集工具与方法.分析调优工具的使用. 文章尚未完成. 性能瓶颈: 慢.写速度比读速度慢很多  主要的性能指标: 访问频度, 并发连接量, ...

  2. Linux性能分析调优工具介绍

    1.常用性能分析工具 1)CPU性能分析工具 vmstat ps sar time strace pstree top 2)Memory性能分析工具 vmstat strace top ipcs ip ...

  3. Linux 性能分析调优 (四)——案例篇:系统中出现大量不可中断进程和僵尸进程怎么办

    之前讲到 CPU 使用率的类型.除了上一节提到的用户 CPU 之外,它还包括系统 CPU(比如上下文切换).等待 I/O 的 CPU(比如等待磁盘的响应)以及中断 CPU(包括软中断和硬中断)等. 在 ...

  4. PHPStorm 初遇 Xdebug (xdebug代码调试及性能分析)

    centos 7 下PHP7安装xdebug # 下载xdebug wget https://xdebug.org/files/xdebug-2.7.2.tgz # 解压 tar -xf xdebug ...

  5. Python:用pyinstrument做性能分析

    导引 在计算密集型计算或一些Web应用中,我们常常需要对代码做性能分析.在Python中,最原始的方法即是使用time包中的time函数(该函数以秒为计时单位): from time import s ...

  6. chrome使用Timeline做性能分析

    使用Timeline做性能分析 Timeline面板记录和分析了web应用运行时的所有活动情况,这是研究和查找性能问题的最佳途径.###Timeline面板概览 Timeline面板主要有三个部分构成 ...

  7. 《linux性能及调优指南》 3.5 网络瓶颈

    3.5 Network bottlenecks A performance problem in the network subsystem can be the cause of many prob ...

  8. 《Linux 性能及调优指南》写在后面的话

    感谢飞哥的翻译. 目前飞哥 (http://hi.baidu.com/imlidapeng)的网址已经不能访问了. <Linux 性能及调优指南>这本书的原文地址:http://www.r ...

  9. Linux服务器性能查看分析调优

    一 linux服务器性能查看 1.1 cpu性能查看 1.查看物理cpu个数: cat /proc/cpuinfo |grep "physical id"|sort|uniq|wc ...

随机推荐

  1. Django admin简单介绍

    生成同步数据库的脚本: python manage.py makemigrations 同步数据库: python manage.py migrate 创建后台用户 python manage.py ...

  2. Centos7 安装使用virtualenvwrapper

    退出所有的虚拟环境,在物理环境下安装 1.下载安装virtualenvwrapper pip3 install virtualenvwrapper 2.查看python3的文件和virtualenvw ...

  3. Golang调用Python

    https://yq.aliyun.com/articles/117329 Python是时髦的机器学习御用开发语言,Golang是大红大紫的新时代后端开发语言.Python很适合让搞算法的写写模型, ...

  4. webdriver模拟鼠标操作

    ActionChains 生成模拟用户操作的对象 from selenium.webdriver.common.action_chains import ActionChains ActionChai ...

  5. Spark & Scala:

    https://blog.csdn.net/do_yourself_go_on/article/details/76033252 Spark源码之reduceByKey与GroupByKey     ...

  6. python中使用rsa加密

    前提不多说, 为什么使用RSA加密请自行搜索,直接正为: 一. 生成公钥及私钥, 并保存 二. 使用公钥加密, 私钥解密 后记: 通常使用中, 会先对数据进行bas64加密, 再对加密后的内容使用rs ...

  7. 【Comet OJ - Contest #0 A】解方程(数学水题)

    点此看题面 大致题意: 给定自然数\(n\),让你求出方程\(\sqrt{x-\sqrt n}+\sqrt y-\sqrt z=0\)的自然数解\(x,y,z\)的数量以及所有解\(xyz\)之和. ...

  8. 文本特征提取---词袋模型,TF-IDF模型,N-gram模型(Text Feature Extraction Bag of Words TF-IDF N-gram )

    假设有一段文本:"I have a cat, his name is Huzihu. Huzihu is really cute and friendly. We are good frie ...

  9. 关于几类STL容器swap的复杂度问题

    \(swap\)的方式有 \(S1.swap(S2)\) 或 \(swap(S1,S2)\) \(vector,map,set,deque \ \ \ \ swap\)复杂度:\(O(1)\) \(p ...

  10. [LeetCode] 660. Remove 9 移除9

    Start from integer 1, remove any integer that contains 9 such as 9, 19, 29... So now, you will have ...