intellex/stopwatch
Composer 安装命令:
composer require intellex/stopwatch
包简介
README 文档
README
Useful for debugging slow scripts and optimizing frameworks.
- Simple start / stop stopwatch
- Supports nested measurements
- Aggregated measurements for repeating functions
- Plain text report
Exception handling
The Stopwatch will throw an exception on any anomaly. However, by default these exceptions are silently ignored.
In order to set a custom handler use:
StopwatchException::setExceptionHandler(StopwatchExceptionHandling $handler);
For complete list of exceptions, see /src/Exception directory.
API
Simple usage
Use the pair of methods Stopwatch::start(string $name) and Stopwatch::stop(string $name) to measure elapsed time.
The string $name is used to name and identify the measurement and it must match across the two methods.
Stopwatch::stop(string $name) returns an instance Measurement, which holds the information about the measurement.
Example:
Stopwatch::start('Init');
// ...
$measurement = Stopwatch::stop('Init'); // Returns an instance of Measurement
// Available options
$measurement->getStart(); // The start time of the measurement, in milliseconds
$measurement->getEnd(); // The end time of the measurement in milliseconds
$measurement->getTime(); // The total time elapsed in milliseconds
Nested
Nested measurements allow you to get more details where the time is spent, but also to easily know the total time spent. Level of nesting is not limited.
Example:
Stopwatch::start('Total');
// ...
Stopwatch::start('Init');
// ...
Stopwatch::stop('Init');
Stopwatch::start('Database');
Stopwatch::start('Connection');
// ...
Stopwatch::stop('Connection');
Stopwatch::start('Read');
// ...
Stopwatch::start('Cache');
// ...
Stopwatch::stop('Cache');
Stopwatch::stop('Read');
Stopwatch::stop('Database');
$measurement = Stopwatch::stop('Total');
// The total time elapsed in 'Total', in milliseconds
$measurement->getTime();
// The list of direct children measurements ('Database' and 'Rendering'), with same API as this measurement
$subMeasurements = $measurement->getChildren();
Aggregated
If a certain function is called multiple times, measure the total time.
$loops = 10;
while ($loops--) {
Stopwatch::startAggregate('loop');
// ...
Stopwatch::pauseAggregate('loop');
}
// Get the instance of AggregateMeasurment
$aggregateMeasurement = Stopwatch::getAggregatedMeasurements()['loop'];
$aggregateMeasurement->getTotal() // The total elapsed time, in milliseconds
$aggregateMeasurement->getCount() // The number of loops
Print summary
Print the summary of all measurements as plain text: $measurement->report().
<pre><?php echo $measurement->report() ?></pre>
From the nested example above, will produce:
Total: 100 ├───Init: 9 └───Database: 91 ├───Connection: 76 └───Read: 15 └───Cache: 3
TODO
- Tests.
- Improve the print summary.
Licence
MIT License
Copyright (c) 2019 Intellex
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Credits
Script has been written by the Intellex team.
intellex/stopwatch 适用场景与选型建议
intellex/stopwatch 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.8k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 03 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 intellex/stopwatch 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 intellex/stopwatch 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.8k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-03-11