centos7 + php7.4のAPIインタフェースは、オーバーヘッド収集xhprofを搭載し、

xhprofは非常に良いPHPプロファイラツールFacebookの09インディアンアウトではありませんが、後にFacebookのhhvmに移動し、もはや維持され、それがPHP7に多くのバグを持っています。

本明細書で使用する場合、互換性のxprofのgithubのプロジェクトPHP7に行うために他の誰かです。

インストール

cd ~/source
git clone https://github.com/longxinH/xhprof
cd xhprof/extension/

PHP-config設定の場所を検索

which php-config

プラグインをインストールします。

phpize
./configure --with-php-config=/usr/local/php/bin/php-config  --enable-xhprof
make
make install

変更のphp.ini、PHP --ini最初の位置php.iniファイルを見つけ、その後、以下で添加します。

[xhprof]
extension=xhprof.so;
xhprof.output_dir=/var/tmp/xhprof

xhprof.output_dirは、各実行run_id.project_name.xhprofファイルsave_run法xhprofを生成します出力ディレクトリxhprofです。それがどこにあるこのディレクトリには関係ありません。

再起動のphp-FPM

service php-fpm restart
php -m | grep xhprof

この時点で、拡張子xhprof正常にインストールされている、ことがわかります

nginxの設定アクセス

ファイルが.xhprof生成されると、我々は結果を表示するxhprof_lib使用することができます。

