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.
You thought that yesterday was solely concerning the new “dd” supply stuff? There’s extra! Starting with yesterday’s @laravelphp launch, you might use the brand new “Benchmark” class to shortly check the efficiency of sure components of your utility. ⏱
📚 https://t.co/7G7B5mJuVx. pic.twitter.com/JeYVk9m5Tm
— nuno maduro 🤌🏻 (@enunomaduro) September 29, 2022
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 Closure
s 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.