Skip to content

Commit

Permalink
add request executionTime time for debugging purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
rafrsr committed Jan 25, 2021
1 parent 701e6a9 commit bd6b136
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 11 deletions.
5 changes: 4 additions & 1 deletion src/Debug/ApiDebugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ public function beginRequestProcess(RequestInterface $request)
* @param RequestProcess $process
* @param ResponseInterface $response
* @param Exception|null $exception
* @param int $executionTime
*/
public function finishRequestProcess(RequestProcess $process, ResponseInterface $response = null, Exception $exception = null)
public function finishRequestProcess(RequestProcess $process, ResponseInterface $response = null, Exception $exception = null, $executionTime = 0)
{
$process->setExecutionTime($executionTime);

if ($response) {
$process->setResponse($response);
}
Expand Down
29 changes: 28 additions & 1 deletion src/Debug/RequestProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,25 @@ class RequestProcess
*/
protected $exception;

/**
* @var int
*/
protected $executionTime = 0;

/**
* RequestProcess constructor.
*
* @param RequestInterface $request
* @param ResponseInterface $response
* @param Exception $exception
* @param int $executionTime
*/
public function __construct(RequestInterface $request = null, ResponseInterface $response = null, Exception $exception = null)
public function __construct(RequestInterface $request = null, ResponseInterface $response = null, Exception $exception = null, $executionTime = 0)
{
$this->request = $request;
$this->response = $response;
$this->exception = $exception;
$this->executionTime = $executionTime;
}

/**
Expand Down Expand Up @@ -105,4 +112,24 @@ public function setException(Exception $exception)

return $this;
}

/**
* @return int
*/
public function getExecutionTime()
{
return $this->executionTime;
}

/**
* @param int $executionTime
*
* @return $this
*/
public function setExecutionTime($executionTime)
{
$this->executionTime = $executionTime;

return $this;
}
}
19 changes: 18 additions & 1 deletion src/Event/OnResponseEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ class OnResponseEvent extends ApiEvent
*/
protected $exception;

/**
* Request execution time in ms
*
* @var int
*/
protected $executionTime = 0;

/**
* PreBuildRequestEvent constructor.
*
Expand All @@ -41,13 +48,15 @@ class OnResponseEvent extends ApiEvent
* @param ResponseInterface $response
* @param RequestInterface|null $request TODO: mark as required in v3.0
* @param \Exception|null $exception
* @param int $executionTime
*/
public function __construct(ApiInterface $api, ApiServiceInterface $service, ResponseInterface $response, RequestInterface $request = null, \Exception $exception = null)
public function __construct(ApiInterface $api, ApiServiceInterface $service, ResponseInterface $response, RequestInterface $request = null, \Exception $exception = null, $executionTime = 0)
{
parent::__construct($api, $service);
$this->response = $response;
$this->request = $request;
$this->exception = $exception;
$this->executionTime = $executionTime;
}

/**
Expand All @@ -73,4 +82,12 @@ public function getException()
{
return $this->exception;
}

/**
* @return int
*/
public function getExecutionTime()
{
return $this->executionTime;
}
}
9 changes: 6 additions & 3 deletions src/GenericApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public function process(ApiServiceInterface $service)

$exception = null;
$httpResponse = null;
$startTime = microtime(true) * 1000;
try {
$httpResponse = $this->sendRequest($request);
} catch (RequestException $e) {
Expand All @@ -158,8 +159,10 @@ public function process(ApiServiceInterface $service)
$exception = $e;
}

$executionTime = number_format((microtime(true) * 1000) - $startTime, 0, '', '');

if ($debugProcess !== null) {
$this->debugger->finishRequestProcess($debugProcess, $httpResponse, $exception);
$this->debugger->finishRequestProcess($debugProcess, $httpResponse, $exception, $executionTime);
}

$newResponse = null;
Expand All @@ -172,7 +175,7 @@ public function process(ApiServiceInterface $service)
}

if ($httpResponse) {
$this->getEventDispatcher()->dispatch(self::EVENT_ON_RESPONSE, new OnResponseEvent($this, $service, $httpResponse, $request, $exception));
$this->getEventDispatcher()->dispatch(self::EVENT_ON_RESPONSE, new OnResponseEvent($this, $service, $httpResponse, $request, $exception, $executionTime));
}

//has pending exception
Expand Down Expand Up @@ -249,7 +252,7 @@ protected function validate(ApiServiceInterface $service)
foreach ($violations as $violation) {
$errorMessage = $violation->getMessage();
if ($violation->getPropertyPath()) {
$errorMessage = sprintf('Error in field "%s": ', $violation->getPropertyPath()) . $errorMessage;
$errorMessage = sprintf('Error in field "%s": ', $violation->getPropertyPath()).$errorMessage;
}
throw new InvalidApiDataException($errorMessage);
}
Expand Down
18 changes: 13 additions & 5 deletions tests/GenericApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PHPUnit\Framework\TestCase;
use Rafrsr\GenericApi\ApiInterface;
use Rafrsr\GenericApi\ApiRequestBuilder;
use Rafrsr\GenericApi\Event\OnResponseEvent;
use Rafrsr\GenericApi\GenericApi;
use Rafrsr\GenericApi\GenericApiMock;
use Rafrsr\GenericApi\GenericApiService;
Expand All @@ -40,16 +41,23 @@ public function testGenericApi()
$api = new GenericApi(ApiInterface::MODE_MOCK); //can use MODE_LIVE

$mockCallback = function () {
return new Response(200, [], file_get_contents(__DIR__ . '/../sample/Fixtures/post1.json'));
return new Response(200, [], file_get_contents(__DIR__.'/../sample/Fixtures/post1.json'));
};

$request = ApiRequestBuilder::create()
->withMethod('get')
->withUri('http://jsonplaceholder.typicode.com/posts/1')
->withMock(new GenericApiMock($mockCallback))
->getRequest();
->withMethod('get')
->withUri('http://jsonplaceholder.typicode.com/posts/1')
->withMock(new GenericApiMock($mockCallback))
->getRequest();

$service = new GenericApiService($request);

$api->onResponse(
function (OnResponseEvent $event) {
self::assertLessThan(10, $event->getExecutionTime());
}
);

$response = $api->process($service);

static::assertEquals('200', $response->getStatusCode());
Expand Down

0 comments on commit bd6b136

Please sign in to comment.