使用 PHP-Valgrind 扩展生成 profile 分析 PHP 脚本性能

之前我们使用 XDebug 工具来生成 profile ,后来鸟哥写了个更简单的扩展 php-valgrind ,它依赖于 valgrind
鸟哥文章地址 https://www.laruence.com/2013/08/14/2899.html

安装 valgrind
yum install -y valgrind

或者去 官网 下载 tar 包

cd /usr/local/src
wget https://sourceware.org/pub/valgrind/valgrind-3.16.1.tar.bz2
tar -jxvf valgrind-3.16.1.tar.bz2
cd valgrind-3.16.1
./configure
make && make install

valgrind -h
安装 php-valgrind 扩展
cd /usr/local/src
wget https://github.com/laruence/php-valgrind/archive/valgrind-0.1.0.tar.gz
tar -xf valgrind-0.1.0.tar.gz
cd php-valgrind-valgrind-0.1.0
phpize
./configure
make && make install

扩展地址
/usr/local/src/php-valgrind-valgrind-0.1.0/modules/valgrind.so

查找扩展目录
php -i | grep extension_dir
extension_dir => /usr/local/lib/php/extensions/no-debug-non-zts-20200930 => /usr/local/lib/php/extensions/no-debug-non-zts-20200930

cd /usr/local/lib/php/extensions/no-debug-non-zts-20200930
opcache.so  sodium.so  valgrind.so

看来已经被复制过去了,接下来修改 php.ini
echo extension=valgrind >> /usr/local/etc/php/php.ini

php -m | grep valgrind

安装完毕
测试

代码 code.php

<?php

callgrind_toggle();

test();

callgrind_toggle();

function test() {
    $tt = 0;
    $temp = funa($tt);
    $res = fun1($temp);
    var_dump($res);
}

function funa(int $a): int {
    $a++;
    return funb($a);
}
function funb(int $a): int {
    $a++;
    return func($a);
}
function func(int $a): int {
    $a++;
    return $a;
}

function fun1($x): int {
    return $x * $x;
}

执行:

valgrind --tool=callgrind --collect-atstart=no --instr-atstart=no php code.php

当前目录下多出了一个文件 callgrind.out.14937,此文件就是 profile 文件。

使用 qcachegrind 软件打开 callgrind.out.14937,效果如下:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值