Search code examples
symfonysymfony-3.4

How to add custom timers to the performance debug page?


On the performance page we see various metrics.

But it's only so useful when the grand majority of your work exists through the controller itself, and likely where you (the developer) are going to introduce bottle necks.

enter image description here

Is there a way to inject more custom timers into this page?

For example, this particular controller is unzipping and processing a zip archive. I'd like to know visually how much of the controller execution length was taken to extract the archive, and how much was taken to process it.


Solution

  • If your zip controller extends Controller try:

    use \Symfony\Bundle\FrameworkBundle\Controller\Controller;
    
    class zipController extends Controller
    {
        public function index(Request $request)
        {
            $debugstopwatch = $this->get('debug.stopwatch');
            $debugstopwatch->start('zipController');
    
    
            /* ..  */
    
            $debugstopwatch->stop('zipController');
    
            /*render page*/
    
        }
    }
    

    Or inject \Symfony\Component\Stopwatch\Stopwatch

    public function index(Request $request, \Symfony\Component\Stopwatch\Stopwatch $debugstopwatch)
    {
        $debugstopwatch->start('zipController');
    
        /* ..  */
    
        $debugstopwatch->stop('zipController');
    
    
    
    }
    

    See Stopwatch