DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How To Become a Symfony Certified Engineer: Your Path to Expertise in the Software Industry
  • Symfony vs Laravel: Why Symfony Is Better Than Laravel

Trending

  • Idempotency Under Concurrency: Why Your Application Check Isn't the Last Line of Defense
  • How to Migrate From C/C++ to Rust: A Practical Guide for Embedded IoT Systems
  • PII Detection Before RAG Ingestion: Screening Documents in C#
  • Build a Voice Assistant With Gemini Live API

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Frameworks
  4. The Wheel: Symfony Stopwatch

The Wheel: Symfony Stopwatch

By 
Giorgio Sironi
·
May. 01, 13 · Interview
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
10.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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:

  • Symfony Console
  • Symfony Filesystem

The API

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');
  usleep(20000);
  $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->openSection();
  $stopwatch->start('foo', 'cat');
  $stopwatch->stop('foo');
  $stopwatch->start('bar', 'cat');
  $stopwatch->stop('bar');
  $stopwatch->stopSection('1');

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->openSection('critical');
  $stopwatch->start($i, 'description');
  // critical section
  $stopwatch->stopSection('critical');
  // 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.

Symfony

Opinions expressed by DZone contributors are their own.

Related

  • How To Become a Symfony Certified Engineer: Your Path to Expertise in the Software Industry
  • Symfony vs Laravel: Why Symfony Is Better Than Laravel

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook