How to test in laravel/phpunit how long query took to execute?
Is it possible so that it does not depend on something else?
You could listen to executing query like this and log the result in storage/logs/laravel. log . \DB::listen(function ($sql, $bindings, $time) { \Log::info($sql, $bindings, $time); });
In Laravel the database query builder provides an easy interface to create and run database queries. It can be used to perform all the database operations in your application, from basic DB Connection, CRUD, Aggregates, etc. and it works on all supported database systems like a champ.
The oldest way is the best one:
$start = microtime(true);
// Execute the query
$time = microtime(true) - $start;
Since Laravel 5.2 listen
has changed to only accept one argument:
\DB::listen(function ($query) {
// $query->sql
// $query->bindings
// $query->time
});
Docs: https://laravel.com/docs/5.2/database
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With