Friday, April 26, 2024
HomePHPBenchmark Code With Laravel's Latest helper

Benchmark Code With Laravel’s Latest helper


With the discharge of Laravel 9.32 yesterday, a benchmarking helper was launched, which is beneficial to shortly check the efficiency of sure components of your utility.

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

1use IlluminateSupportBenchmark;

2 

3Benchmark::measure(fn() => Put up::discover(1));

4// Returns time in ms.

5// i.e., 0.1ms

Moreover, you’ll be able to go an array of Closures and optionally configure what number of iterations the closures ought to run:

1// Run every callback 3 times

2Benchmark::measure([

3 fn() => Post::find(1),

4 fn() => Post::find(5),

5], 3);

6 

7// [0.02, 0.03]

8 

9// Use keys

10Benchmark::measure([

11 'Post 1' => fn() => Post::find(1),

12 'Post 5' => fn() => Post::find(5),

13], 3);

14// ['Post 1' => 0.02, 'Post 5' => 0.03]

The Benchmark class has a dd() methodology which runs the above measurements wrapped with a dd() name, which can output the outcomes to the console or browser and exit.

1Benchmark::dd([

2 'Post 1' => fn() => Post::find(1),

3 'Post 5' => fn() => Post::find(5),

4]);

Couple this replace with the dd() file/line output, and you’ve got some helpful new debugging instruments!

To study extra, try the benchmarking part now out there throughout the helpers documentation.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments