Skip to content

Commit

Permalink
Replace microtime() usages with hrtime() (#2242)
Browse files Browse the repository at this point in the history
* Replace microtime() usages with hrtime()

* Replace microtime() usages with hrtime()

* Wrongly used 1e9 instead of 1e7

* Replaced 1e7 with 1e9

* Marked function as @internal

* Internal function name prefixed with underscore
  • Loading branch information
Dzhuneyt authored and Nyholm committed Apr 15, 2019
1 parent 3d499a1 commit 3b0452a
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/Handler/CurlMultiHandler.php
Expand Up @@ -88,7 +88,7 @@ public function tick()
{
// Add any delayed handles if needed.
if ($this->delays) {
$currentTime = microtime(true);
$currentTime = \GuzzleHttp\_current_time();
foreach ($this->delays as $id => $delay) {
if ($currentTime >= $delay) {
unset($this->delays[$id]);
Expand Down Expand Up @@ -140,7 +140,7 @@ private function addRequest(array $entry)
if (empty($easy->options['delay'])) {
curl_multi_add_handle($this->_mh, $easy->handle);
} else {
$this->delays[$id] = microtime(true) + ($easy->options['delay'] / 1000);
$this->delays[$id] = \GuzzleHttp\_current_time() + ($easy->options['delay'] / 1000);
}
}

Expand Down Expand Up @@ -192,7 +192,7 @@ private function processMessages()

private function timeToNext()
{
$currentTime = microtime(true);
$currentTime = \GuzzleHttp\_current_time();
$nextTime = PHP_INT_MAX;
foreach ($this->delays as $time) {
if ($time < $nextTime) {
Expand Down
4 changes: 2 additions & 2 deletions src/Handler/StreamHandler.php
Expand Up @@ -33,7 +33,7 @@ public function __invoke(RequestInterface $request, array $options)
usleep($options['delay'] * 1000);
}

$startTime = isset($options['on_stats']) ? microtime(true) : null;
$startTime = isset($options['on_stats']) ? \GuzzleHttp\_current_time() : null;

try {
// Does not support the expect header.
Expand Down Expand Up @@ -82,7 +82,7 @@ private function invokeStats(
$stats = new TransferStats(
$request,
$response,
microtime(true) - $startTime,
\GuzzleHttp\_current_time() - $startTime,
$error,
[]
);
Expand Down
12 changes: 12 additions & 0 deletions src/functions.php
Expand Up @@ -331,3 +331,15 @@ function json_encode($value, $options = 0, $depth = 512)

return $json;
}

/**
* Wrapper for the hrtime() or microtime() functions
* (depending on the PHP version, one of the two is used)
*
* @return float|mixed UNIX timestamp
* @internal
*/
function _current_time()
{
return function_exists('hrtime') ? hrtime(true) / 1e9 : microtime(true);
}
4 changes: 2 additions & 2 deletions tests/Handler/CurlHandlerTest.php
Expand Up @@ -47,9 +47,9 @@ public function testDoesSleep()
Server::enqueue([$response]);
$a = new CurlHandler();
$request = new Request('GET', Server::$url);
$s = microtime(true);
$s = \GuzzleHttp\_current_time();
$a($request, ['delay' => 0.1])->wait();
$this->assertGreaterThan(0.0001, microtime(true) - $s);
$this->assertGreaterThan(0.0001, \GuzzleHttp\_current_time() - $s);
}

public function testCreatesCurlErrorsWithContext()
Expand Down
4 changes: 2 additions & 2 deletions tests/Handler/CurlMultiHandlerTest.php
Expand Up @@ -68,10 +68,10 @@ public function testDelaysConcurrently()
Server::flush();
Server::enqueue([new Response()]);
$a = new CurlMultiHandler();
$expected = microtime(true) + (100 / 1000);
$expected = \GuzzleHttp\_current_time() + (100 / 1000);
$response = $a(new Request('GET', Server::$url), ['delay' => 100]);
$response->wait();
$this->assertGreaterThanOrEqual($expected, microtime(true));
$this->assertGreaterThanOrEqual($expected, \GuzzleHttp\_current_time());
}

public function testUsesTimeoutEnvironmentVariables()
Expand Down
4 changes: 2 additions & 2 deletions tests/Handler/StreamHandlerTest.php
Expand Up @@ -506,9 +506,9 @@ public function testDoesSleep()
Server::enqueue([$response]);
$a = new StreamHandler();
$request = new Request('GET', Server::$url);
$s = microtime(true);
$s = \GuzzleHttp\_current_time();
$a($request, ['delay' => 0.1])->wait();
$this->assertGreaterThan(0.0001, microtime(true) - $s);
$this->assertGreaterThan(0.0001, \GuzzleHttp\_current_time() - $s);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/functionsTest.php
Expand Up @@ -128,6 +128,11 @@ public function testDecodesJsonAndThrowsOnError()
{
\GuzzleHttp\json_decode('{{]]');
}

public function testCurrentTime()
{
$this->assertGreaterThan(0, GuzzleHttp\_current_time());
}
}

final class StrClass
Expand Down

0 comments on commit 3b0452a

Please sign in to comment.