diff --git a/benchmarks/BasicPhpBenchmark.php b/benchmarks/BasicPhpBenchmark.php index d8f5153..84aae33 100644 --- a/benchmarks/BasicPhpBenchmark.php +++ b/benchmarks/BasicPhpBenchmark.php @@ -216,11 +216,11 @@ public function benchmarkArrayChangeCastForeach(): void /** - * @title microtime with miliseconds + * @title hrtime as number */ - public function benchmarkMicrotimeMs(): void + public function benchmarkHrtimeMs(): void { - \microtime(TRUE); + \hrtime(TRUE); } diff --git a/benchmarks/BenchmarkCase.php b/benchmarks/BenchmarkCase.php index acdedd3..2f1487c 100644 --- a/benchmarks/BenchmarkCase.php +++ b/benchmarks/BenchmarkCase.php @@ -14,9 +14,9 @@ public function run(): void $class = new \ReflectionClass($this); $methods = $class->getMethods(\ReflectionMethod::IS_PUBLIC); - echo \sprintf('|----------------------------------------------------------------------------------|--------------|-------------|') . \PHP_EOL; - echo \sprintf('| %-80s | Time per run | Repeat |', $this->title()) . \PHP_EOL; - echo \sprintf('|----------------------------------------------------------------------------------|--------------|-------------|') . \PHP_EOL; + echo \sprintf('|----------------------------------------------------------------------------------|-------------------|-------------|') . \PHP_EOL; + echo \sprintf('| %-80s | Time (ms) per run | Repeat |', $this->title()) . \PHP_EOL; + echo \sprintf('|----------------------------------------------------------------------------------|-------------------|-------------|') . \PHP_EOL; foreach ($methods as $method) { $benchmarkMethod = $method->name; @@ -41,7 +41,7 @@ public function run(): void ); } - echo \sprintf('|----------------------------------------------------------------------------------|--------------|-------------|') . \PHP_EOL . \PHP_EOL; + echo \sprintf('|----------------------------------------------------------------------------------|-------------------|-------------|') . \PHP_EOL . \PHP_EOL; $this->tearDown(); } @@ -55,13 +55,13 @@ public function run(): void */ private function runBenchmark(callable $method, string $title, int $repeat): void { - $start = \microtime(TRUE); + $start = \hrtime(TRUE); for ($i = 0; $i < $repeat; $i++) { $method(); } - echo \sprintf('| %-80s | %012.10f | %11d |', \substr($title, 0, 80), (\microtime(TRUE) - $start) / $repeat, $repeat) . \PHP_EOL; + echo \sprintf('| %-80s | %012.15f | %11d |', \substr($title, 0, 80), (\hrtime(TRUE) - $start) / 1000000 / $repeat, $repeat) . \PHP_EOL; } diff --git a/phpstan.neon b/phpstan.neon index cad3559..9ee31a1 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -93,7 +93,7 @@ parameters: count: 1 - - message: '#^Call to function microtime\(\) on a separate line has no effect\.$#' + message: '#^Call to function hrtime\(\) on a separate line has no effect\.$#' path: %rootDir%/../../../benchmarks/BasicPhpBenchmark.php count: 1 diff --git a/src/Db/Connection.php b/src/Db/Connection.php index 09e9afe..399f022 100644 --- a/src/Db/Connection.php +++ b/src/Db/Connection.php @@ -296,7 +296,7 @@ public function queryArgs(string|Sql\Query $sqlQuery, array $params): Result { $query = $this->normalizeSqlQuery($sqlQuery, $params)->createQuery(); - $startTime = $this->events->hasOnQuery() ? \microtime(TRUE) : NULL; + $startTime = $this->events->hasOnQuery() ? \hrtime(TRUE) : NULL; $queryParams = $query->getParams(); if ($queryParams === []) { @@ -309,7 +309,7 @@ public function queryArgs(string|Sql\Query $sqlQuery, array $params): Result } if ($startTime !== NULL) { - $this->events->onQuery($query, \microtime(TRUE) - $startTime); + $this->events->onQuery($query, \hrtime(TRUE) - $startTime); } return $this->createResult($resource, $query); @@ -341,7 +341,7 @@ public function createResult(PgSql\Result $resource, Query $query): Result */ public function execute(string $sql): self { - $startTime = $this->events->hasOnQuery() ? \microtime(TRUE) : NULL; + $startTime = $this->events->hasOnQuery() ? \hrtime(TRUE) : NULL; $resource = @\pg_query($this->getConnectedResource(), $sql); // intentionally @ if ($resource === FALSE) { @@ -349,7 +349,7 @@ public function execute(string $sql): self } if ($startTime !== NULL) { - $this->events->onQuery(new Query($sql, []), \microtime(TRUE) - $startTime); + $this->events->onQuery(new Query($sql, []), \hrtime(TRUE) - $startTime); } return $this; @@ -561,9 +561,9 @@ private function getConnectedResource(): PgSql\Connection \assert($this->resource !== NULL); if ($this->connected === FALSE) { - $start = \microtime(TRUE); + $start = \hrtime(TRUE); do { - $test = \microtime(TRUE); + $test = \hrtime(TRUE); switch (\pg_connect_poll($this->resource)) { case \PGSQL_POLLING_READING: while (!self::isReadable($this->asyncStream)); @@ -583,7 +583,7 @@ private function getConnectedResource(): PgSql\Connection return $this->resource; } - } while (($test - $start) <= $this->connectAsyncWaitSeconds); + } while ((($test - $start) / 1000000000) <= $this->connectAsyncWaitSeconds); throw Exceptions\ConnectionException::asyncConnectTimeout($test, $this->connectAsyncWaitSeconds); } diff --git a/src/Db/Events.php b/src/Db/Events.php index 7e5788a..fbdc631 100644 --- a/src/Db/Events.php +++ b/src/Db/Events.php @@ -12,7 +12,7 @@ class Events /** @var list function (Connection $connection) {} */ private array $onClose = []; - /** @var list function (Connection $connection, Query $query, float|NULL $time, string|NULL $prepareStatementName) {} */ + /** @var list function (Connection $connection, Query $query, int|float|NULL $timeNs, string|NULL $prepareStatementName) {} */ private array $onQuery = []; /** @var list function (Connection $connection, Result $result) {} */ @@ -69,10 +69,10 @@ public function onClose(): void } - public function onQuery(Query $query, float|NULL $time = NULL, string|NULL $prepareStatementName = NULL): void + public function onQuery(Query $query, float|NULL $timeNs = NULL, string|NULL $prepareStatementName = NULL): void { foreach ($this->onQuery as $event) { - $event($this->connection, $query, $time, $prepareStatementName); + $event($this->connection, $query, $timeNs, $prepareStatementName); } } diff --git a/src/Db/PreparedStatement.php b/src/Db/PreparedStatement.php index c4723df..3dc93cf 100644 --- a/src/Db/PreparedStatement.php +++ b/src/Db/PreparedStatement.php @@ -18,7 +18,7 @@ public function executeArgs(array $params): Result { $statementName = $this->prepareStatement(); - $startTime = $this->events->hasOnQuery() ? \microtime(TRUE) : NULL; + $startTime = $this->events->hasOnQuery() ? \hrtime(TRUE) : NULL; $params = self::prepareParams($params); @@ -30,7 +30,7 @@ public function executeArgs(array $params): Result } if ($startTime !== NULL) { - $this->events->onQuery($query, \microtime(TRUE) - $startTime, $statementName); + $this->events->onQuery($query, \hrtime(TRUE) - $startTime, $statementName); } return $this->connection->createResult($resource, $query);