Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Stopwatch Laravel Package

symfony/stopwatch

Symfony Stopwatch is a lightweight profiling utility to measure execution time and memory usage in PHP. Start/stop named events, record laps, and group timings into sections (phases) to benchmark code paths and understand performance bottlenecks.

View on GitHub
Deep Wiki
Context7

Provides a way to profile code

Frequently asked questions about Stopwatch
Can I use Symfony Stopwatch to profile Laravel middleware without slowing down production?
Yes. Wrap middleware logic in `$stopwatch->start('middleware')` and `$stopwatch->stop('middleware')` calls. The overhead is minimal (~1–5% per event), so disable it in production via `app()->environment('local')` checks. For critical paths, consider using it only in staging with a performance budget.
How do I profile Eloquent queries or database operations with Stopwatch?
Hook into Eloquent events like `retrieved`, `saved`, or `deleted` to start/stop Stopwatch events. For raw queries, wrap `DB::select()` or `QueryBuilder` calls in `start()`/`stop()`. Example: `$stopwatch->start('User::find'); $user = User::find(1); $stopwatch->stop('User::find');`
Does Symfony Stopwatch work with Laravel queues or Artisan commands?
Absolutely. Use it in queue jobs by injecting the Stopwatch via the service container or manually instantiating it. For Artisan commands, initialize Stopwatch in the `handle()` method. It’s ideal for profiling CLI-heavy workflows like migrations or batch processing where APMs like Blackfire can’t attach.
What Laravel versions and PHP versions does Stopwatch support?
Stopwatch itself requires PHP 8.1+ (v8.x) or PHP 7.4+ (v7.x). Since Laravel 9+ drops PHP 7.4 support, use the v8.x branch for Laravel 9/10. For older Laravel (8.x), stick with v7.x. No Laravel-specific dependencies mean it works across all supported versions.
How can I enforce Stopwatch usage in performance-critical Laravel code?
Create a base trait like `ProfilableService` with auto-start/stop methods, then extend it in services. Enforce via PR reviews by requiring profiling for paths like checkout or search. Alternatively, add a CI check to fail builds if critical sections exceed thresholds (e.g., using `Stopwatch` data in a custom script).
Is there a way to log Stopwatch data for historical analysis in Laravel?
No built-in persistence, but you can extend it by writing Stopwatch events to Laravel’s log system or a database. Example: `$stopwatch->stop('event'); logger()->info('Profiling', ['event' => $event->getData()]);` For visualization, integrate with Laravel Debugbar or Telescope.
Can I use Stopwatch alongside Blackfire or Xdebug for deeper profiling?
Yes. Stopwatch is lightweight (~1–5% overhead) and ideal for local development or CI, while Blackfire/Xdebug add ~10–20% overhead. Use Stopwatch for broad profiling (e.g., middleware, services) and Blackfire for deep dives into specific bottlenecks. Combine them by disabling Stopwatch in production.
How do I profile nested sections or phases (e.g., job steps) in Laravel?
Use `$stopwatch->openSection('phase_name')` to group events hierarchically. Example: `$stopwatch->openSection('processOrder'); $stopwatch->start('validate'); ... $stopwatch->stop('validate'); $stopwatch->stopSection('processOrder');` This mirrors Laravel’s pipeline stages or job phases, making it easy to analyze execution flow.
What’s the best way to integrate Stopwatch into Laravel’s service container?
Bind Stopwatch to the container in a service provider: `app()->bind(Stopwatch::class, fn() => new Stopwatch());`. Then inject it into controllers, services, or jobs via constructor dependency injection. This centralizes profiling logic and avoids manual instantiation.
Are there alternatives to Stopwatch for Laravel profiling with less manual setup?
For zero-configuration profiling, consider Laravel Debugbar (browser-based) or Blackfire (APM). Debugbar integrates with Stopwatch for visualization, while Blackfire offers deeper insights but higher overhead. If you need lightweight, event-based profiling without APM complexity, Stopwatch is the most flexible choice for Laravel’s architecture.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai