yes / 计时器
计时器
v1.0.0
2016-01-10 10:23 UTC
Requires
- php: >=5.6.0
Requires (Dev)
- phpunit/phpunit: 5.0.*
- yep/reflection: 1.0.*
This package is auto-updated.
Last update: 2024-09-11 18:50:00 UTC
README
计时器 (文档)
Packagist
计时器可在Packagist.org上找到,只需将依赖项添加到您的composer.json中。
{ "require" : { "yep/stopwatch": "1.*" } }
或运行Composer命令
php composer.phar require yep/stopwatch
用法
计时器允许您测量执行特定代码部分所需的时间。
您可以使用与现实生活中一样的start
、stop
和lap
。
<?php use Yep\Stopwatch\Stopwatch; use Yep\Stopwatch\Formatter; require __DIR__ . '/vendor/autoload.php'; $stopwatch = new Stopwatch(); // Start lap $stopwatch->start('event_name'); // ... some code // Stop latest lap and start new one $stopwatch->lap('event_name'); // ... some code // Stop latest lap $event = $stopwatch->stop('event_name'); // Results are in seconds echo $event->getDuration(); // 0.0014588832855225 echo $event->getAverageDuration(); // 0.00063395500183105 // Result is in bytes echo $event->getMemoryUsage(); // 1310720 // or, when you want read duration or memory usage in a human format :) echo Formatter::formatTime($event->getDuration()); // 1.459 ms echo Formatter::formatTime($event->getAverageDuration()); // 633.955 μs echo Formatter::formatMemory($event->getMemoryUsage()); // 1.250 mb
示例用法
<?php use Yep\Stopwatch\Stopwatch; use Yep\Stopwatch\Formatter; require __DIR__ . '/vendor/autoload.php'; $stopwatch = new Stopwatch(); $stopwatch->start('data'); $data = array_merge( range(rand(0, 100), rand(4000, 5000)), range(rand(0, 100), rand(4000, 5000)) ); $stopwatch->stop('data'); $stopwatch->start('unique'); $unique = array_unique($data); $stopwatch->stop('unique'); $stopwatch->start('flip + keys'); $flip_and_keys = array_keys(array_flip($data)); $stopwatch->stop('flip + keys'); $stopwatch->start('unique in cycle'); for ($i = 0; $i < 1000; $i++) { $unique = array_unique($data); $stopwatch->lap('unique in cycle'); } $stopwatch->stop('unique in cycle'); $stopwatch->start('flip + keys in cycle'); for ($i = 0; $i < 1000; $i++) { $flip_and_keys = array_keys(array_flip($data)); $stopwatch->lap('flip + keys in cycle'); } $stopwatch->stop('flip + keys in cycle'); echo Formatter::formatTime($stopwatch->getEvent('data')->getDuration()); // 1.707 ms echo Formatter::formatMemory($stopwatch->getEvent('data')->getMemoryUsage()); // 1.750 mb echo Formatter::formatTime($stopwatch->getEvent('unique')->getDuration()); // 47.297 ms echo Formatter::formatMemory($stopwatch->getEvent('unique')->getMemoryUsage()); // 1.750 mb echo Formatter::formatTime($stopwatch->getEvent('flip + keys')->getDuration()); // 1.153 ms echo Formatter::formatMemory($stopwatch->getEvent('flip + keys')->getMemoryUsage()); // 2.250 mb echo Formatter::formatTime($stopwatch->getEvent('unique in cycle')->getDuration()); // 48.365 s echo Formatter::formatTime($stopwatch->getEvent('unique in cycle')->getMinDuration()); // 22.173 μs echo Formatter::formatTime($stopwatch->getEvent('unique in cycle')->getMaxDuration()); // 83.298 ms echo Formatter::formatTime($stopwatch->getEvent('unique in cycle')->getAverageDuration()); // 48.302 ms echo Formatter::formatMemory($stopwatch->getEvent('unique in cycle')->getMemoryUsage()); // 2.500 mb echo Formatter::formatTime($stopwatch->getEvent('flip + keys in cycle')->getDuration()); // 1.386 s echo Formatter::formatTime($stopwatch->getEvent('flip + keys in cycle')->getMinDuration()); // 12.159 μs echo Formatter::formatTime($stopwatch->getEvent('flip + keys in cycle')->getMaxDuration()); // 1.878 ms echo Formatter::formatTime($stopwatch->getEvent('flip + keys in cycle')->getAverageDuration()); // 1.375 ms echo Formatter::formatMemory($stopwatch->getEvent('flip + keys in cycle')->getMemoryUsage()); // 3.250 mb
如何获取计时器事件?
Stopwatch\Event
对象是从start()
、stop()
、lap()
和getEvent()
方法返回的。
如何获取最低事件圈速持续时间?
<?php use Yep\Stopwatch\Stopwatch; $stopwatch = new Stopwatch(); // ... $event = $stopwatch->getEvent('event_name'); echo $event->getMinDuration(); // 2.6941299438477E-5
如何获取最高事件圈速持续时间?
<?php use Yep\Stopwatch\Stopwatch; $stopwatch = new Stopwatch(); // ... $event = $stopwatch->getEvent('event_name'); echo $event->getMaxDuration(); // 0.00012302398681641
如何获取事件平均持续时间?
<?php use Yep\Stopwatch\Stopwatch; $stopwatch = new Stopwatch(); // ... $event = $stopwatch->getEvent('event_name'); echo $event->getAverageDuration(); // 0.00063395500183105
如何获取事件内存使用情况?
<?php use Yep\Stopwatch\Stopwatch; $stopwatch = new Stopwatch(); // ... $event = $stopwatch->getEvent('event_name'); echo $event->getMemoryUsage(); // 1835008
如何获取所有事件圈速?
<?php use Yep\Stopwatch\Stopwatch; use Yep\Stopwatch\Formatter; $stopwatch = new Stopwatch(); $stopwatch->start('event_name'); range(0, 100); $stopwatch->lap('event_name'); range(0, 3000); $stopwatch->lap('event_name'); range(0, 1000); $event = $stopwatch->stop('event_name'); foreach ($event->getStoppedLaps() as $i => $lap) { echo "Lap $i = " . Formatter::formatTime($lap->getDuration()) . "\n"; }
将打印类似以下内容
Lap 0 = 41.962 μs
Lap 1 = 364.065 μs
Lap 2 = 70.095 μs
我可以使用多个计时器吗?
是的 :)
试试计时器管理器 ;)
如何?
<?php use Yep\Stopwatch\Manager; use Yep\Stopwatch\Stopwatch; require __DIR__ . '/vendor/autoload.php'; $manager = new Manager(); $manager->addStopwatch(new Stopwatch('kernel')); $manager->addStopwatch(new Stopwatch('controller')); $manager->getStopwatch('kernel')->start('event_name'); $manager->getStopwatch('controller')->start('event_name'); $manager->getStopwatch('controller')->stop('event_name'); $manager->getStopwatch('kernel')->stop('event_name');
或者简单地
<?php use Yep\Stopwatch\Manager; require __DIR__ . '/vendor/autoload.php'; $manager = new Manager(); $manager->getStopwatch('kernel', false)->start('event_name'); $manager->getStopwatch('controller', false)->start('event_name'); $manager->getStopwatch('controller')->stop('event_name'); $manager->getStopwatch('kernel')->stop('event_name');