intellex / stopwatch
Requires
- php: >=5.6
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-12 00:08:36 UTC
README
用于调试慢速脚本和优化框架。
- 简单的 开始 / 停止 计时器
- 支持 嵌套 测量
- 汇总 重复函数的测量
- 纯文本 报告
异常处理
计时器将在任何异常时抛出异常。然而,默认情况下,这些异常被静默忽略。
要设置自定义处理器,请使用
StopwatchException::setExceptionHandler(StopwatchExceptionHandling $handler);
有关异常的完整列表,请参阅 /src/Exception
目录。
API
简单用法
使用方法对 Stopwatch::start(string $name)
和 Stopwatch::stop(string $name)
来测量经过的时间。string $name
用于命名和识别测量值,并且必须在两个方法中匹配。
Stopwatch::stop(string $name)
返回一个 Measurement
实例,该实例包含有关测量的信息。
示例
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
嵌套
嵌套测量允许您获取时间消耗的更多细节,同时也易于了解所花费的总时间。嵌套级别不受限制。
示例
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();
汇总
如果某个函数被多次调用,则测量总时间。
$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
打印摘要
以纯文本形式打印所有测量的摘要:$measurement->report()
。
<pre><?php echo $measurement->report() ?></pre>
从上面的嵌套示例中,将产生
Total: 100 ├───Init: 9 └───Database: 91 ├───Connection: 76 └───Read: 15 └───Cache: 3
待办事项
- 测试。
- 改进打印摘要。
许可
MIT 许可证
版权所有 (c) 2019 Intellex
在此特此免费许可任何获得本软件及其相关文档副本(“软件”)的人,在不限制的情况下处理软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本的权利,以及允许软件提供方执行上述操作,前提是遵守以下条件
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
软件按“原样”提供,不提供任何形式的保证,明示或暗示,包括但不限于适销性、适用于特定目的和无侵权性保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任承担责任,无论这种责任是基于合同、侵权或其他方式,源于、涉及或与软件或其使用或其他交易有关。
鸣谢
脚本由 Intellex 团队编写。