Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace microtime() usages with hrtime() #2242

Merged
merged 6 commits into from Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
*/
Dzhuneyt marked this conversation as resolved.
Show resolved Hide resolved
function _current_time()
{
return function_exists('hrtime') ? hrtime(true) / 1e9 : microtime(true);
Dzhuneyt marked this conversation as resolved.
Show resolved Hide resolved
}
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