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