- How do I install symfony/stopwatch in a Laravel application?
- Run composer require symfony/stopwatch in the Laravel project. Laravel 11 and 12 often already include Symfony components transitively, so run composer why symfony/stopwatch before adding a direct constraint. Pin a version that matches the Symfony major already used by the framework.
- Does Symfony Stopwatch support Laravel 11 and 12?
- Yes. Stopwatch is a framework-agnostic Symfony component and Laravel 11 or 12 can use it without Laravel-specific integration. Those Laravel versions already pull Symfony components, so the package may already be in vendor/ transitively. PHP 8.2 or newer is required for Stopwatch 7.x, while 8.4 or newer is required for 8.x, so check your runtime before pinning a version.
- How do I profile a Laravel controller or Artisan command with Stopwatch?
- Create a Stopwatch instance, call start with a descriptive event name, run the code you want to measure, then call stop and read getEvent. The event exposes duration and memory data that you can log or assert against. Use unique event names for each controller action or command path so the output stays joinable.
- What is the difference between start/stop, lap, and sections in Stopwatch?
- start and stop measure a named event and return a StopwatchEvent. lap records a new period inside a running event, which is useful for repeated steps but can grow memory if used inside very large loops. Sections group related events with openSection and stopSection, and getSectionEvents returns the grouped timings.
- Can I use Symfony Stopwatch in production Laravel applications?
- Yes, but keep it narrow and intentional. It is best for sampled, opt-in instrumentation behind a flag or for dev, staging, and pre-deploy checks rather than an APM substitute. Do not instrument every path or add lap calls in high-volume loops without a threshold or ownership plan.
- How should I use Stopwatch with Laravel Octane and queue workers?
- Stopwatch is in-memory and request-scoped by design, so a shared instance can leak stale events in long-lived workers. Use a container-scoped instance that is reset for each request or job, or create a new Stopwatch per unit of work. This matters for Octane, Swoole, RoadRunner, and queue daemons.
- Why does Stopwatch getDuration return 0 for fast Laravel code?
- getDuration returns integer milliseconds, so sub-millisecond work floors to zero. That is acceptable for endpoint, query, and job profiling but disqualifying for micro-benchmarks. Use a higher-resolution timer if you need to measure very small code paths.
- Why does Stopwatch throw LogicException when I start an event twice?
- Calling start on an event that is already running throws a LogicException. This commonly appears with re-entrant code, double-applied middleware, or nested service calls. Guard the instrumentation, use lap for periods, or stop the event before starting it again with a distinct name.
- Does Symfony Stopwatch export or log profiling data automatically?
- No. Stopwatch produces timing data but has no reporter, sink, log channel, or serialization contract. Teams typically write a thin wrapper to format events and send them to logs, storage, or a dashboard. Without that wrapper, the data stays in memory and disappears at the end of the request.
- Is Symfony Stopwatch a replacement for Laravel Telescope or Debugbar?
- No, it complements them. Telescope and Debugbar provide broader debugging, storage, and UI features, while Stopwatch is a lightweight manual timer with no built-in reporting. Use Stopwatch for targeted timing and repeatable performance gates, not as a full APM or observability replacement.