have database performance measured in three ways at once, instead of three separate ways

This commit is contained in:
Shish 2019-07-06 21:41:48 +01:00
parent 2935db9d6d
commit 8ea78eed6a

View file

@ -188,46 +188,33 @@ class Database
return $this->engine->name; return $this->engine->name;
} }
private function count_execs(string $sql, array $inputarray): void private function count_time(string $method, float $start, string $query, ?array $args): void
{ {
global $_tracer;
$dur = microtime(true) - $start;
$_tracer->complete($dur, "DB Query", null, ["query"=>$query, "args"=>$args]);
if ((DEBUG_SQL === true) || (is_null(DEBUG_SQL) && @$_GET['DEBUG_SQL'])) { if ((DEBUG_SQL === true) || (is_null(DEBUG_SQL) && @$_GET['DEBUG_SQL'])) {
$sql = trim(preg_replace('/\s+/msi', ' ', $sql)); $query = trim(preg_replace('/\s+/msi', ' ', $query));
if (isset($inputarray) && is_array($inputarray) && !empty($inputarray)) { if (isset($args) && is_array($args) && !empty($args)) {
$text = $sql." -- ".join(", ", $inputarray)."\n"; $text = $query." -- ".join(", ", $args)."\n";
} else { } else {
$text = $sql."\n"; $text = $query."\n";
} }
$text .= "$method:$dur\n";
file_put_contents("data/sql.log", $text, FILE_APPEND); file_put_contents("data/sql.log", $text, FILE_APPEND);
} }
if (!is_array($inputarray)) {
$this->query_count++;
}
# handle 2-dimensional input arrays
elseif (is_array(reset($inputarray))) {
$this->query_count += sizeof($inputarray);
} else {
$this->query_count++;
}
}
private function count_time(string $method, float $start): void $this->query_count++;
{
if ((DEBUG_SQL === true) || (is_null(DEBUG_SQL) && @$_GET['DEBUG_SQL'])) {
$text = $method.":".(microtime(true) - $start)."\n";
file_put_contents("data/sql.log", $text, FILE_APPEND);
}
$this->dbtime += microtime(true) - $start; $this->dbtime += microtime(true) - $start;
} }
public function execute(string $query, array $args=[]): PDOStatement public function execute(string $query, array $args=[]): PDOStatement
{ {
global $_tracer;
$_tracer->begin("DB Query", null, ["query"=>$query]);
try { try {
if (is_null($this->db)) { if (is_null($this->db)) {
$this->connect_db(); $this->connect_db();
} }
$this->count_execs($query, $args);
$stmt = $this->db->prepare( $stmt = $this->db->prepare(
"-- " . str_replace("%2F", "/", urlencode(@$_GET['q'])). "\n" . "-- " . str_replace("%2F", "/", urlencode(@$_GET['q'])). "\n" .
$query $query
@ -248,8 +235,6 @@ class Database
return $stmt; return $stmt;
} catch (PDOException $pdoe) { } catch (PDOException $pdoe) {
throw new SCoreException($pdoe->getMessage()."<p><b>Query:</b> ".$query); throw new SCoreException($pdoe->getMessage()."<p><b>Query:</b> ".$query);
} finally {
$_tracer->end();
} }
} }
@ -260,7 +245,7 @@ class Database
{ {
$_start = microtime(true); $_start = microtime(true);
$data = $this->execute($query, $args)->fetchAll(); $data = $this->execute($query, $args)->fetchAll();
$this->count_time("get_all", $_start); $this->count_time("get_all", $_start, $query, $args);
return $data; return $data;
} }
@ -271,7 +256,7 @@ class Database
{ {
$_start = microtime(true); $_start = microtime(true);
$row = $this->execute($query, $args)->fetch(); $row = $this->execute($query, $args)->fetch();
$this->count_time("get_row", $_start); $this->count_time("get_row", $_start, $query, $args);
return $row ? $row : null; return $row ? $row : null;
} }
@ -286,7 +271,7 @@ class Database
foreach ($stmt as $row) { foreach ($stmt as $row) {
$res[] = $row[0]; $res[] = $row[0];
} }
$this->count_time("get_col", $_start); $this->count_time("get_col", $_start, $query, $args);
return $res; return $res;
} }
@ -301,7 +286,7 @@ class Database
foreach ($stmt as $row) { foreach ($stmt as $row) {
$res[$row[0]] = $row[1]; $res[$row[0]] = $row[1];
} }
$this->count_time("get_pairs", $_start); $this->count_time("get_pairs", $_start, $query, $args);
return $res; return $res;
} }
@ -312,7 +297,7 @@ class Database
{ {
$_start = microtime(true); $_start = microtime(true);
$row = $this->execute($query, $args)->fetch(); $row = $this->execute($query, $args)->fetch();
$this->count_time("get_one", $_start); $this->count_time("get_one", $_start, $query, $args);
return $row[0]; return $row[0];
} }