Symfony StopWatch events not appearing in profiler timeline

4.9k views Asked by At

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;
    }
}
2

There are 2 answers

1
Wouter J On BEST ANSWER

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...

0
famas23 On

You should inject the stopwacth as a service in your constructor or a specific function, becasue after Symfony 3.4: Services are private by default.

testAction(\Symfony\Component\Stopwatch\Stopwatch $stopwatch) {
    $stopwatch->start('testController');

    // ...

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