1

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

https://symfony.com/doc/current/components/stopwatch.html

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.

1

Yes.

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

1
  • 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? – Dominic Xigen May 31 '20 at 22:49
0

I ended up using Symfony stopwatch

My console code looks a bit like this

https://github.com/DominicWatts/Stopwatch

    $output->writeln('<info>Start</info>');

    $stopwatch = new Stopwatch();
    $stopwatch->start('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));
    $output->writeln('<info>Finish</info>');

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.