justlunix / timer
此包的最新版本(v1.0.0)没有可用的许可信息。
跟踪任务执行时间的工具
v1.0.0
2023-10-14 10:02 UTC
Requires
- ausi/slug-generator: ^1.1
- nesbot/carbon: ^2.71
- symfony/cache: ^6.3
This package is auto-updated.
Last update: 2024-09-15 11:21:40 UTC
README
一个简单的跟踪任务执行时间的工具。
安装
只需通过composer安装此包,一切就绪!
composer require justlunix/timer
用法
简单跟踪任务
$task = Timer::task('Fetching Customer Data From API', function() { // fetching... }); echo $task->getTaskData()->getReadableTimeRun(); // => 2sec 15ms
嵌套任务跟踪
$task = Timer::task('Handling Web Request', function(TaskData $taskData) { $apiTask = Timer::task('Fetching Customer Data From API', function() { // fetching... }, parent: $taskData); $resTask = Timer::task('Building Response', function() use ($apiTask) { // building... }, parent: $taskData); return $resTask->result; });
订阅事件
Timer::subscribe( PostTaskExecutedEvent::class, function (TaskData $taskData) { // do something } );
比较任务
$issetTestTask = Timer::task('isset() performance', function () { // Implement performance test }); $arraySearchTestTask = Timer::task('array_search() performance', function () { // Implement performance test }); $taskComparison = Timer::compare($issetTestTask->getTaskData(), $arraySearchTestTask->getTaskData());
缓存任务结果
// Invalidate caches via Timer::$invalidateCaches = true; $task = Timer::task('Fetching Customer Data From API', function() { // fetching... }, cacheUntil: new \DateTime('tomorrow')); $task = Timer::task('Fetching Customer Data From API', function() { // fetching... }); // => Cache hit!
导出统计信息
// Export all tasks at the end with Timer::exportAsFile(__DIR__ . '/timer.txt'); // TODO: or enable live logging into a file Timer::enableLiveLogging(__DIR__ . '/timer.txt'); // TODO: or log to Spatie Ray! Timer::enableRay();