Test query execution time in laravel
26,645
Solution 1
The oldest way is the best one:
$start = microtime(true);
// Execute the query
$time = microtime(true) - $start;
Solution 2
Since Laravel 5.2 listen
has changed to only except one argument:
\DB::listen(function ($query) {
// $query->sql
// $query->bindings
// $query->time
});
Docs: https://laravel.com/docs/5.2/database
Solution 3
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);
});
You could just use $time
only, but I logged $sql, $bindings, $time
for the completion.
You could put this inside your AppServiceProvider
.
UPDATE:
In Laravel version > 5.5 this approach is documented in the doc as listening for query events ;)
Solution 4
You can use ddd($someVar)
helper starting from Laravel 6.x.
There is Queries
tab which describes each query executed until ddd()
Author by
changer
Updated on November 24, 2020Comments
-
changer over 3 years
How to test in laravel/phpunit how long query took to execute?
Is it possible so that it does not depend on something else?