abbadon1334 / phpench
一个图形化的PHP基准测试工具 - 由 mre/phpench 衍生而来
dev-master
2019-10-10 15:26 UTC
Requires
- php: >=7.2.0
- abbadon1334/gnuplot: *@dev
- phplucidframe/console-table: ^1.2
- phpunit/php-timer: *@dev
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-04 10:21:42 UTC
README
PHPench 为 PHP 基准测试创建图形输出。使用 GnuPlot 实时绘制任何函数的运行时间并生成图像。
为什么它有用?
算法是美丽的
有时候两个算法之间的区别很难解释,但很容易展示。
例如,考虑两种排序算法,它们都具有最佳情况的运行时间 O(n*log n)
。根据输入,其中一个可能比另一个快得多。这个工具可以帮助你看到发生了什么。
终结过早优化
当人们告诉你使用单引号而不是双引号包围字符串是一种性能提升时,是时候破除一些神话了。大多数情况下,这样的程序员传说往往是错误的,实际上可能是有害的。
"过早强调效率是一个大错误,可能是大多数编程复杂性和痛苦的原因。" (Donald Knuth)
让我们成为专业人士。让我们测量。
示例
使用 PHPench 有点像编写视觉单元测试。看看这个
<?php require_once __DIR__.'/../vendor/autoload.php'; /* * You can use an closure or a class that implements TestInterface. * * Data that will be processed by the tested function can be executed * without including its execution time. This will provide more accurate data. */ abstract class AbstractBenchmark implements \mre\PHPench\BenchmarkInterface { protected $test; function setUp($arrSize) { $this->test = array(); for ($i=1; $i<$arrSize; $i++) { $this->test[$i]= $arrSize % $i; } return $this->test; } } class BenchmarkArrayFlip extends AbstractBenchmark { public function execute() { $test = array_flip(array_flip($this->test)); } } class BenchmarkArrayUnique extends AbstractBenchmark { public function execute() { $test = array_unique($this->test); } } // Create a new benchmark instance $phpench = new \mre\PHPench(new \mre\PHPench\Aggregator\MedianAggregator); // Use GnuPlot for output $oOutput = new \mre\PHPench\Output\GnuPlotOutput('test2.png', 1024, 768); // Alternatively, print the values to the terminal //$oOutput = new \mre\PHPench\Output\CliOutput(); $oOutput->setTitle('Compare array_flip and array_unique'); $phpench->setOutput($oOutput); // Add your test to the instance $phpench->addBenchmark(new BenchmarkArrayFlip, 'array_flip'); $phpench->addBenchmark(new BenchmarkArrayUnique, 'array_unique'); // Run the benchmark and plot the results in realtime. // With the second parameter you can specify // the start, end and step for each call $phpench->setInput(range(1,pow(2,16), 1024)); $phpench->setRepetitions(4); $phpench->run();
安装
1.) 将此包添加到你的 composer.json 中
{
"require": {
"mre/phpench": "*@dev"
}
}
2.) 安装 gnuplot (版本 4.6)
对于 Mac OS X,您可以通过 homebrew 安装 gnuplot。对于实时生成的图表,您还需要安装 XQuartz。
Without X11 support:
$ brew install homebrew/versions/gnuplot4
With X11 supprt (recommended!):
$ brew install homebrew/versions/gnuplot4 --with-x11
对于 Linux,请使用您的包管理器。
apt-get install gnuplot
3.) 查看使用示例
维护者
Matthias Endler (@matthiasendler)
Markus Poerschke (@markuspoerschke)
许可
Apache License 版本 2.0
Francesco Danti
- 重构以支持 php 7.2
- 添加了重复时的控制台排名表(可选)
- 添加了最终的控制台排名表(可选)
- 添加了 OutlinersDecorator 以去除异常值 =
stdDev * magnitude