server {
    listen 80;
    root /var/www/html/xhprof/xhprof_html;
    server_name your_host;
    location = / {
        index index.php;
    }
    location ~ \.php {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

XHProfのxhprof_libとの2つのフォルダxhprof_html / var / www / htmlと設定/ xhprof /下げ、その後、訪問のhttp:生成されたデータの解析ファイルに示されているよう//your_host/xhprof_html/index.phpを見ることができます。

トップ構成用のファイルパスを保存します。

使用

この方法の一つ

包まれるには、次のコードを使用すると、コードを確認することができます。

<?php
//header
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
 
// 要检查性能的代码


//footer
$xhprof_data = xhprof_disable();
include_once  '/var/www/html/xhprof/xhprof_lib/utils/xhprof_lib.php';
include_once  '/var/www/html/xhprof/xhprof_lib/utils/xhprof_runs.php';
$xhprof_runs = new \XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, 'test');//test为生成文件内包含的一个关键字区分哪个接口的,可自定义

header.phpのとfooter.php:別途PHPファイルに記述されたコードの前と後にも分離することができます

// header.php
<?php
if (extension_loaded('xhprof')) {
    include_once 'xhprof_lib/utils/xhprof_lib.php';
    include_once 'xhprof_lib/utils/xhprof_runs.php';
    xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
}
?>
// footer.php
<?php
if (extension_loaded('xhprof')) {
    $profiler_namespace = 'your_project';
    $xhprof_data = xhprof_disable();
    $xhprof_runs = new XHProfRuns_Default();
    $run_id = $xhprof_runs->save_run($xhprof_data, $profiler_namespace);
}
?>

その後、二つのファイルと次のようにコードをラップ:

<?php
include_once  '/var/www/html/xhprof/header.php';

// 要检查性能的代码

include_once  '/var/www/html/xhprof/footer.php';

例:

include_once  '/var/www/html/xhprof/header.php';

function hallo() {
}

function simpleucall($n) {
  for ($i = 0; $i < $n; $i++)
    hallo();
}

function simpleudcall($n) {
  for ($i = 0; $i < $n; $i++)
    hallo2();
}

function hallo2() {
}

function simpleicall($n) {
  for ($i = 0; $i < $n; $i++)
    func_num_args();
}

class Foo {
    static $a = 0;
    public $b = 0;
    const TEST = 0;

    static function read_static($n) {
        for ($i = 0; $i < $n; ++$i) {
            $x = self::$a;
        }
    }

    static function write_static($n) {
        for ($i = 0; $i < $n; ++$i) {
            self::$a = 0;
        }
    }

    static function isset_static($n) {
        for ($i = 0; $i < $n; ++$i) {
            $x = isset(self::$a);
        }
    }

    static function empty_static($n) {
        for ($i = 0; $i < $n; ++$i) {
            $x = empty(self::$a);
        }
    }

    static function f() {
    }

    static function call_static($n) {
        for ($i = 0; $i < $n; ++$i) {
            self::f();
        }
    }

    function read_prop($n) {
        for ($i = 0; $i < $n; ++$i) {
            $x = $this->b;
        }
    }

    function write_prop($n) {
        for ($i = 0; $i < $n; ++$i) {
            $this->b = 0;
        }
    }

    function assign_add_prop($n) {
        for ($i = 0; $i < $n; ++$i) {
            $this->b += 2;
        }
    }

    function pre_inc_prop($n) {
        for ($i = 0; $i < $n; ++$i) {
            ++$this->b;
        }
    }

    function pre_dec_prop($n) {
        for ($i = 0; $i < $n; ++$i) {
            --$this->b;
        }
    }

    function post_inc_prop($n) {
        for ($i = 0; $i < $n; ++$i) {
            $this->b++;
        }
    }

    function post_dec_prop($n) {
        for ($i = 0; $i < $n; ++$i) {
            $this->b--;
        }
    }

    function isset_prop($n) {
        for ($i = 0; $i < $n; ++$i) {
            $x = isset($this->b);
        }
    }

    function empty_prop($n) {
        for ($i = 0; $i < $n; ++$i) {
            $x = empty($this->b);
        }
    }

    function g() {
    }

    function call($n) {
        for ($i = 0; $i < $n; ++$i) {
            $this->g();
        }
    }

    function read_const($n) {
        for ($i = 0; $i < $n; ++$i) {
            $x = $this::TEST;
        }
    }

}

function read_static($n) {
    for ($i = 0; $i < $n; ++$i) {
        $x = Foo::$a;
    }
}

function write_static($n) {
    for ($i = 0; $i < $n; ++$i) {
        Foo::$a = 0;
    }
}

function isset_static($n) {
    for ($i = 0; $i < $n; ++$i) {
        $x = isset(Foo::$a);
    }
}

function empty_static($n) {
    for ($i = 0; $i < $n; ++$i) {
        $x = empty(Foo::$a);
    }
}

function call_static($n) {
    for ($i = 0; $i < $n; ++$i) {
        Foo::f();
    }
}

function create_object($n) {
    for ($i = 0; $i < $n; ++$i) {
        $x = new Foo();
    }
}

define('TEST', null);

function read_const($n) {
    for ($i = 0; $i < $n; ++$i) {
        $x = TEST;
    }
}

function read_auto_global($n) {
    for ($i = 0; $i < $n; ++$i) {
        $x = $_GET;
    }
}

$g_var = 0;

function read_global_var($n) {
    for ($i = 0; $i < $n; ++$i) {
        $x = $GLOBALS['g_var'];
    }
}

function read_hash($n) {
    $hash = array('test' => 0);
    for ($i = 0; $i < $n; ++$i) {
        $x = $hash['test'];
    }
}

function read_str_offset($n) {
    $str = "test";
    for ($i = 0; $i < $n; ++$i) {
        $x = $str[1];
    }
}

function issetor($n) {
    $val = array(0,1,2,3,4,5,6,7,8,9);
    for ($i = 0; $i < $n; ++$i) {
        $x = $val ?: null;
    }
}

function issetor2($n) {
    $f = false; $j = 0;
    for ($i = 0; $i < $n; ++$i) {
        $x = $f ?: $j + 1;
    }
}

function ternary($n) {
    $val = array(0,1,2,3,4,5,6,7,8,9);
    $f = false;
    for ($i = 0; $i < $n; ++$i) {
        $x = $f ? null : $val;
    }
}

function ternary2($n) {
    $f = false; $j = 0;
    for ($i = 0; $i < $n; ++$i) {
        $x = $f ? $f : $j + 1;
    }
}

/*****/

function empty_loop($n) {
    for ($i = 0; $i < $n; ++$i) {
    }
}

function gethrtime()
{
  $hrtime = hrtime();
  return (($hrtime[0]*1000000000 + $hrtime[1]) / 1000000000);
}

function start_test()
{
  ob_start();
  return gethrtime();
}

function end_test($start, $name, $overhead = null)
{
  global $total;
  global $last_time;
  $end = gethrtime();
  ob_end_clean();
  $last_time = $end-$start;
  $total += $last_time;
  $num = number_format($last_time,3);
  $pad = str_repeat(" ", 24-strlen($name)-strlen($num));
  if (is_null($overhead)) {
    echo $name.$pad.$num."\n";
  } else {
    $num2 = number_format($last_time - $overhead,3);
    echo $name.$pad.$num."    ".$num2."\n";
  }
  ob_start();
  return gethrtime();
}

function total()
{
  global $total;
  $pad = str_repeat("-", 24);
  echo $pad."\n";
  $num = number_format($total,3);
  $pad = str_repeat(" ", 24-strlen("Total")-strlen($num));
  echo "Total".$pad.$num."\n";
}

const N = 5000000;

$t0 = $t = start_test();
empty_loop(N);
$t = end_test($t, 'empty_loop');
$overhead = $last_time;
simpleucall(N);
$t = end_test($t, 'func()', $overhead);
simpleudcall(N);
$t = end_test($t, 'undef_func()', $overhead);
simpleicall(N);
$t = end_test($t, 'int_func()', $overhead);
Foo::read_static(N);
$t = end_test($t, '$x = self::$x', $overhead);
Foo::write_static(N);
$t = end_test($t, 'self::$x = 0', $overhead);
Foo::isset_static(N);
$t = end_test($t, 'isset(self::$x)', $overhead);
Foo::empty_static(N);
$t = end_test($t, 'empty(self::$x)', $overhead);
read_static(N);
$t = end_test($t, '$x = Foo::$x', $overhead);
write_static(N);
$t = end_test($t, 'Foo::$x = 0', $overhead);
isset_static(N);
$t = end_test($t, 'isset(Foo::$x)', $overhead);
empty_static(N);
$t = end_test($t, 'empty(Foo::$x)', $overhead);
Foo::call_static(N);
$t = end_test($t, 'self::f()', $overhead);
call_static(N);
$t = end_test($t, 'Foo::f()', $overhead);
$x = new Foo();
$x->read_prop(N);
$t = end_test($t, '$x = $this->x', $overhead);
$x->write_prop(N);
$t = end_test($t, '$this->x = 0', $overhead);
$x->assign_add_prop(N);
$t = end_test($t, '$this->x += 2', $overhead);
$x->pre_inc_prop(N);
$t = end_test($t, '++$this->x', $overhead);
$x->pre_dec_prop(N);
$t = end_test($t, '--$this->x', $overhead);
$x->post_inc_prop(N);
$t = end_test($t, '$this->x++', $overhead);
$x->post_dec_prop(N);
$t = end_test($t, '$this->x--', $overhead);
$x->isset_prop(N);
$t = end_test($t, 'isset($this->x)', $overhead);
$x->empty_prop(N);
$t = end_test($t, 'empty($this->x)', $overhead);
$x->call(N);
$t = end_test($t, '$this->f()', $overhead);
$x->read_const(N);
$t = end_test($t, '$x = Foo::TEST', $overhead);
create_object(N);
$t = end_test($t, 'new Foo()', $overhead);
read_const(N);
$t = end_test($t, '$x = TEST', $overhead);
read_auto_global(N);
$t = end_test($t, '$x = $_GET', $overhead);
read_global_var(N);
$t = end_test($t, '$x = $GLOBALS[\'v\']', $overhead);
read_hash(N);
$t = end_test($t, '$x = $hash[\'v\']', $overhead);
read_str_offset(N);
$t = end_test($t, '$x = $str[0]', $overhead);
issetor(N);
$t = end_test($t, '$x = $a ?: null', $overhead);
issetor2(N);
$t = end_test($t, '$x = $f ?: tmp', $overhead);
ternary(N);
$t = end_test($t, '$x = $f ? $f : $a', $overhead);
ternary2(N);
$t = end_test($t, '$x = $f ? $f : tmp', $overhead);
total($t0, "Total");

include_once  '/var/www/html/xhprof/footer.php';

実行します。

php index.php

访问ます。http://your_host/xhprof_html/index.php:

プログラムは、テーブルの時間や実行回数データ等の各種統計手法を実行しているされて、あなたはビューを並べ替えることができ、外出先でクリックしてください:

選択:[ビューフルコールグラフ]を参照のタイミングチャートを生成し、赤色表示は+ +呼び出しによって実行される方法を含む、黄色続いて、最大です。

オプション2:フレームワークで

Xhprof_libと呼ばれるサードパーティのライブラリlibフォルダとしてフレームに直接導入します。 

上側のヘッダとフッタファイルの外装内容コード、及びxhprof_lib.php xhprof_runs.phpに正しいと参照することを保証index.phpを全てコード入力ファイルのフレームワークです。

YAF例への入り口:

<?php
//header
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);


