8000 [HttpClient] fix timing measurements with NativeHttpClient by nicolas-grekas · Pull Request #32147 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/Symfony/Component/HttpClient/NativeHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ public function request(string $method, string $url, array $options = []): Respo
'http_code' => 0,
'redirect_count' => 0,
'start_time' => 0.0,
'fopen_time' => 0.0,
'connect_time' => 0.0,
'redirect_time' => 0.0,
'pretransfer_time' => 0.0,
'starttransfer_time' => 0.0,
'total_time' => 0.0,
'namelookup_time' => 0.0,
Expand All @@ -118,7 +118,7 @@ public function request(string $method, string $url, array $options = []): Respo
$onProgress = static function (...$progress) use ($onProgress, &$lastProgress, &$info) {
$progressInfo = $info;
$progressInfo['url'] = implode('', $info['url']);
unset($progressInfo['fopen_time'], $progressInfo['size_body']);
unset($progressInfo['size_body']);

if ($progress && -1 === $progress[0]) {
// Response completed
Expand All @@ -133,14 +133,14 @@ public function request(string $method, string $url, array $options = []): Respo

// 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) {
$now = microtime(true);
$info['total_time'] = $now - $info['start_time'];
$info['total_time'] = microtime(true) - $info['start_time'];

if (STREAM_NOTIFY_PROGRESS === $code) {
$info['starttransfer_time'] = $info['starttransfer_time'] ?: $info['total_time'];
$info['size_upload'] += $dlNow ? 0 : $info['size_body'];
$info['size_download'] = $dlNow;
} elseif (STREAM_NOTIFY_CONNECT === $code) {
$info['connect_time'] += $now - $info['fopen_time'];
$info['connect_time'] = $info['total_time'];
$info['debug'] .= $info['request_header'];
unset($info['request_header']);
} else {
Expand Down Expand Up @@ -310,7 +310,7 @@ private static function dnsResolve(array $url, NativeClientState $multi, array &
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];
$info['debug'] .= "* Added {$host}:0:{$ip} to DNS cache\n";
} else {
Expand Down Expand Up @@ -368,10 +368,9 @@ private static function createRedirectResolver(array $options, string $host, ?ar
return null;
}

$now = microtime(true);
$info['url'] = $url;
++$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
if (\in_array($info['http_code'], [301, 302, 303], true)) {
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/HttpClient/Response/NativeResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function getInfo(string $type = null)

$info = $this->info;
$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) {
$this->finalInfo = $info;
Expand Down Expand Up @@ -128,7 +128,6 @@ private function open(): void
$this->info['request_header'] .= implode("\r\n", $context['http']['header'])."\r\n\r\n";

// Send request and follow redirects when needed
$this->info['fopen_time'] = microtime(true);
$this->handle = $h = fopen($url, 'r', false, $this->context);
self::addResponseHeaders($http_response_header, $this->info, $this->headers, $this->info['debug']);
$url = ($this->resolveRedirect)($this->multi, $this->headers['location'][0] ?? null, $this->context);
Expand All @@ -146,7 +145,7 @@ private function open(): void

return;
} 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();
}

Expand Down Expand Up @@ -267,6 +266,7 @@ private static function perform(NativeClientState $multi, array &$responses = nu
if (null !== $e || !$remaining || feof($h)) {
// Stream completed
$info['total_time'] = microtime(true) - $info['start_time'];
$info['starttransfer_time'] = $info['starttransfer_time'] ?: $info['total_time'];

if ($onProgress) {
try {
Expand Down
0