christiaanbye/stopwatch
Composer 安装命令:
composer require christiaanbye/stopwatch
包简介
Drop-dead simple execution time measurements
README 文档
README
As the name implies, this is a helper for profiling code in PHP. The library is designed with the following in mind:
- Absolute minimal overhead. The reason you are reading this, is you likely have one or more short tasks to profile. Polluting the results by adding a few hundreds of a second in overhead, won't do anybody good.
- Drop-dead simple API. No decision-making in how the results should be presented or how to name the measuring instance. Just a simple start and stop with maybe a few laps in between. If you require more advanced features, in-code profiling might not be the most suitable approach for your use case.
- Maximum compatibility with older PHP versions. The most of us have had the joy of working with end-of-life PHP versions. Thankfully versioning exists, so one could install an older version of their library of choice. Unfortunately this sometimes does come at the expense of a subtly altered public API. I'd rather remove mental friction than add to it, so you can use the very same library with age-old PHP 5.6 all the way to modern-day PHP 8.
Prerequisites
- PHP 5.4 or later
Installation
Installation can be easily done using Composer:
composer require christiaanbye/stopwatch
Advanced users may advocate to add the --dev flag and rely on their quality gate to detect usages of the undefined Stopwatch class to prevent accidental production use.
Usage
For basic use, you can use the start(), stop() and elapsed() methods:
use Stopwatch\Stopwatch; Stopwatch::start(); // Start the stopwatch // ... run your tasks here echo Stopwatch::elapsed(); // Optionally take a peek at the elapsed time // ... run some more tasks here Stopwatch::stop(); // Stop the stopwatch once your tasks have ran echo Stopwatch::elapsed(); // Output the time elapsed between the moment the stopwatch was started and stopped
The elapsed() method returns a float of one of the following:
- If the stopwatch is still running, the amount of time passed since starting the stopwatch and now
- If the stopwatch has been stopped, the amount of time passed since starting and stopping the stopwatch
The latter can be useful if you wish to log the elapsed time further down execution.
In more advanced use cases, one can also make use of the split() method in conjunction with the getSplits() method. This allows you to record the intermediate time between tasks:
use Stopwatch\Stopwatch; Stopwatch::start(); // ... run a first batch of tasks here Stopwatch::split('1st batch of tasks'); // ... run a second batch of tasks here Stopwatch::split('2nd batch of tasks'); // ... run a third batch of tasks here Stopwatch::split('3rd batch of tasks'); // ... optionally run more tasks for which an intermediate time is not necessary Stopwatch::stop(); print_r(Stopwatch::getSplits()); // Output the time elapsed between the moment the stopwatch was started and stopped
The getSplits() method returns all intermediates and the overall time as an array containing the following:
- The time since the stopwatch was started
- The time between the previous and current intermediate
The result of the above code looks as follows:
Array
(
[1st batch of tasks] => Array
(
[sinceStart] => 0.007193305
)
[2nd batch of tasks] => Array
(
[sinceStart] => 0.012397519
[sincePreviousSplit] => 0.005204214
)
[3rd batch of tasks] => Array
(
[sinceStart] => 0.01354921
[sincePreviousSplit] => 0.001151691
)
[overall] => Array
(
[sinceStart] => 0.01888658
)
)
Similarly to the elapsed() method, the stopwatch can be stopped prior to calling getSplits() so the recorded execution times are not affected by logging late during runtime.
christiaanbye/stopwatch 适用场景与选型建议
christiaanbye/stopwatch 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.02k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 08 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「performance」 「time」 「timer」 「benchmark」 「stopwatch」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 christiaanbye/stopwatch 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 christiaanbye/stopwatch 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 christiaanbye/stopwatch 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
g4 application profiler package
CSS/Javascript Minificator, Compressor and Concatenator for TYPO3 - highly configurable frontend asset optimization for CSS/JS merging, minification and compression with optional body parsing, async/defer loading, inline output, data-ignore exclusions, SRI integrity validation/calculation, external
Native PHP Library to profile PHP code.
Tools to convert between .NET and PHP date formats
WordPress mu-plugin to remove jQuery Migrate from the list of jQuery dependencies and to allow jQuery to enqueue before </body> instead of in the <head>.
Adds the EDTF data type to Wikibase
统计信息
- 总下载量: 4.02k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-08-07