Symfony Stopwatch
A tiny PHP component for profiling code by timing named events and measuring memory usage
Repository Health
Technical Analysis
The Symfony Stopwatch component provides a simple, dependency-light way to profile PHP code. You start and stop named events, and the stopwatch records elapsed time and memory consumption for each, letting you measure how long individual operations or phases of execution take.
Events can be grouped into sections and given categories, periods can be lapped, and the collected data is exposed as StopwatchEvent and StopwatchPeriod objects with precise timing and memory figures. It powers the timeline profiler in the Symfony web debug toolbar but works equally well as a standalone micro-profiler in any PHP application.
What You Get
- Named start/stop timing of code with elapsed-time measurement
- Per-event memory-usage tracking alongside timing
- Grouping of events into sections and categories
- Lap support to record multiple periods within a single event
StopwatchEventandStopwatchPeriodobjects exposing precise timing data
Common Use Cases
- Profiling how long specific operations take during development
- Measuring memory usage of a block of code
- Building a timeline of execution phases in an application
- Powering custom performance dashboards or debug output
Under The Hood
Architecture - The component is intentionally small: Stopwatch (Stopwatch.php) is the entry point managing a stack of Section objects. Each start() creates or resumes a StopwatchEvent (StopwatchEvent.php) which owns a list of StopwatchPeriod records (StopwatchPeriod.php) capturing start time, end time, and memory usage. Sections let events be grouped and nested to represent execution phases, and the API returns event objects so callers can read durations and memory figures directly.
Tech Stack - Pure PHP (>=8.4.1) with a single runtime dependency on symfony/service-contracts. It is packaged as a Symfony component with PSR-4 autoloading under Symfony\Component\Stopwatch and has no other external requirements.
Code Quality - Being four core classes, the component is easy to reason about and thoroughly covered by a PHPUnit suite under Tests/. It follows Symfony’s coding standards and is continuously tested as part of the symfony/symfony monorepo (this repo is a read-only subtree split).
API Design - The API is famously ergonomic: start('name'), run code, stop('name'), then read getDuration() / getMemory() off the returned event. There is almost no boilerplate, which is why it is the profiling primitive behind Symfony’s own debug timeline.
Used by 2 apps in this directory
Kimai
Invoicing Finance · Project Management
Professional open-source time tracking with invoicing, multi-user support, SAML/LDAP auth, and a full REST API—self-host it or use the cloud.
Mautic
Automation · Marketing · Ecommerce
The world's largest open source marketing automation platform — own your data, run multi-channel campaigns, and escape vendor lock-in forever.