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

资源魔 53 0

引见

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-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 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/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

装置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%的采样率,改为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的成果图

相干学习保举:PHP编程从入门到通晓

以上就是php7若何应用xhprof测试php功能?(办法引见)的具体内容,更多请存眷资源魔其它相干文章!

标签: PHP7 xhprof php7开发教程 php7开发资料 php7开发自学 php性能

抱歉,评论功能暂时关闭!