Symfony Stopwatch

A tiny PHP component for profiling code by timing named events and measuring memory usage

Library
Composer
vv8.1.0
2,811stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
74/100Good
Development Activity72
Maintenance72
Community52
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
83/100Excellent
Architecture82
Code Quality88
Innovation70
Learning Curve92

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
  • StopwatchEvent and StopwatchPeriod objects 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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search