1

I need to get symfony2 application execution time in twig or controller, and to store it in mysql database. How I can do this? It is available in web profiler toolbar, but I don't know how to access it.

6
  • Do you need it in dev and prod environments? Since the toolbar is only present in dev environment, you'll have to find another way. Commented Jan 28, 2014 at 18:48
  • 1
    I need it in prod environment. I think it is possible, because if I create log in form to enable user log in, later I can from twig template access logged in user with app.user.username. Commented Jan 28, 2014 at 19:48
  • 1
    Create a listener for kernel.request and kernel.terminate events, storing the startime and then updating it with the endtime. Commented Jan 28, 2014 at 21:06
  • 1
    any example how to do that? Commented Jan 28, 2014 at 21:25
  • Solved it with microtime() function. At the beginning of the controller I put starttime and at the end endtime. I did not count the time spent on display in twig, but on the basis of previous tests error is of the order 0.5 - 1s Commented Feb 3, 2014 at 15:42

1 Answer 1

4

If you're only accounting for the time the controller takes, you can use the Stopwatch component:

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

     ...

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.