Skip to content

Commit f6468c7

Browse files
authored
Merge pull request #412 from bowphp/refactor/code-base
feat(database): implement query execution performance metric mesurement
2 parents e5ac986 + 33e4a5d commit f6468c7

3 files changed

Lines changed: 31 additions & 11 deletions

File tree

src/Database/Database.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,11 @@ private static function executePrepareQuery(string $sql_statement, array $data =
191191
Sanitize::make($data, true)
192192
);
193193

194+
$start_at = microtime(true);
194195
$pdo_statement->execute();
196+
$ended_at = microtime(true);
195197

196-
static::triggerQueryEvent($sql_statement, $data);
198+
static::triggerQueryEvent($sql_statement, $ended_at - $start_at, $data);
197199

198200
return $pdo_statement->rowCount();
199201
}
@@ -490,12 +492,13 @@ public static function setPdo(PDO $pdo): void
490492
* Trigger the query executed event
491493
*
492494
* @param string $sql
495+
* @param float $execution_time
493496
* @param array $bindings
494497
* @return void
495498
*/
496-
public static function triggerQueryEvent(string $sql, array $bindings = []): void
499+
public static function triggerQueryEvent(string $sql, float $execution_time = 0, array $bindings = []): void
497500
{
498-
$event = new QueryEvent($sql, $bindings);
501+
$event = new QueryEvent($sql, $execution_time, $bindings);
499502

500503
app_event($event);
501504
}

src/Database/QueryBuilder.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,9 +1417,11 @@ public function truncate(): bool
14171417

14181418
$this->last_query = $sql;
14191419

1420+
$start_at = microtime(true);
14201421
$result = (bool) $this->connection->exec($sql);
1422+
$ended_at = microtime(true);
14211423

1422-
$this->triggerQueryEvent($sql, []);
1424+
$this->triggerQueryEvent($sql, $ended_at - $start_at);
14231425

14241426
$this->last_query = $sql;
14251427

@@ -1523,9 +1525,11 @@ private function execute(string $sql, array $bindings = []): PDOStatement
15231525
$this->bind($statement, $bindings);
15241526

15251527
try {
1528+
$start_at = microtime(true);
15261529
$statement->execute();
1530+
$ended_at = microtime(true);
15271531

1528-
$this->triggerQueryEvent($sql, $bindings);
1532+
$this->triggerQueryEvent($sql, $ended_at - $start_at, $bindings);
15291533
} catch (\Exception $e) {
15301534
throw new QueryBuilderException(
15311535
'Error executing query: ' . $e->getMessage() . ' | Query: ' . $this->last_query,
@@ -1548,9 +1552,11 @@ public function drop(): bool
15481552

15491553
$this->last_query = $sql;
15501554

1555+
$start_at = microtime(true);
15511556
$result = (bool) $this->connection->exec($sql);
1557+
$ended_at = microtime(true);
15521558

1553-
$this->triggerQueryEvent($sql, []);
1559+
$this->triggerQueryEvent($sql, $ended_at - $start_at);
15541560

15551561
return $result;
15561562
}
@@ -1668,12 +1674,13 @@ public function setWhereDataBinding(array $data_binding): void
16681674
* Trigger the query event
16691675
*
16701676
* @param string $sql
1677+
* @param float $execution_time
16711678
* @param array $bindings
16721679
* @return void
16731680
*/
1674-
private function triggerQueryEvent(string $sql, array $bindings): void
1681+
private function triggerQueryEvent(string $sql, float $execution_time = 0, array $bindings = []): void
16751682
{
1676-
Database::triggerQueryEvent($sql, $bindings);
1683+
Database::triggerQueryEvent($sql, $execution_time, $bindings);
16771684
}
16781685

16791686
/**

src/Database/QueryEvent.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ final class QueryEvent implements AppEvent
1818
*/
1919
public string $sql;
2020

21+
/**
22+
* Define the query execution time
23+
*
24+
* @var mixed
25+
*/
26+
public float $execution_time;
27+
2128
/**
2229
* The query bindings
2330
*
@@ -28,11 +35,14 @@ final class QueryEvent implements AppEvent
2835
/**
2936
* QueryEvent constructor.
3037
*
31-
* @param array $data
38+
* @param string $sql
39+
* @param float $execution_time
40+
* @param array $bindings
3241
*/
33-
public function __construct(string $sql, array $bindings = [])
42+
public function __construct(string $sql, float $execution_time = 0, array $bindings = [])
3443
{
3544
$this->sql = $sql;
45+
$this->execution_time = $execution_time;
3646
$this->bindings = $bindings;
3747
}
3848

@@ -51,7 +61,7 @@ public function getName(): string
5161
* @param mixed $value
5262
* @throws \Exception
5363
*/
54-
public function __set($name, $value)
64+
final public function __set($name, $value)
5565
{
5666
throw new \Exception("Cannot set property $name on QueryEvent");
5767
}

0 commit comments

Comments
 (0)