From 08e4128ddce9dcffe0a4224ae57ad664609935b2 Mon Sep 17 00:00:00 2001 From: Jonas Kohl Date: Sun, 22 Dec 2024 13:06:42 +0100 Subject: Add statistics --- src/application/appdef.php | 2 +- src/application/messages/de.msg | 6 +++++ src/application/mystic/forum/Database.php | 38 ++++++++++++++++++++++--------- src/application/templates/old/base.twig | 8 +++++++ src/index.php | 3 +++ 5 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/application/appdef.php b/src/application/appdef.php index b08bf14..6c69ee8 100644 --- a/src/application/appdef.php +++ b/src/application/appdef.php @@ -1,3 +1,3 @@ queryCount; + } + + protected function queryParams(string $query, array $params): Result|false { + ++$this->queryCount; + return \pg_query_params ($this->connection, $query, $params); + } + + protected function query(string $query): Result|false { + ++$this->queryCount; + return \pg_query ($this->connection, $query); + } + public static function generateId(int $length = 64): string { static $charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; static $charsetLength = strlen($charset); @@ -323,7 +339,7 @@ class Database { foreach (self::getColumnValues($entity, $cols) as $i => $value) $values[$i] = $value; $query = "INSERT INTO $tableName VALUES (" . implode(",", ArrayUtils::fill(fn($i) => "$" . ($i + 1), count($cols))) . ");"; - $result = \pg_query_params($this->connection, $query, $values); + $result = $this->queryParams($query, $values); if ($result === false) throw new \RuntimeException("Insert failed: " . \pg_last_error($this->connection)); \pg_free_result($result); @@ -338,7 +354,7 @@ class Database { if ($primaryCol === null) throw new \RuntimeException("Fetching an entity requires a primary key column to be specified"); $query = "SELECT * FROM $tableName WHERE $primaryCol = \$1 LIMIT 1;"; - $result = \pg_query_params($this->connection, $query, [ $entity->{$cols[$primaryCol]["propertyName"]} ]); + $result = $this->queryParams($query, [ $entity->{$cols[$primaryCol]["propertyName"]} ]); if ($result === false) throw new \RuntimeException("Fetch failed: " . \pg_last_error($this->connection)); $row = \pg_fetch_assoc($result); @@ -371,7 +387,7 @@ class Database { } $whereClause = implode(" AND ", $whereClause); $query = "SELECT * FROM $tableName WHERE $whereClause LIMIT 1;"; - $result = \pg_query_params($this->connection, $query, $columnValues); + $result = $this->queryParams($query, $columnValues); if ($result === false) throw new \RuntimeException("Fetch failed: " . \pg_last_error($this->connection)); $row = \pg_fetch_assoc($result); @@ -388,7 +404,7 @@ class Database { $reflClass = new ReflectionClass($entityClassName); $cols = self::getColumns($reflClass); $query = "SELECT * FROM $tableName;"; - $result = \pg_query($this->connection, $query); + $result = $this->query($query); if ($result === false) throw new \RuntimeException("Fetch failed: " . \pg_last_error($this->connection)); $items = []; @@ -408,9 +424,9 @@ class Database { $cols = self::getColumns($reflClass); $query = "SELECT * FROM $tableName $customQuery;"; if ($customQueryParams === null) - $result = \pg_query($this->connection, $query); + $result = $this->query($query); else - $result = \pg_query_params($this->connection, $query, $customQueryParams); + $result = $this->queryParams($query, $customQueryParams); if ($result === false) throw new \RuntimeException("Fetch failed: " . \pg_last_error($this->connection)); $items = []; @@ -426,9 +442,9 @@ class Database { public function execCustomQuery(string $query, ?array $customQueryParams = null, ?string $entityClassName = null): array { if ($customQueryParams === null) - $result = \pg_query($this->connection, $query); + $result = $this->query($query); else - $result = \pg_query_params($this->connection, $query, $customQueryParams); + $result = $this->queryParams($query, $customQueryParams); if ($result === false) throw new \RuntimeException("Query failed: " . \pg_last_error($this->connection)); $cols = null; @@ -460,7 +476,7 @@ class Database { if ($primaryCol === null) throw new \RuntimeException("Deleting an entity requires a primary key column to be specified"); $query = "DELETE FROM $tableName WHERE $primaryCol = \$1;"; - $result = \pg_query_params($this->connection, $query, [ $entity->{$cols[$primaryCol]["propertyName"]} ]); + $result = $this->queryParams($query, [ $entity->{$cols[$primaryCol]["propertyName"]} ]); if ($result === false) throw new \RuntimeException("Deletion failed: " . \pg_last_error($this->connection)); $num_affected_rows = \pg_affected_rows($result); @@ -489,7 +505,7 @@ class Database { $colDef = implode(", ", $qCols); $query = "UPDATE $tableName SET $colDef WHERE $primaryCol = \$1;"; //$theCols = ArrayUtils::assocFromPairs($filteredCols); - $result = \pg_query_params($this->connection, $query, self::getColumnValues($entity, $cols)); + $result = $this->queryParams($query, self::getColumnValues($entity, $cols)); if ($result === false) throw new \RuntimeException("Update failed: " . \pg_last_error($this->connection)); $num_affected_rows = \pg_affected_rows($result); @@ -503,7 +519,7 @@ class Database { $query = "CREATE TABLE IF NOT EXISTS $tableName ();"; foreach ($colDefs as $colDef) $query .= "ALTER TABLE $tableName ADD COLUMN IF NOT EXISTS $colDef;"; - $result = \pg_query($this->connection, $query); + $result = $this->query($query); if ($result === false) throw new \RuntimeException("Table creation failed: " . \pg_last_error($this->connection)); \pg_free_result($result); diff --git a/src/application/templates/old/base.twig b/src/application/templates/old/base.twig index bdad515..bdb9768 100644 --- a/src/application/templates/old/base.twig +++ b/src/application/templates/old/base.twig @@ -73,6 +73,14 @@ © {{ "now"|date("Y") }} {{ g.env.MYSTIC_FORUM_COPYRIGHT|default(g.env.MYSTIC_FORUM_TITLE)|default("Forum") }}. Powered by mysticBB v{{ constant("MYSTICBB_VERSION") }}. + + {{ __("Page generation took %dur% second(s)", { + dur: getRunTime()|round(3), + }) }}
+ {{ __("Number of database queries: %count%", { + count: getQueryCount(), + }) }} +
diff --git a/src/index.php b/src/index.php index b7e0397..5350680 100644 --- a/src/index.php +++ b/src/index.php @@ -17,6 +17,7 @@ use Twig\TwigFilter; use Twig\TwigFunction; header_remove("X-Powered-By"); +$GLOBALS["startTime"] = microtime(true); include __DIR__ . "/application/appdef.php"; @@ -181,6 +182,8 @@ function render(string $page, array $context = [], array $functions = []): void $twig->addFunction(new TwigFunction("renderPostSummary", renderPostSummary(...), [ "is_safe" => ["html"] ])); $twig->addFunction(new TwigFunction("permission", fn(string $name): int => constant(UserPermissions::class . "::" . $name))); $twig->addFunction(new TwigFunction("getAndClearFormError", RequestUtils::getAndClearFormError(...))); + $twig->addFunction(new TwigFunction("getQueryCount", $GLOBALS["db"]->getQueryCount(...))); + $twig->addFunction(new TwigFunction("getRunTime", fn() => microtime(true) - $GLOBALS["startTime"])); $twig->addFunction(new TwigFunction("lastFormField", function(string $formId, string $field): ?string { $lastFormId = ""; $lastForm = RequestUtils::getLastForm($lastFormId) ?? null; -- cgit v1.2.3