php7如何使用xhprof测试php性能?(方法介绍)

发布时间:2024-03-19 点击:130
介绍
1 背景
php的xhprof扩展facebook不再进行更新和维护,因为faceboo已经全面使用hhvm,不再使用php zend引擎。
xhprof不支持新版本的php(php7),tideways扩展是从xhprof项目fork下来继续进行维护的,目前支持php 7.2, 7.1, 7.0, 5.6 and 5.5 。
tideways是开源项目,它收费的只是ui服务,其实 xhgui完全可以满足我们日常的需求
2 功能
tideways是用来测试php性能的扩展,它能获取php执行的整个过程中调用的函数、调用函数次数、执行时间、cpu时间、内存占用、内存峰值、总执行时间、总cpu时间、总内存占用、总内存峰值等数据,通过以上数据进行分析,找出php的性能瓶颈、分析php执行过程等。
3 优点
tideways是一个php扩展,结合xhgui,无需在php代码中进行埋点来监控代码
可以设置执行频率(例如1/100),无需每个请求都生成执行日志,从而导致性能损失;也可以主动控制是否生成执行日志,通过请求参数来控制(debug=1)
有简单直接的ui对数据进行转化
可以自由的搭配条件进行数据筛选,例如分析某个特定的接口,分析某个时间段的接口请求情况等
4 缺点
虽然是非侵入式的,但是如果对每个接口生成执行日志,那么对cpu和内存的消耗是不可忽略的。
5 实现原理
tideways扩展负责生成运行日志nginx中通过配置fastcgi_param php_value auto_prepend_file,在请求开始之前执行auto_prepend_file配置的php文件,文件中利用register_shutdown_function方法,在php进程结束的时候调用tideways_disable来实现tideways的嵌入,然后将执行日志存入mongodb或者mysql或者文件中,通过xhgui分析之后进行展示,展示形式包括柱状图、瀑布流、火焰图。
应用
接下来介绍两种应用方式:侵入式和非侵入式
侵入式指的是在代码中添加代码,侵入式使用的是默认的ui;
非侵入式指的是在不对代码做任何改动,通过改动nginx/apache实现代码注入,非侵入式使用的是xhgui;
安装 tideways_xhprof
git clone "https://github.com/tideways/php-xhprof-extension.git"cd php-xhprof-extensionphpize./configure --with-php-config=/usr/local/php7/bin/php-configmakesudo make install 在php.ini中加上 extension=tideways_xhprof.so
非侵入式:
<?phptideways_xhprof_enable();// your application code$data = tideways_xhprof_disable();file_put_contents( sys_get_temp_dir() . "/" . uniqid() . ".yourapp.xhprof", serialize($data)); // $data = tideways_xhprof_disable(); // file_put_contents( // sys_get_temp_dir() . "/" . date('his', time()) . ".material.xhprof", // serialize($data) // );这里生成的 .xhprof文件在 tmp 目录下,默认ui也会去tmp目录下查找 .xhprof文件
安装默认ui用来查找数据
git clone git@github.com:phacility/xhprof.git将此存储库中的xhprof_lib和xhprof_html目录安装到您的web文件夹中,并导航xhprof_html/index.php 以查看跟踪列表。
如果想要看函数调用笔记需要安装callgraph
安装 callgraph
callgraph 实际由三个工具组合而成。
一个是用于生成 c 函数调用树的 cflow 或者 calltree,下文主要介绍 cflow。
一个处理 dot 文本图形语言的工具,由 graphviz 提升。
一个用于把 c 函数调用树转换为 dot 格式的脚本:tree2dotx
以 ubuntu 为例,分别安装它们:
sudo apt-get install cflow graphviz接下来安装 tree2dotx 和 callgraph,这里都默认安装到 /usr/local/bin。
wget -c https://github.com/tinyclub/linux-0.11-lab/raw/master/tools/tree2dotxwget -c https://github.com/tinyclub/linux-0.11-lab/raw/master/tools/callgraphsudo cp tree2dotx callgraph /usr/local/binsudo chmod x /usr/local/bin/{tree2dotx,callgraph}接下来展示两张效果图:
侵入式:
侵入式使用的 xhgui 需要用到 mongodb
安装xhgui
git clone git@github.com:perftools/xhgui.git配置nginx
server { listen 80; server_name site.localhost; root /users/markstory/sites/awesome-thing/app/webroot/; fastcgi_param php_value "auto_prepend_file=/home/www/xhgui/external/header.php"; #这里依据个人目录而配}这里的意思是在执行项目php代码前 先执行 header.php,从而达到非侵入式检测性能的目的
xhgui配置(生成日志的频率)
在xhgui的config/config.default.php中,可设置采样命中次数;
return rand(1, 100) === 42; 为1%%u7684采样率,改成return true;则标识每次都采样
'profiler.enable' => function() { // url 中包含debug=1则百分百捕获 if(!empty($_get['debug'])){ return true; }else{ // 1%%u91c7样 return rand(1, 100) === 42; }}mongodb的配置
xhgui/config/config.default.php
// can be either mongodb or file. /* 'save.handler' => 'file', 'save.handler.filename' => dirname(__dir__) . '/cache/' . 'xhgui.data.' . microtime(true) . '_' . substr(md5($url), 0, 6), */ 'save.handler' => 'mongodb', // needed for file save handler. beware of file locking. you can adujst this file path // to reduce locking problems (eg uniqid, time ...) //'save.handler.filename' => __dir__.'/../data/xhgui_'.date('ymd').'.dat', 'db.host' => 'mongodb://127.0.0.1:27017', 'db.db' => 'xhprof',mongo服务器的配置
mongo > use xhprof > db.results.ensureindex( { 'meta.server.request_time'

网站建设有哪些缩短加载时间的妙技
电脑桌面有残留选项或图标无法刷新去掉如何解决
申请域名要多长时间
主机访问报错麻烦处理下-虚拟主机/数据库问题
案例云服务器怎么安装宝塔镜像
电脑文件如何隐藏才安全?隐藏电脑文件的方法
网站如何导入云服务器
关于服务器只允许特定IP访问问题的设置-Windows server 2003、2008、2012