bug #32147 [HttpClient] fix timing measurements with NativeHttpClient (nicolas-grekas)
This PR was merged into the 4.3 branch.
Discussion
----------
[HttpClient] fix timing measurements with NativeHttpClient
| Q | A
| ------------- | ---
| Branch? | 4.3
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | -
| License | MIT
| Doc PR | -
Align to what curl does.
Commits
-------
c5c3332400
[HttpClient] fix timing measurements with NativeHttpClient
This commit is contained in:
commit
a59082937f
@ -98,9 +98,9 @@ final class NativeHttpClient implements HttpClientInterface, LoggerAwareInterfac
|
|||||||
'http_code' => 0,
|
'http_code' => 0,
|
||||||
'redirect_count' => 0,
|
'redirect_count' => 0,
|
||||||
'start_time' => 0.0,
|
'start_time' => 0.0,
|
||||||
'fopen_time' => 0.0,
|
|
||||||
'connect_time' => 0.0,
|
'connect_time' => 0.0,
|
||||||
'redirect_time' => 0.0,
|
'redirect_time' => 0.0,
|
||||||
|
'pretransfer_time' => 0.0,
|
||||||
'starttransfer_time' => 0.0,
|
'starttransfer_time' => 0.0,
|
||||||
'total_time' => 0.0,
|
'total_time' => 0.0,
|
||||||
'namelookup_time' => 0.0,
|
'namelookup_time' => 0.0,
|
||||||
@ -118,7 +118,7 @@ final class NativeHttpClient implements HttpClientInterface, LoggerAwareInterfac
|
|||||||
$onProgress = static function (...$progress) use ($onProgress, &$lastProgress, &$info) {
|
$onProgress = static function (...$progress) use ($onProgress, &$lastProgress, &$info) {
|
||||||
$progressInfo = $info;
|
$progressInfo = $info;
|
||||||
$progressInfo['url'] = implode('', $info['url']);
|
$progressInfo['url'] = implode('', $info['url']);
|
||||||
unset($progressInfo['fopen_time'], $progressInfo['size_body']);
|
unset($progressInfo['size_body']);
|
||||||
|
|
||||||
if ($progress && -1 === $progress[0]) {
|
if ($progress && -1 === $progress[0]) {
|
||||||
// Response completed
|
// Response completed
|
||||||
@ -133,14 +133,14 @@ final class NativeHttpClient implements HttpClientInterface, LoggerAwareInterfac
|
|||||||
|
|
||||||
// Always register a notification callback to compute live stats about the response
|
// Always register a notification callback to compute live stats about the response
|
||||||
$notification = static function (int $code, int $severity, ?string $msg, int $msgCode, int $dlNow, int $dlSize) use ($onProgress, &$info) {
|
$notification = static function (int $code, int $severity, ?string $msg, int $msgCode, int $dlNow, int $dlSize) use ($onProgress, &$info) {
|
||||||
$now = microtime(true);
|
$info['total_time'] = microtime(true) - $info['start_time'];
|
||||||
$info['total_time'] = $now - $info['start_time'];
|
|
||||||
|
|
||||||
if (STREAM_NOTIFY_PROGRESS === $code) {
|
if (STREAM_NOTIFY_PROGRESS === $code) {
|
||||||
|
$info['starttransfer_time'] = $info['starttransfer_time'] ?: $info['total_time'];
|
||||||
$info['size_upload'] += $dlNow ? 0 : $info['size_body'];
|
$info['size_upload'] += $dlNow ? 0 : $info['size_body'];
|
||||||
$info['size_download'] = $dlNow;
|
$info['size_download'] = $dlNow;
|
||||||
} elseif (STREAM_NOTIFY_CONNECT === $code) {
|
} elseif (STREAM_NOTIFY_CONNECT === $code) {
|
||||||
$info['connect_time'] += $now - $info['fopen_time'];
|
$info['connect_time'] = $info['total_time'];
|
||||||
$info['debug'] .= $info['request_header'];
|
$info['debug'] .= $info['request_header'];
|
||||||
unset($info['request_header']);
|
unset($info['request_header']);
|
||||||
} else {
|
} else {
|
||||||
@ -310,7 +310,7 @@ final class NativeHttpClient implements HttpClientInterface, LoggerAwareInterfac
|
|||||||
throw new TransportException(sprintf('Could not resolve host "%s".', $host));
|
throw new TransportException(sprintf('Could not resolve host "%s".', $host));
|
||||||
}
|
}
|
||||||
|
|
||||||
$info['namelookup_time'] += microtime(true) - $now;
|
$info['namelookup_time'] = microtime(true) - ($info['start_time'] ?: $now);
|
||||||
$multi->dnsCache[$host] = $ip = $ip[0];
|
$multi->dnsCache[$host] = $ip = $ip[0];
|
||||||
$info['debug'] .= "* Added {$host}:0:{$ip} to DNS cache\n";
|
$info['debug'] .= "* Added {$host}:0:{$ip} to DNS cache\n";
|
||||||
} else {
|
} else {
|
||||||
@ -368,10 +368,9 @@ final class NativeHttpClient implements HttpClientInterface, LoggerAwareInterfac
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
$now = microtime(true);
|
|
||||||
$info['url'] = $url;
|
$info['url'] = $url;
|
||||||
++$info['redirect_count'];
|
++$info['redirect_count'];
|
||||||
$info['redirect_time'] = $now - $info['start_time'];
|
$info['redirect_time'] = microtime(true) - $info['start_time'];
|
||||||
|
|
||||||
// Do like curl and browsers: turn POST to GET on 301, 302 and 303
|
// Do like curl and browsers: turn POST to GET on 301, 302 and 303
|
||||||
if (\in_array($info['http_code'], [301, 302, 303], true)) {
|
if (\in_array($info['http_code'], [301, 302, 303], true)) {
|
||||||
|
@ -80,7 +80,7 @@ final class NativeResponse implements ResponseInterface
|
|||||||
|
|
||||||
$info = $this->info;
|
$info = $this->info;
|
||||||
$info['url'] = implode('', $info['url']);
|
$info['url'] = implode('', $info['url']);
|
||||||
unset($info['fopen_time'], $info['size_body'], $info['request_header']);
|
unset($info['size_body'], $info['request_header']);
|
||||||
|
|
||||||
if (null === $this->buffer) {
|
if (null === $this->buffer) {
|
||||||
$this->finalInfo = $info;
|
$this->finalInfo = $info;
|
||||||
@ -128,7 +128,6 @@ final class NativeResponse implements ResponseInterface
|
|||||||
$this->info['request_header'] .= implode("\r\n", $context['http']['header'])."\r\n\r\n";
|
$this->info['request_header'] .= implode("\r\n", $context['http']['header'])."\r\n\r\n";
|
||||||
|
|
||||||
// Send request and follow redirects when needed
|
// Send request and follow redirects when needed
|
||||||
$this->info['fopen_time'] = microtime(true);
|
|
||||||
$this->handle = $h = fopen($url, 'r', false, $this->context);
|
$this->handle = $h = fopen($url, 'r', false, $this->context);
|
||||||
self::addResponseHeaders($http_response_header, $this->info, $this->headers, $this->info['debug']);
|
self::addResponseHeaders($http_response_header, $this->info, $this->headers, $this->info['debug']);
|
||||||
$url = ($this->resolveRedirect)($this->multi, $this->headers['location'][0] ?? null, $this->context);
|
$url = ($this->resolveRedirect)($this->multi, $this->headers['location'][0] ?? null, $this->context);
|
||||||
@ -146,7 +145,7 @@ final class NativeResponse implements ResponseInterface
|
|||||||
|
|
||||||
return;
|
return;
|
||||||
} finally {
|
} finally {
|
||||||
$this->info['starttransfer_time'] = $this->info['total_time'] = microtime(true) - $this->info['start_time'];
|
$this->info['pretransfer_time'] = $this->info['total_time'] = microtime(true) - $this->info['start_time'];
|
||||||
restore_error_handler();
|
restore_error_handler();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -267,6 +266,7 @@ final class NativeResponse implements ResponseInterface
|
|||||||
if (null !== $e || !$remaining || feof($h)) {
|
if (null !== $e || !$remaining || feof($h)) {
|
||||||
// Stream completed
|
// Stream completed
|
||||||
$info['total_time'] = microtime(true) - $info['start_time'];
|
$info['total_time'] = microtime(true) - $info['start_time'];
|
||||||
|
$info['starttransfer_time'] = $info['starttransfer_time'] ?: $info['total_time'];
|
||||||
|
|
||||||
if ($onProgress) {
|
if ($onProgress) {
|
||||||
try {
|
try {
|
||||||
|
Reference in New Issue
Block a user