codon/profiler

此包已被弃用且不再维护。未建议替代包。

PHP 5.4+ 性能分析/基准测试工具

1.0.0 2012-08-16 00:10 UTC

This package is not auto-updated.

Last update: 2020-08-21 18:03:41 UTC


README

一个基于 PEAR Benchmark 包的 PHP 5.4+ 性能分析类

当前待办事项列表

安装

将其添加到 composer.json 文件中

"require": {
    "codon/profiler": "*"
}

然后运行 composer update

./composer.phar update

基本用法

此类广泛使用闭包,所有方法(除 getResults 外)都是可链的。声明一个新的 Profiler 类

<?php
$data = [/* ... */];
$profiler = new \Codon\Profiler();
$profiler->set('showOutput', false)
    ->add([
        'name' => 'Count in loop', 'iterations' => 100,
        'function' => function() use ($data, &$profiler) {
            // This is the code to benchmark:
            for($i = 0; $i <= count($data); $i++) {
                echo $data[$i] . "\n";
            }
        }
    ])->run()->showResults();

它将输出类似的内容

Tests started at: 2012-08-15T12:05:37-04:00
Tests run: 2, iterations: 200
PHP Version: 5.4.5-1~dotdeb.0

Count in loop               (Iterations: 100)
---------
Timers:
              total:        0.000011045170

设置

要更改性能分析器设置,请使用 set() 函数

<?php
$profiler->set($name, $value);

一些设置(类型和默认值)

  • showOutput - bool (false) - 抑制测试运行中的输出
  • tareRuns - bool (true) - 运行闭包有一些惩罚。这将平均运行空闭包的时间,并从所有运行的平均值中减去(仅从总数中减去,不包括在内部调用的检查点或计时器)
  • formatMemoryUsage - bool (true) - 以可消化的格式显示内存使用情况

使用性能分析器

您可以使用两种方式使用配置文件

  1. 使用 add() 添加测试;(见 test/benchmark.php)
  2. 使用 startTimer()/endTimer() 内联配置文件;(见 test/inline.php)

add()

要添加测试,您可以使用 add() 方法,该方法接受一个数组

<?php

$test = [
    'name' => 'Benchmark name',
    'iterations' => # of times to run
    'function' => closure of the test to run
];

$profiler->add($test);

此方法可以链式调用

提示:为了在测试运行中使用检查点/计时器和其他功能,请通过 use(作为引用)传递 Profiler 对象。您还可以通过 use 传递测试所需的任何其他数据/变量

<?php
$test = [
    'name' => 'Sample',
    'iterations' => 100
    'function' => function() use (&$profiler) {

        $profiler->checkpoint('Started!');

        // some code

        $profiler->startTimer('Subsection');
        // Subsection of code
        $profiler->endTimer('Subsection');
    }
];

$profiler->add($test);

# You can chain these too
$profiler->add($test1)->add($test2);

run()

要运行所有测试,请调用 run()

<?php
$profiler->run();

传递参数

您的测试函数也可以有任何参数,并且传递给 run() 的任何参数都将传递给闭包。例如,如果您正在测量类中方法的运行时间

<?php
class Test {

    public $_data = [];

    protected $_profiler;

    public function __construct() {
        $this->_profiler = new \Codon\Profiler();
    }

    public function runMethodProfiler() {
        $this->_profiler->add([
            'name' => 'testFunction', 'iterations' => 1,
            'function' => function($class) use (&$this->_profiler) {
                $class->testFunction($class->_data);
            }
        ])->run($this);
    }

    public function testFunction($data = '') {

        // Benchmark something in here

    }
}

clear()

使用 clear() 函数清除先前测试的结果。

<?php
$profiler->add(/*...*/)->run();
// something
$profiler->clear()->run(); # Re-run the tests

clearAll()

使用clearAll()函数将分析器重置为没有任何测试或运行。

<?php
$profiler->add(/*...*/)->run();
$profiler->clearAll();
$profiler->add(/*...*/)->run();

startTimer($name)/endTimer($name)

用于测量正在运行的基准测试中的点。也可以直接使用(见下文)

  • $name 计时器名称
