Use Laravel's Benchmark helper to test and compare execution times of complex queries, algorithms, and closures.
When optimizing database queries, comparing caching strategies, or evaluating algorithm performance, calculating execution time with microtime(true) math is cumbersome.
Laravel provides the Illuminate\Support\Benchmark class to measure runtime durations.
Measuring a Single Operation
use App\Models\User;
use Illuminate\Support\Benchmark;
// Measures execution time in milliseconds (e.g. "4.12ms")
$time = Benchmark::measure(function () {
return User::with('orders.items')->get();
});
logger()->info("Query took {$time}ms to complete.");
Comparing Multiple Approaches with Benchmark::dd()
Pass an associative array of closures to Benchmark::dd() to compare competing implementations and inspect the results in the browser or terminal:
use App\Models\User;
use Illuminate\Support\Benchmark;
Benchmark::dd([
'Eager Loading' => fn () => User::with('posts')->get(),
'Lazy Loading' => fn () => User::all()->each->posts,
'Raw DB Query' => fn () => DB::table('users')->join('posts', 'users.id', '=', 'posts.user_id')->get(),
], iterations: 10);
Output Example
array:3 [
"Eager Loading" => "3.45ms"
"Lazy Loading" => "18.92ms"
"Raw DB Query" => "1.21ms"
]
Summary
- Measures execution duration in milliseconds.
iterationsparameter runs the closures multiple times and computes the average duration.- Ideal for comparing query efficiency and cache optimizations.
Related Tips
View all tips →Eliminate Cache Stampedes with Cache::flexible() and Stale-While-Revalidate
Use Cache::flexible() to serve slightly stale cache values immediately to users while refreshing the cache in the background.
Attach Global Request Metadata to Logs with Log::withContext()
Use Log::withContext() in HTTP middleware to automatically attach request IDs, user IDs, and tenant metadata to every log entry written during a request.