《让php扩展开拓编程前路》 之 性能分析利器 tideways | php 技术论坛-大发黄金版app下载
- title: 《让php扩展开拓编程前路》 之 性能分析利器 - tideways
- tag: - pecl、- php、- profiler、- tideways、- 性能分析
- author: tacks 
- create-date: 2023-05-19 
- update-date: 2023-05-22 
ref
- docs
- blog
- github
- github
- github
- github
- github
1、intro
1.1 tideways 的发展
php 扩展
xhprof是一个轻量级的分层性能测量分析器,最开始由php的步伐,掉队 php7.x 系列,但tideways抓住了时机,紧随其后,对xhprof进行重写,推出 扩展。虽然tideways没有发布在pecl,也没有在 有相关说明,但有商业公司支持,并且积极支持 php7.x 系列,显然是受欢迎的。
tideways其中有特点的几个版本v4.1.7v5.x。 其中 ,最高支持 php7.3 ,如果 php7.4 上无法使用该版本的tideways,但是该版本有 sql 分析功能,还是很有亮点的一部分,并且此时的tideways扩展名为tideways.so;但由于后续商业化,在 时,更名为tideways_xhprof.so扩展,并且移除 sql 分析功能;
- tideways.so开源版- php-xhprof-extension的- v4.1.7版本,最高支持- php7.3版本,带有 sql 分析功能;
 
- tideways_xhprof.so开源版- php-xhprof-extension的- v5.x版本,支持- php7以上,当前也包括- php8,此版本移除 sql 分析功能,基本的分析还是有的,比如 callgraph, 而且此扩展还在更新;
 
- tideways.so商业版- 需要购买 tideways产品,它将以 saas 方式更丰富的性能分析功能(本文不讨论商业版),但是仍然致敬,有商业支持的,才有精力开源,虽然丐版,但tideways_xhprof.so也够用;
 
- 需要购买 
1.2 xhgui 汉化的事情
xhgui基于 bootstrap 的 xhprof web ui ,当然原英文版 ,看起来确实没那么舒服,但还好有汉化版。感谢@laynefyc,提供两款 ui ,分别是 基于 bootstrap 的 和 基于 vue 的 。
对于
profiler性能分析器来说, 不论是xhprof还是tideways、tideways_xhprof扩展,所采集的日志数据是可以通用的,你可以这么理解,虽然逻辑不太一样,但最终展示数据格式的一样的,所以像xhgui这样的 web ui 机会很灵活。你可以根据自己 php 的版本,来选择合适的profiler扩展,但记住只需要启用任意一个扩展即可,再选择合适的ui展示。
- 本文采用 php-monitor来实现 web ui 展示;
1.3 采集数据存储的事情
通常来说 xhgui 会提供几种方式,比如
mongodbsqlitemysql等,你可以根据自己情况来进行选择。本文采用mongodb,因此首先要确定mongodb的服务端,以及php的扩展;
- 通常来说 profiler采集数据包括- 函数调用次数
- 执行消耗的时间
- cpu消耗的时间
- 使用的内存
- sql的执行
- 函数调用图
- 火焰图
- 瀑布图
 
2、prepare
2.1 我的环境
- php 7.3.20没有是最新的是因为我太懒,晚点换
[root@centos7 ~]# php -v
php 7.3.20 (cli) (built: jan 11 2021 17:21:40) ( nts )
大发黄金版app下载 copyright (c) 1997-2018 the php group
zend engine v3.3.20, 大发黄金版app下载 copyright (c) 1998-2018 zend technologies
    with zend opcache v7.3.20, 大发黄金版app下载 copyright (c) 1999-2018, by zend technologies
[root@centos7 ~]# /php/php73/bin/pecl -v
pear version: 1.10.12
php version: 7.3.20
zend engine version: 3.3.20
running on: linux centos7 3.10.0-1160.11.1.el7.x86_64 #1 smp fri dec 18 16:34:56 utc 2020 x86_64
[root@centos7 ~]# cat /etc/redhat-release 
centos linux release 7.9.2009 (core)2.2 安装 tideways 扩展
- 源码安装 tideways- 由于我的 php 是 7.3 版本,因此可以安装 tideways v4.x 的版本,这里采用
 
