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.
StopwatchInterface).composer require symfony/stopwatch) with no breaking changes in recent versions (v7.x/v8.x).$this->app->singleton(Stopwatch::class, function () {
return new Stopwatch();
});
Kernel::handle()) or queue jobs (Illuminate\Queue\Events\JobProcessed).$stopwatch->start('User::find');
User::find($id);
$event = $stopwatch->stop('User::find');
start()/stop() calls, which may resist adoption if developers prefer automated tools (e.g., Blackfire). Mitigate via:
ProfileableTrait for services).$this->logger->info('Stopwatch', ['event' => $event->toArray()]);
config/stopwatch.php)Illuminate\Database\Events\QueryExecuted.Illuminate\Queue\Events\JobProcessed.composer.json and manually instrument 3–5 critical paths (e.g., slow queries, API endpoints).// In a GitHub Action or Laravel Pint/Pint rule
$stopwatch->start('critical_path');
// ... code ...
$event = $stopwatch->stop('critical_path');
if ($event->getDuration() > 500) { // 500ms threshold
throw new \RuntimeException("Performance regression detected!");
}
// app/Traits/Profileable.php
trait Profileable {
protected Stopwatch $stopwatch;
public function __construct(Stopwatch $stopwatch) {
$this->stopwatch = $stopwatch;
}
public function profile(string $name, callable $callback) {
$this->stopwatch->start($name);
$result = $callback();
$event = $this->stopwatch->stop($name);
$this->logEvent($event);
return $result;
}
}
// app/Providers/AppServiceProvider.php
public function boot() {
Stopwatch::get()->listen(function (StopwatchEvent $event) {
\DB::table('stopwatch_events')->insert([
'name' => $event->getName(),
'duration_ms' => $event->getDuration(),
'memory' => memory_get_usage(),
'created_at' => now(),
]);
});
}
/api/search).php artisan queue:work, migrations).DB::select(), Model::find(), or Query Builder calls.InvoiceService::generate()).HandleIncomingRequest, Terminate.Illuminate\Events\Dispatcher overhead.Blade compilation or View::make().StopwatchEvent to include memory usage or tags.How can I help you explore Laravel packages today?