• Reading time ~ 3 min
  • 08.10.2022

With the release of Laravel 9.32 yesterday, a benchmarking helper was introduced, which is useful to quickly test the performance of certain parts of your application.

It works by passing a Closure that runs some code you want to benchmark and returns the time it took in ms:

use Illuminate\Support\Benchmark;
 
Benchmark::measure(fn() => Post::find(1));
// Returns time in ms.
// i.e., 0.1ms

Additionally, you can pass an array of Closures and optionally configure how many iterations the closures should run:

// Run each callback three times
Benchmark::measure([
  fn() => Post::find(1),
  fn() => Post::find(5),
], 3);
 
// [0.02, 0.03]
 
// Use keys
Benchmark::measure([
  'Post 1' => fn() => Post::find(1),
  'Post 5' => fn() => Post::find(5),
], 3);
// ['Post 1' => 0.02, 'Post 5' => 0.03]

The Benchmark class has a dd() method which runs the above measurements wrapped with a dd() call, which will output the results to the console or browser and exit.

Benchmark::dd([
  'Post 1' => fn() => Post::find(1),
  'Post 5' => fn() => Post::find(5),
]);

Couple this update with the dd() file/line output, and you have some useful new debugging tools!

To learn more, check out the benchmarking section now available within the helpers documentation.

Comments

No comments yet
Yurij Finiv

Yurij Finiv

Full stack

ABOUT

Professional Fullstack developer with experience in website development and desktop development. I have experience with various tools and technologies such as Bootstrap, Tailwind, HTML5, CSS3, PUG, JS, AlpineJs, jQuery, PHP, MODX, Laravel, NodeJS, Git, Docker, hQuery, Guzzle, Livewire, Blade, Twig, Fenom, and Smarty. I have experience in website development using Symfony, MODX and Laravel. Experie...

More
WORK EXPERIENCE
Contact
Ukraine, Lutsk
+380979856297