如何获得symfony2应用程序执行时间?

时间:2014-01-28 16:50:04

标签: symfony profiler execution-time

我需要在twig或controller中获取symfony2应用程序执行时间,并将其存储在mysql数据库中。我怎么能这样做?它可以在Web Profiler工具栏中找到,但我不知道如何访问它。

1 个答案:

答案 0 :(得分:4)

如果你只考虑控制器的时间, 你可以使用秒表组件:

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

use Symfony\Component\Stopwatch\Stopwatch;

public function indexAction()
{
    $stopwatch = new Stopwatch();
    // Start event named 'eventName'
    $stopwatch->start('eventName');
    // ... some code goes here
    $event = $stopwatch->stop('eventName');
    // get total duration (but see the docs for more info)
    $timeTaken = $event->getDuration();   // Returns the event duration, including all periods

     ...

}
相关问题