diff --git a/src/Handler/CurlMultiHandler.php b/src/Handler/CurlMultiHandler.php index 7097835d1..d8297623c 100644 --- a/src/Handler/CurlMultiHandler.php +++ b/src/Handler/CurlMultiHandler.php @@ -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]); @@ -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); } } @@ -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) { diff --git a/src/Handler/StreamHandler.php b/src/Handler/StreamHandler.php index 741e02d2a..0dedd7da4 100644 --- a/src/Handler/StreamHandler.php +++ b/src/Handler/StreamHandler.php @@ -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. @@ -82,7 +82,7 @@ private function invokeStats( $stats = new TransferStats( $request, $response, - microtime(true) - $startTime, + \GuzzleHttp\_current_time() - $startTime, $error, [] ); diff --git a/src/functions.php b/src/functions.php index 5081bb5fa..3c472bc6a 100644 --- a/src/functions.php +++ b/src/functions.php @@ -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); +} diff --git a/tests/Handler/CurlHandlerTest.php b/tests/Handler/CurlHandlerTest.php index 25c499020..6a4d233a0 100644 --- a/tests/Handler/CurlHandlerTest.php +++ b/tests/Handler/CurlHandlerTest.php @@ -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() diff --git a/tests/Handler/CurlMultiHandlerTest.php b/tests/Handler/CurlMultiHandlerTest.php index 6c987afb8..03043f5ff 100644 --- a/tests/Handler/CurlMultiHandlerTest.php +++ b/tests/Handler/CurlMultiHandlerTest.php @@ -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() diff --git a/tests/Handler/StreamHandlerTest.php b/tests/Handler/StreamHandlerTest.php index 6c10ec3f0..f62994896 100644 --- a/tests/Handler/StreamHandlerTest.php +++ b/tests/Handler/StreamHandlerTest.php @@ -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); } /** diff --git a/tests/functionsTest.php b/tests/functionsTest.php index fd65d41b6..bf5c8d8b8 100644 --- a/tests/functionsTest.php +++ b/tests/functionsTest.php @@ -128,6 +128,11 @@ public function testDecodesJsonAndThrowsOnError() { \GuzzleHttp\json_decode('{{]]'); } + + public function testCurrentTime() + { + $this->assertGreaterThan(0, GuzzleHttp\_current_time()); + } } final class StrClass