minond/profiler

PHP 性能分析器

dev-master 2014-01-31 06:55 UTC

This package is not auto-updated.

Last update: 2024-09-23 14:18:22 UTC


README

重要提示

需要注意的是,这是一个非常简单的性能分析器,仅用于了解脚本的性能。

性能分析器

创建新的性能分析器

use util\profile\Profiler;
use util\profile\Snapshot;

$profiler = new Profiler('AcmeAnvils');

启动和关闭性能分析器

$profiler->start();
$profiler->stop();

监听性能分析器事件

// triggered by calling Profiler::start
$profiler->on('start', function(Snapshot $snapshot) {
  youroutput('profiling: %s', $snapshot->name);
});

// triggered on every PHP tick
$profiler->on('tick', function(Snapshot $snapshot) {
  youroutput('.');
});

// triggered by calling Profiler::stop
// you'll at least want to register to this event,
// otherwise the profiling data it worthless
$profiler->on('stop', function(Snapshot $snapshot) {
  youroutput('done profiling: %s', $snapshot->name);
  print_r($snapshot);
});

不需要在性能分析器上调用 stop 方法,因为它会注册一个关闭函数,该函数会触发对 stop 的调用。

性能分析

性能分析代码示例

use util\profile\Profiler;
use util\profile\Snapshot;

$profiler = new Profiler('AcmeAnvils');
$profiler->start();
$profiler->on('stop', function(Snapshot $snapshot) {
  print_r($snapshot);
});

// code you want to profile
// ...

$profiler->stop();

快照

示例 Snapshot 对象

object(util\profile\Snapshot)[33]
  public 'name' => string 'AcmeAnvils' (length=10)
  public 'mode' => int 2
  public 'maxmemory' => int 1135472
  public 'avgmemory' => float 1128570.4
  public 'currentmemory' => int 1128568
  public 'currenttime' => float 1368675239.0269
  public 'starttime' => float 1368675239.0263
  public 'startmemory' => int 1122604
  public 'endtime' => float 1368675239.0269
  public 'endmemory' => int 1128520
  public 'runtime' => float 0.00060391426086426
  public 'trace' =>
    array (size=5)
      0 =>
        array (size=7)
          'file' => string 'file.php' (length=53)
          'line' => int 10
          'function' => string 'function_name' (length=14)
          'class' => null
          'type' => null
          'memory' => int 1126948
          'time' => float 1368675239.0264

报告

报告用于以人类/环境友好的格式输出快照对象

use util\profile\reports\Chart;

$chart = new Chart;
$chart->prepare($snapshot);
$chart->configure(['chart_type' => 'LineChart']);
echo $chart->output();

安装

使用 composer 安装: "minond/profiler": "dev-master"