zoukankan      html  css  js  c++  java
  • php7使用xhprof测试php性能

      

    介绍

    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_libxhprof_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的效果图

  • 相关阅读:
    Linux命令: 向文件写内容,编辑文件,保存文件,查看文件,不保存文件
    SQL: 左连接,右连接,内连接,左外连接,右外连接,完全连接
    Python: 没有switch-case语句
    Python:键盘输入input
    Python: 猴子分桃。海滩上有一堆桃子,五只猴子来分。
    error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools
    Scrapy安装
    Linux: 回到根目录cd /
    怎么查看是否安装Scrapy
    虚拟环境Scrapy安装
  • 原文地址:https://www.cnblogs.com/xbblogs/p/12215051.html
Copyright © 2011-2022 走看看