Laravel Artisan Benchmark: Track Time, Memory & Query Count

While working on Laravel applications, command optimization in Artisan is crucial to maintaining your app’s efficiency and stability. Developers use the Laravel Artisan Benchmark to track the time taken to execute commands, memory usage, and query execution count to monitor and optimize command performance.

Picture executing a sophisticated import or report-generating command in production. Without an estimate of the time it will take and the amount of memory it will use, debugging performance problems is maddening. That’s where Artisan Benchmark in Laravel 12 comes in handy. It provides you with a transparent view of what is going on behind the scenes, making it possible for you to optimize your application’s performance with ease.

Why Use the Laravel Artisan Benchmark Package

Performance bottlenecks tend to escape unnoticed during development. A command might execute happily on a local machine but come to a crawl in production. With the Artisan Benchmark package, you can effortlessly measure execution time, verify memory usage, and observe query counts for any Artisan command.

This transparency enables developers to spot inefficient queries, optimize code logic, and view the performance differences after upgrading code. Developers often use this utility before deployment to spot heavy database queries or memory leaks in long-running jobs.

Installing the Artisan Benchmark Package

To get started, install the Laravel Artisan Benchmark package via Composer:

composer require christophrumpel/artisan-benchmark --dev

Once installed, you can use it right away without any extra setup. Just run your Artisan commands with the benchmark: prefix to start measuring execution time, memory usage, and query count in your Laravel 12 application.

Using the Laravel Artisan Benchmark

The Laravel Artisan Benchmark package makes it simple to test and analyze how efficiently your Artisan commands perform. Once package is installed, you can start benchmarking command performance immediately using a single command.

To benchmark any existing Artisan command, follow below syntax:

php artisan benchmark your:command

Change your artisan command call with, your own command to perform. For example, if you want to check how your database seeder performs, you can run:

php artisan benchmark db:seed

This will display detailed metrics after execution, including execution time, memory usage, and query count similar to below image.

Artisan benchmark command output - codewolfy

If your command has arguments or options, wrap the command signature in quotes to include them correctly:

php artisan benchmark "import:users --source=upload"

You can also run the benchmark command without specifying a command name. In that case, Laravel will show you a list of available commands so you can select one interactively:

php artisan benchmark
Artisan benchmark command list

It will list out all the available commands and once you select command run it will start bench-marking details.

Conclusion

The Laravel Artisan Benchmark is essential for performance- and scalability-mindful developers. It assists you in monitoring Artisan command execution time, memory usage within Artisan commands, and query counts within Laravel command execution. Whether you are optimizing a data migration process, report generator, or any background task, this package provides you with the visibility you require to make better decisions.

If you want to take your Artisan commands a step further, check out our related post “Make Artisan Commands More User Friendly with Laravel Prompts” to learn how to create interactive and user-focused command-line experiences in Laravel.