The Stopwatch Component
The Stopwatch Component¶
The Stopwatch component provides a way to profile code.
$ composer require symfony/stopwatch
Alternatively, you can clone the https://github.com/symfony/stopwatch repository.
If you install this component outside of a Symfony application, you must
vendor/autoload.php file in your code to enable the class
autoloading mechanism provided by Composer. Read
this article for more details.
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. Instead, use the simple
1 2 3 4 5 6 7
use Symfony\Component\Stopwatch\Stopwatch; $stopwatch = new Stopwatch(); // starts event named 'eventName' $stopwatch->start('eventName'); // ... some code goes here $event = $stopwatch->stop('eventName');
StopwatchEvent object can be retrieved
The latter should be used when you need to retrieve the duration of an event
while it is still running.
You can also provide a category name to an event:
You can consider categories as a way of tagging events. For example, the Symfony Profiler tool uses categories to nicely color-code different events.
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
1 2 3 4 5 6 7 8 9
$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 information call:
In addition to periods, you can get other useful information from the event object. For example:
1 2 3 4 5 6 7
$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 are a way to logically split the timeline into groups. You can see how Symfony uses sections to nicely visualize the framework lifecycle in the Symfony Profiler tool. Here is a basic usage example using sections:
1 2 3 4 5 6 7
$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
method and specifying the id of the section to be reopened:
$stopwatch->openSection('routing'); $stopwatch->start('building_config_tree'); $stopwatch->stopSection('routing');
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.