Does magento 2 have a built in timer component like Symfony Stopwatch?


Would be useful for console commands / cron tasks.

I'm aware that I can load things in but wondered if something already exists and I try not to reinvent the wheel.

I've had a quick scan of the codebase and can't find anything obvious.



See \Magento\Framework\Profiler and \Magento\Framework\Profiler\Driver\Standard\Stat classes

  • Seems logical. Does raise a couple of questions. Do you have to have profiler enabled and if not have you got a basic usage example if for example I were to use it within a console script? May 31 '20 at 22:49

I ended up using Symfony stopwatch

My console code looks a bit like this



    $stopwatch = new Stopwatch();

    // lengthy process

    $event = $stopwatch->stop('stopwatch');
    $output->writeln((string) $event);
    $output->writeln((string) __("Start : %1", date("d-m-Y H:i:s", (int) ($event->getOrigin() / 1000))));
    $output->writeln((string) __("End : %1", date("d-m-Y H:i:s", (int) (($event->getOrigin() + $event->getEndTime()) / 1000))));
    $output->writeln((string) __("Memory : %1 MiB", $event->getMemory() / 1024 / 1024));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.