PHP code example of justlunix / timer

1. Go to this page and download the library: Download justlunix/timer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

justlunix / timer example snippets


$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();