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
:
1use Illuminate\Support\Benchmark;2 3Benchmark::measure(fn() => Post::find(1));4// Returns time in ms.5// i.e., 0.1ms
Additionally, you can pass an array of Closure
s and optionally configure how many iterations the closures should run:
1// Run each callback three times 2Benchmark::measure([ 3 fn() => Post::find(1), 4 fn() => Post::find(5), 5], 3); 6 7// [0.02, 0.03] 8 9// Use keys10Benchmark::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()
method which runs the above measurements wrapped with a dd()
call, which will output the results 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 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.
0 comments:
Post a Comment
Thanks