Skip to content

Commit

Permalink
PHP: use hrtime instead of microtime
Browse files Browse the repository at this point in the history
  • Loading branch information
forrest79 committed Jan 23, 2024
1 parent 11f6046 commit 698c6f7
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 22 deletions.
6 changes: 3 additions & 3 deletions benchmarks/BasicPhpBenchmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down
12 changes: 6 additions & 6 deletions benchmarks/BenchmarkCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,7 +41,7 @@ public function run(): void
);
}

echo \sprintf('|----------------------------------------------------------------------------------|--------------|-------------|') . \PHP_EOL . \PHP_EOL;
echo \sprintf('|----------------------------------------------------------------------------------|-------------------|-------------|') . \PHP_EOL . \PHP_EOL;

$this->tearDown();
}
Expand All @@ -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;
}


Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 7 additions & 7 deletions src/Db/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 === []) {
Expand All @@ -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);
Expand Down Expand Up @@ -341,15 +341,15 @@ 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) {
throw Exceptions\QueryException::queryFailed(new Query($sql, []), $this->getLastError());
}

if ($startTime !== NULL) {
$this->events->onQuery(new Query($sql, []), \microtime(TRUE) - $startTime);
$this->events->onQuery(new Query($sql, []), \hrtime(TRUE) - $startTime);
}

return $this;
Expand Down Expand Up @@ -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));
Expand All @@ -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);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Db/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Events
/** @var list<callable(Connection): void> function (Connection $connection) {} */
private array $onClose = [];

/** @var list<callable(Connection, Query, float|NULL, string|NULL): void> function (Connection $connection, Query $query, float|NULL $time, string|NULL $prepareStatementName) {} */
/** @var list<callable(Connection, Query, int|float|NULL, string|NULL): void> function (Connection $connection, Query $query, int|float|NULL $timeNs, string|NULL $prepareStatementName) {} */
private array $onQuery = [];

/** @var list<callable(Connection, Result): void> function (Connection $connection, Result $result) {} */
Expand Down Expand Up @@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Db/PreparedStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
Expand Down

0 comments on commit 698c6f7

Please sign in to comment.