[root@centos7 phpext]# wget https://github.com/tideways/php-xhprof-extension/archive/v4.1.7.tar.gz 
[root@centos7 phpext]# tar zxvf v4.1.7.tar.gz 
[root@centos7 phpext]# cd php-xhprof-extension-4.1.7
[root@centos7 php-xhprof-extension-4.1.7]# /php/php73/bin/phpize && ./configure --with-php-config=/php/php73/bin/php-config &&  make && make install
[root@centos7 ~]# ll /php/php7.3.20/lib/php/extensions/no-debug-non-zts-20180731/tideways.so 
-rwxr-xr-x 1 root root 445120 may 18 19:08 /php/php7.3.20/lib/php/extensions/no-debug-non-zts-20180731/tideways.so
# 追加配置
[root@centos7 php-xhprof-extension-4.1.7]# vim /php/php7.3.20/lib/php.ini
[tideways]
extension=tideways.so
;不需要自动加载,在程序中控制就行
tideways.auto_prepend_library=0
;频率设置为100,在程序调用时能改
tideways.sample_rate=100
# 重启 php
[root@centos7 php-xhprof-extension-4.1.7]# /etc/init.d/php-fpm73 restart
gracefully shutting down php-fpm . done
starting php-fpm  done
# 检查 `tideways` 是否被加载
[root@centos7 ~]# php -m | grep tideways 
tideways
[root@centos7 ~]# php --ri  tideways
tideways
tideways => 4.1.7
connection (tideways.connection) => unix:///var/run/tideways/tidewaysd.sock
udp connection (tideways.udp_connection) => 127.0.0.1:8135
default api key (tideways.api_key) =>  
default sample-rate (tideways.sample_rate) => 100
framework detection (tideways.framework) =>  
service name (tideways.service) =>  
automatically start (tideways.auto_start) => yes
tideways collect mode (tideways.collect) => tracing
tideways monitoring mode (tideways.monitor) => basic
cli monitoring enabled (tideways.monitor_cli) => 0
timeout for talk to daemon (tideways.timeout) => 10000
allowed distributed tracing hosts (tideways.distributed_tracing_hosts) => 127.0.0.1
load php library (tideways.auto_prepend_library) => no
tideways.php found => no2.3 安装 tideways_xhprof 扩展
- 源码安装 tideways_xhprof- 由于我的 php 是 7.3 版本,因此可以安装 tideways v5.x 的版本,这里采用
 
