symfonysymfony-stopwatch

Symfony StopWatch events not appearing in profiler timeline


I'm trying to get additional timing information into the Symfony Profiler Timeline, but I can't get anything to appear. According to the documentation I've read, it should be as simple as the following example, but this doesn't cause any additional info to appear on the timeline.

Do I need to somehow make the profiler aware of the events I'm starting and stopping?

use Symfony\Component\Stopwatch\Stopwatch;

class DefaultController extends Controller
{
    public function testAction()
    {
        $stopwatch = new Stopwatch();
        $stopwatch->start('testController');

        usleep(1000000);

        $response = new Response(
            '<body>Hi</body>',
            Response::HTTP_OK,
            array('content-type' => 'text/html')
        );

        $event = $stopwatch->stop('testController');

        return $response;
    }
}

Solution

  • Symfony's profiler can't scan to code for all stopwatch instances and put that into the timeline. You have to use the preconfigured stopwatch provided by the profiler instead:

    public function testAction()
    {
        $stopwatch = $this->get('debug.stopwatch');
        $stopwatch->start('testController');
    
        // ...
    
        $event = $stopwatch->stop('testController');
    
        return $response;
    }
    

    However, your controller is already on the timeline...