To use Symfony’s Stopwatch component to measure performance, you can follow these steps:
composer require symfony/stopwatch
use Symfony\Component\Stopwatch\Stopwatch;
$stopwatch = new Stopwatch();
$stopwatch->start('task_name');
Perform the code or task you want to measure the performance of.
Stop the stopwatch to end measuring time:
$event = $stopwatch->stop('task_name');
$duration = $event->getDuration(); // in milliseconds
echo 'Task duration: '.$duration.'ms';
$periods = $event->getPeriods();
foreach ($periods as $index => $period) {
echo 'Period '.$index.': '.$period->getDuration().'ms<br>';
}
By following these steps, you can effectively use Symfony’s Stopwatch component to measure the performance of your code and tasks.