Merge pull request #17276 from kamil-tekiela/microtime-sql

Expose execution time as public property
This commit is contained in:
Maurício Meneghini Fauth 2022-01-19 12:13:12 -03:00 committed by GitHub
commit c03cbdea88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 45 deletions

View File

@ -139,6 +139,9 @@ class DatabaseInterface implements DbalInterface
/** @var Cache */
private $cache;
/** @var float */
public $lastQueryExecutionTime = 0;
/**
* @param DbiExtension $ext Object to be used for database queries
*/
@ -210,10 +213,7 @@ class DatabaseInterface implements DbalInterface
return false;
}
$time = 0;
if ($debug) {
$time = microtime(true);
}
$time = microtime(true);
$result = $this->extension->realQuery($query, $this->links[$link], $options);
@ -221,14 +221,14 @@ class DatabaseInterface implements DbalInterface
$GLOBALS['cached_affected_rows'] = $this->affectedRows($link, false);
}
$this->lastQueryExecutionTime = microtime(true) - $time;
if ($debug) {
$time = microtime(true) - $time;
$errorMessage = $this->getError($link);
Utilities::debugLogQueryIntoSession(
$query,
$errorMessage !== '' ? $errorMessage : null,
$result,
$time
$this->lastQueryExecutionTime
);
if ($GLOBALS['cfg']['DBG']['sqllog']) {
$warningsCount = 0;
@ -244,7 +244,7 @@ class DatabaseInterface implements DbalInterface
'SQL[%s?route=%s]: %0.3f(W:%d,C:%s,L:0x%02X) > %s',
basename($_SERVER['SCRIPT_NAME']),
Routing::getCurrentRoute(),
$time,
$this->lastQueryExecutionTime,
$warningsCount,
$cache_affected_rows ? 'y' : 'n',
$link,

View File

@ -22,7 +22,6 @@ use PhpMyAdmin\Utils\ForeignKey;
use function __;
use function array_keys;
use function array_map;
use function array_sum;
use function bin2hex;
use function ceil;
use function count;
@ -33,7 +32,6 @@ use function in_array;
use function is_array;
use function is_bool;
use function is_object;
use function microtime;
use function session_start;
use function session_write_close;
use function sprintf;
@ -605,38 +603,6 @@ class Sql
$bookmark->save();
}
/**
* Executes the SQL query and measures its execution time
*
* @param string $fullSqlQuery the full sql query
*
* @return array ($result, $querytime)
* @psalm-return array{ResultInterface|false, int|float}
*/
private function executeQueryAndMeasureTime($fullSqlQuery)
{
if (! defined('TESTSUITE')) {
// close session in case the query takes too long
session_write_close();
}
// Measure query time.
$queryTimeBefore = array_sum(explode(' ', microtime()));
$result = $this->dbi->tryQuery($fullSqlQuery);
$queryTimeAfter = array_sum(explode(' ', microtime()));
if (! defined('TESTSUITE')) {
// reopen session
session_start();
}
return [
$result,
$queryTimeAfter - $queryTimeBefore,
];
}
/**
* Function to get the affected or changed number of rows after executing a query
*
@ -842,10 +808,18 @@ class Sql
} else { // If we don't ask to see the php code
Profiling::enable($this->dbi);
[
$result,
$GLOBALS['querytime'],
] = $this->executeQueryAndMeasureTime($fullSqlQuery);
if (! defined('TESTSUITE')) {
// close session in case the query takes too long
session_write_close();
}
$result = $this->dbi->tryQuery($fullSqlQuery);
$GLOBALS['querytime'] = $this->dbi->lastQueryExecutionTime;
if (! defined('TESTSUITE')) {
// reopen session
session_start();
}
// Displays an error message if required and stop parsing the script
$error = $this->dbi->getError();