A simple tool to track execution times of tasks.
Simply install the package via composer and you're good to go!
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();