[root@centos7 phpext]# wget https://github.com/tideways/php-xhprof-extension/releases/download/v5.0.4/tideways-xhprof-5.0.4-x86_64.tar.gz
[root@centos7 phpext]# tar zxvf tideways-xhprof-5.0.4-x86_64.tar.gz
./
./tideways_xhprof-5.0.4/
./tideways_xhprof-5.0.4/tideways_xhprof-8.0.so
./tideways_xhprof-5.0.4/license
./tideways_xhprof-5.0.4/tideways_xhprof-7.3.so
./tideways_xhprof-5.0.4/tideways_xhprof-7.4-zts.so
./tideways_xhprof-5.0.4/tideways_xhprof-7.2.so
./tideways_xhprof-5.0.4/tideways_xhprof-7.0.so
./tideways_xhprof-5.0.4/tideways_xhprof-7.2-zts.so
./tideways_xhprof-5.0.4/tideways_xhprof-7.4.so
./tideways_xhprof-5.0.4/tideways_xhprof-7.1.so
./tideways_xhprof-5.0.4/tideways_xhprof-7.3-zts.so
./tideways_xhprof-5.0.4/notice
./tideways_xhprof-5.0.4/tideways_xhprof-8.0-zts.so
./tideways_xhprof-5.0.4/tideways_xhprof-7.1-zts.so
[root@centos7 phpext]# cd tideways_xhprof-5.0.4
[root@centos7 tideways_xhprof-5.0.4]# cp tideways_xhprof-7.3.so /php/php7.3.20/lib/php/extensions/no-debug-non-zts-20180731/tideways_xhprof.so 
# 追加配置
[root@centos7 php-xhprof-extension-4.1.7]# vim /php/php7.3.20/lib/php.ini
[tideways_xhprof]
extension=tideways_xhprof.so
# 重启 php
[root@centos7 php-xhprof-extension-4.1.7]# /etc/init.d/php-fpm73 restart
gracefully shutting down php-fpm . done
starting php-fpm  done
# 检查 `tideways_xhprof` 是否被加载
[root@centos7 ~]# php -m | grep tideways_xhprof 
tideways_xhprof
[root@centos7 tideways_xhprof-5.0.4]# php --ri  tideways_xhprof
tideways_xhprof
version => 5.0.4
clock source => clock_gettime
tideways is a php profiler, monitoring and exception tracking software.
the 'tideways_xhprof' extension provides a subset of the functionality of our commercial tideways offering in a modern, optimized fork of the xhprof extension from facebook as open-source. (c) tideways gmbh 2014-2017, (c) facebook 2009
register for a free trial on https://tideways.io3、ues profiler
3.0 准备工作
3.0.1 快速起一个 web 项目
可以找任意框架,或者你当前的项目,这里只是为了演示,用了这个仓库 。
[root@centos7 php-xhprof]# pwd
/code/php-xhprof
# 克隆项目
[root@centos7 php-xhprof]# git clone https://github.com/tacks9/php-framework-by-composer pfc
[root@centos7 php-xhprof]# ls pfc/
app  bootstrap.php  composer.json  composer.lock  composer.phar  config  data  public  readme.md  services  vendor
# 配置 nginx
[root@centos7 php-xhprof]# vim /etc/nginx/conf/pfc.conf
server
{
    listen 80;
    server_name pfc.tacks.com;
    index index.html index.htm index.php index.shtml index.shtm;
    root /code/php-xhprof/pfc/public;
    access_log /logs/tacks/pfc.tacks.com-access.log;
    error_log /logs/tacks/pfc.tacks.com-error.log;
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param script_filename $document_root$fastcgi_script_name;
        fastcgi_param server_software nginx;
        include fastcgi_params;
    }
}
# 重启 nginx
[root@centos7 php-xhprof]# systemctl restart nginx
# 设置 hosts
[root@centos7 php-xhprof]# vim /etc/hosts
127.0.0.1 pfc.tacks.com
# 模拟请求
[root@centos7 php-xhprof]# curl http://pfc.tacks.com/home/echotest
<h1> 控制器、方法  home/echotest </h1>3.0.2 部署 xhgui 汉化版 php-monitor
# 克隆项目
[root@centos7 php-xhprof]# git clone https://github.com/laynefyc/php-monitor monitor
[root@centos7 monitor]# pwd
/code/php-xhprof/monitor
# composer 安装依赖
[root@centos7 monitor]# composer install
[root@centos7 monitor]# ls
composer.json  composer.lock  phpunit.xml  public  readme.md  readme-zh_cn.md  src  tests  vendor
# 配置 nginx
[root@centos7 monitor]# vim /etc/nginx/conf/monitor.conf
server
{
    listen 80;
    server_name monitor.tacks.com;
    index index.html index.htm index.php index.shtml index.shtm;
    root /code/php-xhprof/monitor/public;
    access_log /logs/tacks/monitor.tacks.com-access.log;
    error_log /logs/tacks/monitor.tacks.com-error.log;
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param script_filename $document_root$fastcgi_script_name;
        fastcgi_param server_software nginx;
        include fastcgi_params;
    }
}
# 重启 nginx
[root@centos7 monitor]# systemctl restart nginx
# 配置 host
# 127.0.0.1 monitor.tacks.com
3.0.3 php-monitor 默认配置
- src/config/config.php- debug默认 false
- extension默认- tideways- xhprof
- tideways_xhprof
 
- save存储默认- sqlite,但无论那种方式存储,默认表名- php_monitor- mysql
- mongodb
 
- profiler采集比例
- user登陆账号逻辑在- src/controller/logincontroller.php- web ui 默认账号密码
 
 
return [
    'debug' => false,
    /*
     * support extension: uprofiler, tideways_xhprof, tideways, xhprof
     * default: tideways
     */
    'extension' => 'tideways',
    // 'save' => [
    //     'driver'    => 'mysql',
    //     'host'      => '127.0.0.1:3306',
    //     'database'  => 'php_monitor',
    //     'username'  => '',
    //     'password'  => 'abcd1234',
    //     'charset'   => 'utf8mb4',
    //    'options' => [
    //        1005 => 16777216, //pdo::mysql_attr_max_buffer_size and 16m
    //    ]
    // ],
    //  'save' => [
    //      'driver'    => 'mongodb',
    //      'host'      => '127.0.0.1:27017',
    //      'database'  => 'php_monitor',
    //      'username'  => '',
    //      'password'  => ''
    //  ],
   'save' => [
       'driver'    => 'sqlite',
       'database'  =>  dirname(__dir__).'/db/php_monitor.sqlite3'
   ],
    'profiler' => [
        'enable' => function() {
            return true;//rand(1, 100) > 0;
        },
        'filter_path' => [
            //filter the document_root
            //'/home/www/xhgui/webroot','f:/phppro'
        ]
    ],
    'user' => [
        //login account and password
        ['account'=>'php','password'=>'php'],
        ['account'=>'admin','password'=>'abcd']
    ]
];- 访问


