Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test query execution time in laravel

How to test in laravel/phpunit how long query took to execute?

Is it possible so that it does not depend on something else?

like image 771
changer Avatar asked Jan 19 '17 15:01

changer


People also ask

How to check query execution time in laravel?

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); });

What is query () in laravel?

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.


2 Answers

The oldest way is the best one:

$start = microtime(true);
// Execute the query
$time = microtime(true) - $start;
like image 130
Alexey Mezenin Avatar answered Sep 21 '22 18:09

Alexey Mezenin


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

like image 22
j3py Avatar answered Sep 21 '22 18:09

j3py