Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by SensioLabs
  1. Home
  2. Documentation
  3. Components
  4. The Stopwatch Component
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud

Table of Contents

  • Installation
  • Usage
  • Periods
  • Sections

The Stopwatch Component

Edit this page

Warning: You are browsing the documentation for Symfony 2.2, which is no longer maintained.

Read the updated version of this page for Symfony 6.3 (the current stable version).

The Stopwatch Component

Stopwatch component provides a way to profile code.

2.2

The Stopwatch component is new to Symfony 2.2. Previously, the Stopwatch class was located in the HttpKernel component (and was new in 2.1).

Installation

You can install the component in two different ways:

  • Install it via Composer (symfony/stopwatch on Packagist);
  • Use the official Git repository (https://github.com/symfony/Stopwatch).

Usage

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 Stopwatch class:

1
2
3
4
5
6
7
use Symfony\Component\Stopwatch\Stopwatch;

$stopwatch = new Stopwatch();
// Start event named 'eventName'
$stopwatch->start('eventName');
// ... some code goes here
$event = $stopwatch->stop('eventName');

You can also provide a category name to an event:

1
$stopwatch->start('eventName', 'categoryName');

You can consider categories as a way of tagging events. For example, the Symfony Profiler tool uses categories to nicely color-code different events.

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:

1
2
3
4
5
6
7
8
9
$stopwatch = new Stopwatch();
// Start 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:

1
$event->getPeriods();

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

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 openSection() method and specifying the id of the section to be reopened:

1
2
3
$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.
TOC
    Version
    We stand with Ukraine.
    Version:
    Show your Sylius expertise

    Show your Sylius expertise

    Peruse our complete Symfony & PHP solutions catalog for your web development needs.

    Peruse our complete Symfony & PHP solutions catalog for your web development needs.

    Symfony footer

    ↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

    Avatar of Adam Mikolaj, a Symfony contributor

    Thanks Adam Mikolaj (@mausino) for being a Symfony contributor

    1 commit • 17 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • Symfony at a Glance
      • Symfony Components
      • Case Studies
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • SymfonyConnect
      • Support
      • How to be Involved
      • Code of Conduct
      • Events & Meetups
      • Projects using Symfony
      • Downloads Stats
      • Contributors
      • Backers
    • Blog

      • Events & Meetups
      • A week of symfony
      • Case studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Documentation
      • Living on the edge
      • Releases
      • Security Advisories
      • SymfonyInsight
      • Twig
      • SensioLabs
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Deployed on

    Follow Symfony

    Search by Meilisearch