3.0.4 php-monitor 源码修改,修复 profiler.enable 回调函数
- src/common/pmonitor.php
将
if(!$config['profiler']['enable']代码15行位置,修改为,否则无法过滤域名的作用
if(!$config['profiler']['enable']()3.0.5 pfc.tacks.com web 项目 nginx 配置修改
- 增加一行 auto_prepend_file设置,然后重启 nginx
location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9001;
        fastcgi_index index.php;
        fastcgi_param script_filename $document_root$fastcgi_script_name;
        fastcgi_param server_software nginx;
        fastcgi_param php_value "auto_prepend_file=/code/php-xhprof/monitor/src/autoprepend.php";
        include fastcgi_params;
    }3.0.6 pfc.tacks.com web 项目,新增 mysql,导入 sql 数据
- 查看 pfc/data/pfc.sql,新增article表, 以及录入数据
可以访问
- http://pfc.tacks.com/home/recentlist
- http://pfc.tacks.com/home/article
3.0.7 php-monitor 设置 mongodb 索引
[root@centos7 monitor]# mongo
> show dbs
> use php_monitor // please select your own database
> db.php_monitor.createindex({"url":1})
> db.php_monitor.createindex({"ip":1})3.1 使用 tideways 搭配 php-monitor
3.1.1 调整 php-monitor 的配置
- php 扩展
[root@centos7 monitor]# php --ri tideways
tideways
tideways => 4.1.7- php.ini的扩展配置
; 启用 mongodb
[mongodb]
extension=mongodb.so
; 启用 tideways
[tideways]
extension=tideways.so
tideways.auto_prepend_library=0
tideways.sample_rate=100
; 关闭 tideways_xhprof
;[tideways_xhprof]
;extension=tideways_xhprof.so- php-monitor配置修改- extension采用- tideways
- save采用- mongodb
- profiler收集- pfc.tacks.com域名
- userweb ui 账号密码采用- php
 
return [
    'debug' => true,
    'extension' => 'tideways',
    'save' => [
         'driver'    => 'mongodb',
         'host'      => '127.0.0.1:27017',
         'database'  => 'php_monitor',
         'username'  => '',
         'password'  => ''
    ],
    'profiler' => [
        'enable' => function() {
            // return true;//rand(1, 100) === 42;
            if ($_server['server_name'] == 'pfc.tacks.com') {
                return true;
                // 100%采样,默认为1%
                return rand(1, 100) === 42;
            } else {
                return false;
            }
        },
        'filter_path' => [
            //filter the document_root
            //'/home/www/xhgui/webroot','f:/phppro'
        ]
    ],
    'user' => [
        ['account'=>'php','password'=>'php'],
    ]
];3.1.2 php-monitor 监控大发黄金版app下载首页
- 访问

3.1.3 php-monitor 请求url折线图
- 点击列表的 “请求url” ,查看请求的折线图

3.1.4 php-monitor 请求详情页
- 点击列表的“请求时间”
3.1.5 php-monitor 请求火焰图
3.1.6 php-monitor sql执行记录
- 访问
3.2 使用 tideways_xhprof 搭配 php-monitor
3.2.1 调整 php-monitor 的配置
- php 扩展
[root@centos7 monitor]# php --ri tideways_xhprof
tideways_xhprof
version => 5.0.4- php.ini的扩展配置,修改后重启 php
; 启用 mongodb
[mongodb]
extension=mongodb.so
; 关闭 tideways
;[tideways]
;extension=tideways.so
;tideways.auto_prepend_library=0
;tideways.sample_rate=100
; 开启 tideways_xhprof
[tideways_xhprof]
extension=tideways_xhprof.so- php-monitor配置修改- extension采用- tideways_xhprof
- save采用- mongodb
- profiler收集- pfc.tacks.com域名
- userweb ui 账号密码采用- php
 
return [
    'debug' => true,
    'extension' => 'tideways_xhprof',
    'save' => [
         'driver'    => 'mongodb',
         'host'      => '127.0.0.1:27017',
         'database'  => 'php_monitor',
         'username'  => '',
         'password'  => ''
    ],
    'profiler' => [
        'enable' => function() {
            // return true;//rand(1, 100) === 42;
            if ($_server['server_name'] == 'pfc.tacks.com') {
                return true;
                // 100%采样,默认为1%
                return rand(1, 100) === 42;
            } else {
                return false;
            }
        },
        'filter_path' => [
            //filter the document_root
            //'/home/www/xhgui/webroot','f:/phppro'
        ]
    ],
    'user' => [
        ['account'=>'php','password'=>'php'],
    ]
];3.2.2 php-monitor 监控器无法查看 sql 执行记录
- 访问
3.2.3 php-monitor 监控器其他服务都正常
- …
本作品采用《cc 协议》,转载必须注明作者和本文链接
tacks
                                
                             
 
支持swoole么
我debian 安装一直/www/server/php/73/lib/php/extensions/no-debug-non-zts-20180731/tideways.so 报错 segmentation fault