Wednesday, November 21, 2012

How to time your own events in Symfony 2?

As some of you might have noticed by now, I am quite interested in the performance of my application. Since Symfony 2 has such a great profiler I was looking for a way to time my own code within the existing profiler and without writing to much code. After some digging I figured out it is quite easy to do so:
class OrderController extends Controller
{
    public function showAction($id)
    {
        $stopwatch = $this->get('debug.stopwatch');
        $stopwatch->start("eventName");
        // ... code you want to time ...
        $stopwatch->stop("eventName");
    }
}
Please note that the string you give as a paremeter to the start function is the name of your event and you should use exactly the same name when calling the stop function. Also note that this is the way to do it within a controller (but I'm sure you now know how to use it in other classes as well).

No comments:

Post a Comment