Over a million developers have joined DZone.

The Wheel: Symfony Stopwatch

DZone 's Guide to

The Wheel: Symfony Stopwatch

· Web Dev Zone ·
Free Resource

It's impossible to predict performance and you need the right tooling to measure it. The Stopwatch Symfony Component is a userland object that lets you time critical section of code to get some data about their execution, even directly in the production environment.

The previous episodes of The Wheel:


A Stopwatch is an object that measures time, and that you can start and stop at will to focus the measuremente only on interesting parts of the code. Basically, the Stopwatch is a form of automated logging that marks the start and stop of a section with timestamps, calculating the difference between them.

Here is a base test from the suite of the component:

  $stopwatch = new Stopwatch();
  $stopwatch->start('foo', 'cat');
  $event = $stopwatch->stop('foo');

  $this->assertInstanceof('Symfony\Component\Stopwatch\StopwatchEvent', $event);
  $total = $event->getDuration(); // about 20

The Stopwatch can also divide the measurement into sections, so that you only need one Stopwatch object even for multiple measurements:

  $stopwatch->start('foo', 'cat');
  $stopwatch->start('bar', 'cat');

Since typically stopwatches are objects that are introduced into the code when there is a performance problem and discarded thereafter, I have no problem into putting a Stopwatch instance in a global or static variable, and log its results at the end of the process. Having a single instance that can work with multiple intervals simplifies this process.

The pros

The functionality of the Stopwatch is very basic, but lets you profile your code tentatively in production, where you usually cannot install Xdebug or other tools that produce a cachegrind result due to their weight.

The Stopwatch is only a composer.json line away, and we're talking about 4 classes in total: its installation into your project shouldn't raise concerns. We have to resist the urge to code up a Stopwatch class ourselves when the need for it manifests. :)

The cons

The only problem I see with the Symfony Stopwatch is its limited functionality: you'll have to build a new Stopwatch or extend this one (or another library) to get some other feature, such as the ability to see a section as a single event. It's mostly useful in loops:

foreach ($bigArray as $i => $value) {
  // stuff
  $stopwatch->start($i, 'description');
  // critical section
  // other stuff

The API exposes the events as separate object, so currently you have to sum them up yourself. I'm not sure the vision of this component would accomodate these extensions, but we can always make a pull request.

The Stopwatch also do not expose time measurements with a microsecond-based precision, but you probably shouldn't use PHP if you're needing this fine tuning for your code.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}