tideways_xhprof
1、安装 tideways_xhprof 拓展
php是编译安装的:
1 [root@localhost ~]# cd /datas/package/ 2 [root@localhost package]# wget https://github.com/tideways/php-xhprof-extension/archive/refs/tags/v5.0.4.tar.gz -O php-xhprof-extension-5.0.4.tar.gz 3 [root@localhost package]# tar -xzf php-xhprof-extension-5.0.4.tar.gz 4 [root@localhost package]# cd php-xhprof-extension-5.0.4/ 5 [root@localhost php-xhprof-extension-5.0.4]# /datas/soft/php73/bin/phpize 6 [root@localhost php-xhprof-extension-5.0.4]# ./configure --with-php-config=/datas/soft/php73/bin/php-config 7 [root@localhost php-xhprof-extension-5.0.4]# make 8 [root@localhost php-xhprof-extension-5.0.4]# make install 9 [root@localhost php-xhprof-extension-5.0.4]# ll /datas/soft/php73/lib/php/extensions/no-debug-non-zts-20180731/ 10 -rwxr-xr-x. 1 root root 122549 9月 23 10:49 tideways_xhprof.so 11 [root@localhost php-xhprof-extension-5.0.4]# vim /datas/soft/php73/etc/php.ini # 加入 extension=tideways_xhprof.so 12 [root@localhost php-xhprof-extension-5.0.4]# php --ri tideways_xhprof 13 tideways_xhprof 14 Version => 5.0.4 15 Clock Source => clock_gettime
php是yum安装的:
[root@fhys-api-b7dd ~]# yum --enablerepo=remi search tideways_xhprof No matches found. [root@fhys-api-b7dd ~]# wget https://github.com/tideways/php-xhprof-extension/archive/refs/tags/v5.0.4.tar.gz -O php-xhprof-extension-5.0.4.tar.gz [root@fhys-api-b7dd ~]# tar -xzf php-xhprof-extension-5.0.4.tar.gz [root@fhys-api-b7dd ~]# cd php-xhprof-extension-5.0.4 [root@fhys-api-b7dd php-xhprof-extension-5.0.4]# /opt/remi/php74/root/bin/phpize Can't find PHP headers in /opt/remi/php74/root/usr/include/php The php74-php-devel package is required for use of this command. [root@fhys-api-b7dd php-xhprof-extension-5.0.4]# yum --enablerepo=remi install php74-php-devel [root@fhys-api-b7dd php-xhprof-extension-5.0.4]# /opt/remi/php74/root/bin/phpize [root@fhys-api-b7dd php-xhprof-extension-5.0.4]# ./configure --with-php-config=/opt/remi/php74/root/bin/php-config [root@fhys-api-b7dd php-xhprof-extension-5.0.4]# make [root@fhys-api-b7dd php-xhprof-extension-5.0.4]# make install Installing shared extensions: /opt/remi/php74/root/usr/lib64/php/modules/ [root@fhys-api-b7dd php-xhprof-extension-5.0.4]# ll /opt/remi/php74/root/usr/lib64/php/modules/ -rwxr-xr-x 1 root root 162552 Sep 23 17:05 tideways_xhprof.so [root@fhys-api-b7dd php-xhprof-extension-5.0.4]# echo 'extension=tideways_xhprof.so' > /etc/opt/remi/php74/php.d/50-tideways_xhprof.ini [root@fhys-api-b7dd php-xhprof-extension-5.0.4]# php74 --ri tideways_xhprof tideways_xhprof Version => 5.0.4 Clock Source => clock_gettime
2、接入代码
以 thinkphp5.1 为例,调整 vendor/topthink/think-swoole/src/Http.php 的 onRequest 方法
1 public function onRequest($request, $response) 2 { 3 tideways_xhprof_enable(TIDEWAYS_XHPROF_FLAGS_MEMORY | TIDEWAYS_XHPROF_FLAGS_CPU); 4 // 执行应用并响应 5 $this->app->swoole($request, $response); 6 file_put_contents( 7 Env::get('runtime_path') . date('YmdHis') . str_replace('/', '-', $request->server['request_uri']) . '-' . uniqid() . '.xhprof.xhprof', 8 serialize(tideways_xhprof_disable()) 9 ); 10 }
3、查看图形界面
下载 https://codeload.github.com/longxinH/xhprof/zip/refs/heads/master 代码,并把 nginx 的 root 指向 xhprof_html 目录,在 xhprof_html 下新建 files 目录,把上面生成的文件拷进来,访问
这是因为没有安装 xhprof 拓展,如果不想安装,可以稍微调整下 index.php 和 callgraph.php 的代码
1 // 找到 XHProfRuns_Default ,传入目录 2 $xhprof_runs_impl = new XHProfRuns_Default('./files'); 3 // 新增几行接收参数 4 $run = $_GET['run']; 5 $source = $_GET['source']; 6 $params = ['run' => $run, 'source' => $source];
图片这里可能还会报 failed to execute cmd ” dot -Tpng”,需运行
1 yum install -y libpng 2 yum install -y graphviz
5、官方github
https://github.com/tideways/php-xhprof-extension
https://github.com/longxinH/xhprof