Skip to content

Commit

Permalink
bug #32147 [HttpClient] fix timing measurements with NativeHttpClient…
Browse files Browse the repository at this point in the history
… (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
-------

c5c3332 [HttpClient] fix timing measurements with NativeHttpClient
  • Loading branch information
fabpot committed Jun 26, 2019
2 parents e8c68d5 + c5c3332 commit a590829
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
15 changes: 7 additions & 8 deletions src/Symfony/Component/HttpClient/NativeHttpClient.php
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
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 comments on commit a590829

Please sign in to comment.