在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
介绍1 背景
2 功能tideways是用来测试PHP性能的扩展,它能获取PHP执行的整个过程中调用的函数、调用函数次数、执行时间、CPU时间、内存占用、内存峰值、总执行时间、总CPU时间、总内存占用、总内存峰值等数据,通过以上数据进行分析,找出PHP的性能瓶颈、分析PHP执行过程等。 3 优点
4 缺点虽然是非侵入式的,但是如果对每个接口生成执行日志,那么对CPU和内存的消耗是不可忽略的。 5 实现原理
应用接下来介绍两种应用方式:侵入式和非侵入式 侵入式指的是在代码中添加代码,侵入式使用的是默认的ui; 非侵入式指的是在不对代码做任何改动,通过改动nginx/apache实现代码注入,非侵入式使用的是xhgui; 安装 tideways_xhprof git clone "https://github.com/tideways/php-xhprof-extension.git" cd php-xhprof-extension phpize ./configure --with-php-config=/usr/local/php7/bin/php-config make sudo make install 在php.ini中加上 extension=tideways_xhprof.so
非侵入式:<?php tideways_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 [email protected]:phacility/xhprof.git 将此存储库中的 如果想要看函数调用笔记需要安装Callgraph 安装 Callgraph Callgraph 实际由三个工具组合而成。 sudo apt-get install cflow graphviz 接下来安装 tree2dotx 和 Callgraph,这里都默认安装到 /usr/local/bin。 wget -c https://github.com/tinyclub/linux-0.11-lab/raw/master/tools/tree2dotx wget -c https://github.com/tinyclub/linux-0.11-lab/raw/master/tools/callgraph sudo cp tree2dotx callgraph /usr/local/bin sudo chmod +x /usr/local/bin/{tree2dotx,callgraph} 接下来展示两张效果图:
侵入式:侵入式使用的 xhgui 需要用到 mongodb 安装xhguigit clone [email protected]:perftools/xhgui.git 配置Nginxserver { 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%的采样率,改成return True;则标识每次都采样 'profiler.enable' => function() { // url 中包含debug=1则百分百捕获 if(!empty($_GET['debug'])){ return True; }else{ // 1%采样 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' : -1 } ) > db.results.ensureIndex( { 'profile.main().wt' : -1 } ) > db.results.ensureIndex( { 'profile.main().mu' : -1 } ) > db.results.ensureIndex( { 'profile.main().cpu' : -1 } ) > db.results.ensureIndex( { 'meta.url' : 1 } ) 最后展示几张xhgui的效果图
|
2022-08-30
2022-08-17
2022-11-06
2022-08-15
2022-08-18
请发表评论