//code
define('APPLICATION_PATH', dirname(__FILE__)."/../");

$application = new Yaf_Application( APPLICATION_PATH . "/conf/application.ini");

$application->bootstrap()->run();


//footer
$XHPROF_PATH = APPLICATION_PATH . "library/ThirdParty/";
include_once $XHPROF_PATH . 'xhprof_lib/utils/xhprof_lib.php';
include_once $XHPROF_PATH . 'xhprof_lib/utils/xhprof_runs.php';
$profiler_namespace = 'test';
$xhprof_data = xhprof_disable();
$xhprof_runs = new XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, $profiler_namespace);

?>

概要

複数の単語は、開発環境、またはオンラインをテストするためのいくつかのテストを行うことができますよりも、そのコードを削除する必要があります。

  1. "DOT -Tpng" stderrの:. `(:あなたが実行CMDにエラーが発生した場合は失敗したプロセス:24220):Pangoの-WARNING **を:無効なUTF-8文字列pango_layout_set_textに渡されました(「)。一時的に解決する方法をクリアしていない、あなたはそれを避けるために選択することができます。/ utilsの/ callgraph_utils.phpと出口はコメントアウトxhprof_lib 121122行を印刷します。
  2. エラーエラーが発生した場合:どちらか我々は、プロファイルを見つけることができません RUN_ID XXXまたはしきい値0.01のためのデータを小さすぎるか、「ドット」の画像生成ユーティリティがインストールされていない、 画像をpngの生成できない、生成されたファイルが存在しないからだろう文字認識、修理は、以下のように3
    $cmd = " dot -T".$type;
    // 在 cmd 之后添加一个转码工作就可以了
    $dot_script = iconv("UTF-8", "ASCII//IGNORE", $dot_script);
    
  3. あなたはより良いUIを見たい場合は、以下を参照することができ、リンク1リンク2リンク3  (フリーライドマニュアル)。
  4. ここではいくつかのパラメータ説明されています
パフォーマンスのポイント 説明
インクルーシブタイム すべての実行時関数を含みます
独占タイム/セルフタイム サブツリーの実行時間を含まない、時間関数の実行そのものを過ごします
ウォール時間 費やした時間や壁時計時間
CPU時間 ユーザーは、時間消費コア+を経過します
インクルーシブCPU 含むサブ機能は、一緒にCPUを占有しました
独占CPU それ自体はCPUによって占め機能します
  1. 問題xhprof  やブログ記事に加えて、代替レポのいくつかを言及した  tideways  など 

  2. nginxの参照構成  gitbookの  と  nginxのウィキ 

  3. これは、インターネット上で解決策を見つけるのは難しいですが、私はバグプラットフォームPHPでそれを見つけた:リンク 

参考:http://wulfric.me/2017/08/php7-xhprof/

公開された115元の記事 ウォンの賞賛101 ビュー370 000 +

おすすめ

転載: blog.csdn.net/Alen_xiaoxin/article/details/105309562