yep/stopwatch
Composer 安装命令:
composer require yep/stopwatch
包简介
Stopwatch
关键字:
README 文档
README
Stopwatch (docs)
Packagist
Stopwatch are available on Packagist.org, just add the dependency to your composer.json.
{
"require" : {
"yep/stopwatch": "1.*"
}
}
or run Composer command:
php composer.phar require yep/stopwatch
Usage
Stopwatch allows you to measure how long it takes to execute a certain parts of code.
You can use start, stop and lap like in real world.
<?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
Example usage
<?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
How can I get the Stopwatch Event?
The Stopwatch\Event object is returned from the start(), stop(), lap() and getEvent() methods.
How can I get the lowest Event lap duration?
<?php use Yep\Stopwatch\Stopwatch; $stopwatch = new Stopwatch(); // ... $event = $stopwatch->getEvent('event_name'); echo $event->getMinDuration(); // 2.6941299438477E-5
How can I get the highest Event lap duration?
<?php use Yep\Stopwatch\Stopwatch; $stopwatch = new Stopwatch(); // ... $event = $stopwatch->getEvent('event_name'); echo $event->getMaxDuration(); // 0.00012302398681641
How can I get the average Event duration?
<?php use Yep\Stopwatch\Stopwatch; $stopwatch = new Stopwatch(); // ... $event = $stopwatch->getEvent('event_name'); echo $event->getAverageDuration(); // 0.00063395500183105
How can I get the Event memory usage?
<?php use Yep\Stopwatch\Stopwatch; $stopwatch = new Stopwatch(); // ... $event = $stopwatch->getEvent('event_name'); echo $event->getMemoryUsage(); // 1835008
How can I get all Event laps?
<?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"; }
will print something like this:
Lap 0 = 41.962 μs
Lap 1 = 364.065 μs
Lap 2 = 70.095 μs
Can I work with multiple stopwatches?
Yes :)
Try stopwatch manager ;)
How?
<?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');
or simply
<?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');
yep/stopwatch 适用场景与选型建议
yep/stopwatch 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 68 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 01 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「stopwatch」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yep/stopwatch 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yep/stopwatch 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yep/stopwatch 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A lightweight benchmark timer and lap profiler for PHP.
Yii2 widget that displays controllable timer/stopwatch
Elapsed time measurement
Drop-dead simple execution time measurements
Provides a Symfony Stopwatch decorator for file fetchers
Just a small stopwatch
统计信息
- 总下载量: 68
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2016-01-09