How to profile code through Symfony StopWatch component

1.4k views Asked by At

As per docs says:

The Stopwatch component provides an easy and consistent way to measure execution time of certain parts of code so that you don't constantly have to parse microtime by yourself.

I want to profile my REST API cURL calls and code execution in order to reduce the times but I am not sure how to do this. What I did on my code is put this piece of code:

$stopwatch->start('repsync', 'internal');

// the code goes here

$stopwatch->stop('repsync', 'internal');

But since this is a RESTful API I am not sure how to get profiler results. I was looking at StopWatch and notice getEvent and getSectionEvents but again I am not sure if I should call them directly and past the result to a Monolog logger or there is any other way to achieve this. In a few words: how do I see the profiler results when accessing routes trough a RESTful API? Any advice? Ideas?

1

There are 1 answers

0
Julz On BEST ANSWER

This is how I handle it.

Use twig to render your api's data, in your Controller :

return $this->render('MyApiBundle:Default:debug.html.twig', array('data' => $data));

Then create the 'debug' template in Resources/views/Default/debug.html.twig

{% extends 'base.html.twig' %}

{% block body %}
    Debug page
    {{ dump(data) }}
{% endblock %}

Within your browser, call one of your API GET url. You will access Symfony's profiler and see your data.

For code execution time, you can listen to kernel.request and kernel.terminate events.