1
0
Fork 0

Add saving time duration for curl HTTP requests

pull/9592/head
Wissem Riahi 2021-01-06 16:05:00 +01:00
parent 2406a094d3
commit 370e082b4a
3 changed files with 32 additions and 1 deletions

View File

@ -20,6 +20,7 @@ class TransportException extends \RuntimeException
protected $headers; protected $headers;
protected $response; protected $response;
protected $statusCode; protected $statusCode;
protected $totalResponseTime;
public function setHeaders($headers) public function setHeaders($headers)
{ {
@ -50,4 +51,14 @@ class TransportException extends \RuntimeException
{ {
return $this->statusCode; return $this->statusCode;
} }
public function getTotalResponseTime()
{
return $this->totalResponseTime;
}
public function setTotalResponseTime($totalResponseTime)
{
$this->totalResponseTime = $totalResponseTime;
}
} }

View File

@ -303,7 +303,10 @@ class CurlDownloader
if (!$error && function_exists('curl_strerror')) { if (!$error && function_exists('curl_strerror')) {
$error = curl_strerror($errno); $error = curl_strerror($errno);
} }
throw new TransportException('curl error '.$errno.' while downloading '.Url::sanitize($progress['url']).': '.$error);
$exception = new TransportException('curl error '.$errno.' while downloading '.Url::sanitize($progress['url']).': '.$error);
$exception->setTotalResponseTime($progress['total_time_us']);
throw $exception;
} }
$statusCode = $progress['http_code']; $statusCode = $progress['http_code'];
rewind($job['headerHandle']); rewind($job['headerHandle']);
@ -322,11 +325,13 @@ class CurlDownloader
$contents = stream_get_contents($job['bodyHandle']); $contents = stream_get_contents($job['bodyHandle']);
} }
$response = new Response(array('url' => $progress['url']), $statusCode, $headers, $contents); $response = new Response(array('url' => $progress['url']), $statusCode, $headers, $contents);
$response->setTotalResponseTime($progress['total_time_us']);
$this->io->writeError('['.$statusCode.'] '.Url::sanitize($progress['url']), true, IOInterface::DEBUG); $this->io->writeError('['.$statusCode.'] '.Url::sanitize($progress['url']), true, IOInterface::DEBUG);
} else { } else {
rewind($job['bodyHandle']); rewind($job['bodyHandle']);
$contents = stream_get_contents($job['bodyHandle']); $contents = stream_get_contents($job['bodyHandle']);
$response = new Response(array('url' => $progress['url']), $statusCode, $headers, $contents); $response = new Response(array('url' => $progress['url']), $statusCode, $headers, $contents);
$response->setTotalResponseTime($progress['total_time_us']);
$this->io->writeError('['.$statusCode.'] '.Url::sanitize($progress['url']), true, IOInterface::DEBUG); $this->io->writeError('['.$statusCode.'] '.Url::sanitize($progress['url']), true, IOInterface::DEBUG);
} }
fclose($job['bodyHandle']); fclose($job['bodyHandle']);

View File

@ -20,6 +20,7 @@ class Response
private $code; private $code;
private $headers; private $headers;
private $body; private $body;
private $totalResponseTime;
public function __construct(array $request, $code, array $headers, $body) public function __construct(array $request, $code, array $headers, $body)
{ {
@ -69,6 +70,20 @@ class Response
return $this->body; return $this->body;
} }
/**
* Total duration time it took for the response in micro seconds
* @return int|null
*/
public function getTotalResponseTime()
{
return $this->totalResponseTime;
}
public function setTotalResponseTime($totalResponseTime)
{
$this->totalResponseTime = $totalResponseTime;
}
public function decodeJson() public function decodeJson()
{ {
return JsonFile::parseJson($this->body, $this->request['url']); return JsonFile::parseJson($this->body, $this->request['url']);