<?php
$profiler
 ->add([ 'name' => 'Count in loop', 'iterations' => 1,
     'function' => function() use ($data, &$profiler) {

        $profiler->startTimer('Inside loop');
        for($i = 0; $i < count($data); $i++) {
            echo $data[$i] . "\n";
        }
        $profiler->endTimer('Inside loop');

        // Or this way:

        $timer_outside = $profiler->startTimer('Outside loop');
        $count = count($data);
        for($i = 0; $i < $count; $i++) {
            echo $data[$i] . "\n";
        }
        $profiler->endTimer($timer_outside);
     })->run();

checkpoint($name)

从当前测试的开始标记一个检查点时间

  • $name 检查点名称

markMemoryUsage($name)

这将记录在调用此方法时的内存使用情况。获取脚本、PHP引擎和峰值使用量。

  • $name 当前点的名称
<?php
$profiler
->clearAll()
 ->add([
     'name' => 'Count in loop',
     'iterations' => 100,
     'function' => function() use ($data, &$profiler) {

        $profiler->markMemoryUsage('Before loop');
        for($i = 0; $i < count($data); $i++) {
            echo $data[$i] . "\n";
        }
        $profiler->markMemoryUsage('After loop');

     })->run();

showResults($html = false, $return = false)

显示一个格式化的表格

  • $html 如果不是false,则返回
    而不是换行符,并将其放入
     tag with the class name of what's passed
  • $return 要将结果作为字符串返回,或立即将其输出
Tests started at: 2012-08-15T12:40:28-04:00
Tests run: 1, iterations: 300
PHP Version: 5.4.5-1~dotdeb.0
Count in loop               (Iterations: 100)
---------
Timers:
        Inside loop:        0.000016908646
       Outside loop:        0.000004930496
              total:        0.000022240901

getResults()

返回一个包含运行原始结果的数组

内联使用分析器

您可以在任何内联代码中调用startTimer()/endTimer()markMemoryUsage()函数。

<?php
$data = [];
for($i = 0; $i < 1000; $i++) {
	$data[] = $i;
}

# You can pass options to the constructor
$profiler = new \Codon\Profiler([
	'showOutput' => false
]);

$profiler->markMemoryUsage('start');
$profiler->startTimer('Count in loop');

for($i = 0; $i < count($data); $i++) {
	echo $data[$i] . "\n";
}

$profiler->endTimer('Count in loop');
$profiler->markMemoryUsage('end');

$profiler->showResults();

这将显示

Tests started at: 2012-08-15T12:30:02-04:00
Tests run: 1, iterations: 0
PHP Version: 5.4.5-1~dotdeb.0

---------
Timers:
      Count in loop:        0.001808881760

      Memory Usage:         script         peak        total
              start:        435 KB       456 KB       512 KB
                end:        438 KB       456 KB       512 KB

示例

测量两种执行for循环的方式

<?php
$profiler
	->clearAll()
    ->add([
        'name' => 'Count in loop',
        'iterations' => 100,
        'function' => function() use ($data, &$profiler) {

			$profiler->startTimer('Inside loop');
            for($i = 0; $i < count($data); $i++) {
                echo $data[$i] . "\n";
            }
			$profiler->endTimer('Inside loop');


			$profiler->startTimer('Outside loop');
			$count = count($data);
			for($i = 0; $i < $count; $i++) {
				echo $data[$i] . "\n";
			}
			$profiler->endTimer('Outside loop');
        }
    ])
	->run()
	->showResults();

显示

Tests started at: 2012-08-15T12:40:28-04:00
Tests run: 1, iterations: 300
PHP Version: 5.4.5-1~dotdeb.0
Count in loop               (Iterations: 100)
---------
Timers:
        Inside loop:        0.000016908646
       Outside loop:        0.000004930496
              total:        0.000022240901

使用计时器和检查点

<?php
$profiler = new \Codon\Profiler();
$profiler
	->set('showOutput', false)
    ->add([
        'name' => 'Count in loop',
        'iterations' => 100,
        'function' => function() use ($data, &$profiler) {

            // This is the code we are profiling

			$profiler->markMemoryUsage('Start of loop');
			$profiler->startTimer('Loop only');
            for($i = 0; $i < count($data); $i++) {

				if($i === 500) {
					$profiler->checkpoint('halfway');
					$profiler->markMemoryUsage('halfway');
				}

                echo $data[$i] . "\n";
            }
			$profiler->endTimer('Loop only');
        }
    ])
	->run()
	->showResults();

这将输出

Tests started at: 2012-08-15T12:30:07-04:00
Tests run: 1, iterations: 100
PHP Version: 5.4.5-1~dotdeb.0

Count out of loop           (Iterations: 100)
---------
Timers:
          Loop only:        0.000006530285
              total:        0.000006922960

Checkpoints:
            halfway:        0.000361402035

      Memory Usage:         script         peak        total
      Start of loop:        472 KB       491 KB       512 KB
            halfway:        488 KB       491 KB       512 KB