alecrabbit/php-simple-profiler

此包已被弃用,不再维护。未建议替换包。

用于功能基准测试,包含计数器和计时器类。


README

PHP Version Build Status Scrutinizer Code Quality Code Coverage Total Downloads

Latest Stable Version Latest Version Latest Unstable Version

License Average time to resolve an issue Percentage of issues still open

演示

查看演示

快速开始

基准测试
use AlecRabbit\Tools\BenchmarkSymfonyProgressBar;

require_once __DIR__ . '/vendor/autoload.php';

$benchmark = new BenchmarkSymfonyProgressBar(900000);
$benchmark
    ->addFunction('hrtime', true); 
$benchmark
    ->addFunction('microtime', true);
echo $benchmark->run()->noReturns()->report() . PHP_EOL;
echo $benchmark->stat() . PHP_EOL;

输出将类似于

Results:
Benchmark:
1.  150.1ns (  0.00%) ⟨1⟩ hrtime(boolean) 
2.  157.7ns (  5.07%) ⟨2⟩ microtime(boolean) 
Benchmarked: 2 
Memory: 0.88MB(0.92MB) Real: 2.00MB(2.00MB)

Done in: 2.0s

更多信息请参阅示例

注意:某些示例可能不是最新的... 进行中

进行中

安装

目前建议在开发过程中使用此包

composer require --dev alecrabbit/php-simple-profiler

或者

composer require alecrabbit/php-simple-profiler

基准测试类

有时候你需要从两种或更多不同的方法中选择。基准测试类可以帮助你选择哪种更快 :)

  • Benchmark::class(无默认进度条,静默测量)
  • BenchmarkSymfonyPB::class(带有Symfony进度条)
 26% [████████████████████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░]  1 sec/4 secs
  • BenchmarkSimplePB::class(带有星形进度条)
******************************
示例

假设你想知道哪个更快:call_user_func($func) 还是 $func()。首先你需要创建一个基准测试类的实例

$b = new BenchmarkSymfonyPB(900000) // with Symfony Progress bar, 900000 measurments

注意:Xdebug扩展会大幅降低速度!请禁用它(我使用了两个不同的镜像无Xdebug有Xdebug

然后你需要添加要测试的函数。但首先让我们添加一个闭包

$func = function (array $a) {
    return array_sum($a);
};

现在我们准备好添加函数

$a = [1, 2, 3];

$b->addFunction('call_user_func', $func, $a);

$b->addFunction($func, $a);

现在你可以运行基准测试

$b->run();

获取结果

$report = $b->report(); // you can get report object and use data from it 
echo $report . PHP_EOL; // or you can print it by default formatter
echo $b->stat() . PHP_EOL;

结果将类似于这样

Results:
Benchmark:
1.  219.3ns (  0.00%) $func(array) $func(...$args)
2.  287.4ns ( 31.09%) call_user_func(array) \call_user_func($func, ...$args)
All returns are equal: 
integer(6) 
Benchmarked: 2 
Memory: 0.87MB(0.91MB) Real: 2.00MB(2.00MB)

Done in: 1.1s

Profiler::class

Profiler是一种包装Counter和Timer的类,如果你需要同时使用它们。

$profiler = new Profiler();

for ($i = 0; $i < 100; $i++) {
    $profiler->counter()->bump();
    someOperation();
    $profiler->timer()->check();
}

echo $profiler->report() . PHP_EOL;

Counter::class

// todo

Timer::class

// todo