The Stopwatch Component
The Stopwatch component provides a way to profile code.
Installation
- $ composer require symfony/stopwatch
Note
If you install this component outside of a Symfony application, you mustrequire the vendor/autoload.php
file in your code to enable the classautoloading mechanism provided by Composer. Readthis article for more details.
Usage
The Stopwatch component provides a consistent way to measure executiontime of certain parts of code so that you don't constantly have to parsemicrotime by yourself. Instead, use theStopwatch
class:
- use Symfony\Component\Stopwatch\Stopwatch;
- $stopwatch = new Stopwatch();
- // starts event named 'eventName'
- $stopwatch->start('eventName');
- // ... some code goes here
- $event = $stopwatch->stop('eventName');
The StopwatchEvent
object can be retrievedfrom the start()
,stop()
,lap()
andgetEvent()
methods.The latter should be used when you need to retrieve the duration of an eventwhile it is still running.
Tip
By default, the stopwatch truncates any sub-millisecond time measure to 0
,so you can't measure microseconds or nanoseconds. If you need more precision,pass true
to the Stopwatch
class constructor to enable full precision:
- $stopwatch = new Stopwatch(true);
The stopwatch can be reset to its original state at any given time with thereset()
method, which deletesall the data measured so far.
You can also provide a category name to an event:
- $stopwatch->start('eventName', 'categoryName');
You can consider categories as a way of tagging events. For example, theSymfony Profiler tool uses categories to nicely color-code different events.
Tip
Read this article to learn more aboutintegrating the Stopwatch component into the Symfony profiler.
Periods
As you know from the real world, all stopwatches come with two buttons:one to start and stop the stopwatch, and another to measure the lap time.This is exactly what the lap()
method does:
- $stopwatch = new Stopwatch();
- // starts event named 'foo'
- $stopwatch->start('foo');
- // ... some code goes here
- $stopwatch->lap('foo');
- // ... some code goes here
- $stopwatch->lap('foo');
- // ... some other code goes here
- $event = $stopwatch->stop('foo');
Lap information is stored as "periods" within the event. To get lap informationcall:
- $event->getPeriods();
In addition to periods, you can get other useful information from the event object.For example:
- $event->getCategory(); // returns the category the event was started in
- $event->getOrigin(); // returns the event start time in milliseconds
- $event->ensureStopped(); // stops all periods not already stopped
- $event->getStartTime(); // returns the start time of the very first period
- $event->getEndTime(); // returns the end time of the very last period
- $event->getDuration(); // returns the event duration, including all periods
- $event->getMemory(); // returns the max memory usage of all periods
Sections
Sections are a way to logically split the timeline into groups. You can seehow Symfony uses sections to nicely visualize the framework lifecycle in theSymfony Profiler tool. Here is a basic usage example using sections:
- $stopwatch = new Stopwatch();
- $stopwatch->openSection();
- $stopwatch->start('parsing_config_file', 'filesystem_operations');
- $stopwatch->stopSection('routing');
- $events = $stopwatch->getSectionEvents('routing');
You can reopen a closed section by calling the openSection()
method and specifying the id of the section to be reopened:
- $stopwatch->openSection('routing');
- $stopwatch->start('building_config_tree');
- $stopwatch->stopSection('routing');