Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
    }
}
like image 239
Eirik A. Johansen Avatar asked Oct 18 '14 06:10

Eirik A. Johansen


2 Answers

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

like image 102
Wouter J Avatar answered Nov 12 '22 11:11

Wouter J


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');
}
like image 1
famas23 Avatar answered Nov 12 '22 10:11

famas23