Testare le performance in Laravel 9 con Benchmark
Dalla versione 9.32 di Laravel è stato introdotto l'oggetto Benchmark, che ci permette di eseguire dei testi di performance sulle nostre funzioni.
E' molto facile da usare, e anche molto comodo.
Qui useremo una funziona molto banale, ma il concetto è uguale.
Ecco il mio controller di test:
namespace App\Http\Controllers;
use Illuminate\Support\Benchmark;
class TestController extends Controller {
public function index() {
echo Benchmark::measure(fn() => $this->test());
}
private function test() {
for ($i = 0; $i < 1000; $i++) {
echo $i . '<br>';
}
}
}
Volendo possiamo anche indicare una serie di funzioni, e anche quante volte ripetere il test:
namespace App\Http\Controllers;
use Illuminate\Support\Benchmark;
class TestController extends Controller {
public function index() {
echo Benchmark::measure([
fn() => $this->test(),
fn() => $this->test2(),
], 2);
}
private function test() {
for ($i = 0; $i < 1000; $i++) {
echo $i . '<br>';
}
}
private function test2() {
for ($i = 0; $i < 2000; $i++) {
echo $i . '<br>';
}
}
}
Enjoy!
php laravel benchmark
Commentami!