Skip to content

Extend profiler toolbar item #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 30, 2017
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

- The real request method and target url are now displayed in the profiler.
- Support the cache plugin configuration for `respect_response_cache_directives`.
- Extended WebProfilerToolbar item to list request with details.

### Changed

- The profiler design has been updated.
- Removed stopwatch-plugin in favor of `ProfileClient`.

### Deprecated

Expand Down
10 changes: 10 additions & 0 deletions Collector/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,14 @@ public function getClientStacks($client)
return $stack->getClient() == $client;
});
}

/**
* @return int
*/
public function getTotalDuration()
{
return array_reduce($this->data['stacks'], function ($carry, Stack $stack) {
return $carry + $stack->getDuration();
}, 0);
}
}
76 changes: 61 additions & 15 deletions Collector/ProfileClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Http\Client\HttpClient;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Component\Stopwatch\StopwatchEvent;

/**
* The ProfileClient decorates any client that implement both HttpClient and HttpAsyncClient interfaces to gather target
Expand All @@ -34,13 +36,24 @@ class ProfileClient implements HttpClient, HttpAsyncClient
*/
private $formatter;

/**
* @var Stopwatch
*/
private $stopwatch;

/**
* @var array
*/
private $eventNames = [];

/**
* @param HttpClient|HttpAsyncClient $client The client to profile. Client must implement both HttpClient and
* HttpAsyncClient interfaces.
* @param Collector $collector
* @param Formatter $formatter
* @param Stopwatch $stopwatch
*/
public function __construct($client, Collector $collector, Formatter $formatter)
public function __construct($client, Collector $collector, Formatter $formatter, Stopwatch $stopwatch)
{
if (!($client instanceof HttpClient && $client instanceof HttpAsyncClient)) {
throw new \RuntimeException(sprintf(
Expand All @@ -54,6 +67,7 @@ public function __construct($client, Collector $collector, Formatter $formatter)
$this->client = $client;
$this->collector = $collector;
$this->formatter = $formatter;
$this->stopwatch = $stopwatch;
}

/**
Expand All @@ -63,16 +77,21 @@ public function sendAsyncRequest(RequestInterface $request)
{
$stack = $this->collector->getCurrentStack();
$this->collectRequestInformations($request, $stack);
$event = $this->stopwatch->start($this->getStopwatchEventName($request));

return $this->client->sendAsyncRequest($request)->then(function (ResponseInterface $response) use ($stack) {
$this->collectResponseInformations($response, $stack);
return $this->client->sendAsyncRequest($request)->then(
function (ResponseInterface $response) use ($event, $stack) {
$event->stop();
$this->collectResponseInformations($response, $event, $stack);

return $response;
}, function (\Exception $exception) use ($stack) {
$this->collectExceptionInformations($exception, $stack);
return $response;
}, function (\Exception $exception) use ($event, $stack) {
$event->stop();
$this->collectExceptionInformations($exception, $event, $stack);

throw $exception;
});
throw $exception;
}
);
}

/**
Expand All @@ -82,15 +101,18 @@ public function sendRequest(RequestInterface $request)
{
$stack = $this->collector->getCurrentStack();
$this->collectRequestInformations($request, $stack);
$event = $this->stopwatch->start($this->getStopwatchEventName($request));

try {
$response = $this->client->sendRequest($request);
$event->stop();

$this->collectResponseInformations($response, $stack);
$this->collectResponseInformations($response, $event, $stack);

return $response;
} catch (\Exception $e) {
$this->collectExceptionInformations($e, $stack);
$event->stop();
$this->collectExceptionInformations($e, $event, $stack);

throw $e;
}
Expand All @@ -115,32 +137,56 @@ private function collectRequestInformations(RequestInterface $request, Stack $st

/**
* @param ResponseInterface $response
* @param StopwatchEvent $event
* @param Stack|null $stack
*/
private function collectResponseInformations(ResponseInterface $response, Stack $stack = null)
private function collectResponseInformations(ResponseInterface $response, StopwatchEvent $event, Stack $stack = null)
{
if (!$stack) {
return;
}

$stack->setDuration($event->getDuration());
$stack->setResponseCode($response->getStatusCode());
$stack->setClientResponse($this->formatter->formatResponse($response));
}

/**
* @param \Exception $exception
* @param Stack|null $stack
* @param \Exception $exception
* @param StopwatchEvent $event
* @param Stack|null $stack
*/
private function collectExceptionInformations(\Exception $exception, Stack $stack = null)
private function collectExceptionInformations(\Exception $exception, StopwatchEvent $event, Stack $stack = null)
{
if ($exception instanceof HttpException) {
$this->collectResponseInformations($exception->getResponse(), $stack);
$this->collectResponseInformations($exception->getResponse(), $event, $stack);
}

if (!$stack) {
return;
}

$stack->setDuration($event->getDuration());
$stack->setClientException($this->formatter->formatException($exception));
}

/**
* Generates the event name.
*
* @param RequestInterface $request
*
* @return string
*/
private function getStopwatchEventName(RequestInterface $request)
{
$name = sprintf('%s %s', $request->getMethod(), $request->getUri());

if (isset($this->eventNames[$name])) {
$name .= sprintf(' [#%d]', ++$this->eventNames[$name]);
} else {
$this->eventNames[$name] = 1;
}

return $name;
}
}
12 changes: 10 additions & 2 deletions Collector/ProfileClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\HttplugBundle\ClientFactory\ClientFactory;
use Symfony\Component\Stopwatch\Stopwatch;

/**
* The ProfileClientFactory decorates any ClientFactory and returns the created client decorated by a ProfileClient.
Expand All @@ -31,19 +32,26 @@ class ProfileClientFactory implements ClientFactory
*/
private $formatter;

/**
* @var Stopwatch
*/
private $stopwatch;

/**
* @param ClientFactory|callable $factory
* @param Collector $collector
* @param Formatter $formatter
* @param Stopwatch $stopwatch
*/
public function __construct($factory, Collector $collector, Formatter $formatter)
public function __construct($factory, Collector $collector, Formatter $formatter, Stopwatch $stopwatch)
{
if (!$factory instanceof ClientFactory && !is_callable($factory)) {
throw new \RuntimeException(sprintf('First argument to ProfileClientFactory::__construct must be a "%s" or a callable.', ClientFactory::class));
}
$this->factory = $factory;
$this->collector = $collector;
$this->formatter = $formatter;
$this->stopwatch = $stopwatch;
}

/**
Expand All @@ -57,6 +65,6 @@ public function createClient(array $config = [])
$client = new FlexibleHttpClient($client);
}

return new ProfileClient($client, $this->collector, $this->formatter);
return new ProfileClient($client, $this->collector, $this->formatter, $this->stopwatch);
}
}
21 changes: 21 additions & 0 deletions Collector/Stack.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ final class Stack
*/
private $responseCode;

/**
* @var int
*/
private $duration = 0;

/**
* @param string $client
* @param string $request
Expand Down Expand Up @@ -277,4 +282,20 @@ public function setRequestScheme($requestScheme)
{
$this->requestScheme = $requestScheme;
}

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

/**
* @param int $duration
*/
public function setDuration($duration)
{
$this->duration = $duration;
}
}
6 changes: 1 addition & 5 deletions DependencyInjection/HttplugExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,6 @@ private function configureClient(ContainerBuilder $container, $clientName, array

$pluginClientOptions = [];
if ($profiling) {
// Add the stopwatch plugin
if (!in_array('httplug.plugin.stopwatch', $arguments['plugins'])) {
array_unshift($plugins, 'httplug.plugin.stopwatch');
}

//Decorate each plugin with a ProfilePlugin instance.
foreach ($plugins as $pluginServiceId) {
$this->decoratePluginWithProfilePlugin($container, $pluginServiceId);
Expand Down Expand Up @@ -561,6 +556,7 @@ private function configureAutoDiscoveryFactory(ContainerBuilder $container, $dis
$factory,
new Reference('httplug.collector.collector'),
new Reference('httplug.collector.formatter'),
new Reference('debug.stopwatch'),
]);
$factory = new Reference($factoryServiceId);
}
Expand Down
6 changes: 6 additions & 0 deletions Resources/config/data-collector.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,37 @@
<argument type="service" id="httplug.collector.factory.buzz.inner"/>
<argument type="service" id="httplug.collector.collector"/>
<argument type="service" id="httplug.collector.formatter"/>
<argument type="service" id="debug.stopwatch"/>
</service>
<service id="httplug.collector.factory.curl" class="Http\HttplugBundle\Collector\ProfileClientFactory" decorates="httplug.factory.curl" public="false">
<argument type="service" id="httplug.collector.factory.curl.inner"/>
<argument type="service" id="httplug.collector.collector"/>
<argument type="service" id="httplug.collector.formatter"/>
<argument type="service" id="debug.stopwatch"/>
</service>
<service id="httplug.collector.factory.guzzle5" class="Http\HttplugBundle\Collector\ProfileClientFactory" decorates="httplug.factory.guzzle5" public="false">
<argument type="service" id="httplug.collector.factory.guzzle5.inner"/>
<argument type="service" id="httplug.collector.collector"/>
<argument type="service" id="httplug.collector.formatter"/>
<argument type="service" id="debug.stopwatch"/>
</service>
<service id="httplug.collector.factory.guzzle6" class="Http\HttplugBundle\Collector\ProfileClientFactory" decorates="httplug.factory.guzzle6" public="false">
<argument type="service" id="httplug.collector.factory.guzzle6.inner"/>
<argument type="service" id="httplug.collector.collector"/>
<argument type="service" id="httplug.collector.formatter"/>
<argument type="service" id="debug.stopwatch"/>
</service>
<service id="httplug.collector.factory.react" class="Http\HttplugBundle\Collector\ProfileClientFactory" decorates="httplug.factory.react" public="false">
<argument type="service" id="httplug.collector.factory.react.inner"/>
<argument type="service" id="httplug.collector.collector"/>
<argument type="service" id="httplug.collector.formatter"/>
<argument type="service" id="debug.stopwatch"/>
</service>
<service id="httplug.collector.factory.socket" class="Http\HttplugBundle\Collector\ProfileClientFactory" decorates="httplug.factory.socket" public="false">
<argument type="service" id="httplug.collector.factory.socket.inner"/>
<argument type="service" id="httplug.collector.collector"/>
<argument type="service" id="httplug.collector.formatter"/>
<argument type="service" id="debug.stopwatch"/>
</service>
</services>
</container>
32 changes: 27 additions & 5 deletions Resources/views/webprofiler.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,39 @@
{% set icon %}
{{ include('@Httplug/Icon/httplug.svg') }}
<span class="sf-toolbar-value">{{ collector.stacks|length }}</span>
<span class="sf-toolbar-label">req.</span>
<span class="sf-toolbar-label">in</span>
<span class="sf-toolbar-value">{{ collector.totalDuration|number_format }}</span>
<span class="sf-toolbar-label">ms</span>
{% endset %}

{% set text %}
<div class="sf-toolbar-info-piece">
<b>Successful requests</b>
<span class="sf-toolbar-status">{{ collector.successfulStacks|length }}</span>
<b>{{ collector.stacks|length }} requests</b>
</div>
<div class="sf-toolbar-info-piece">
<b>Failed requests</b>
<span class="sf-toolbar-status {{ collector.failedStacks|length ? 'sf-toolbar-status-red' }}">{{ collector.failedStacks|length }}</span>
<table class="sf-toolbar-ajax-requests">
<thead>
<tr>
<th>Client</th>
<th>Method</th>
<th>Target</th>
<th>Time</th>
<th>Status</th>
</tr>
</thead>
<tbody class="sf-toolbar-ajax-request-list">
{% for stack in collector.stacks %}
<tr>
<td>{{ stack.client }}</td>
<td>{{ stack.requestMethod }}</td>
{% set target = stack.requestTarget %}
<td title="{{ target }}">{{ target|length > 30 ? target[:30] ~ '...' : target }}</td>
<td>{{ stack.duration == 0 ? 'n/a' : stack.duration|number_format ~ ' ms'}}</td>
<td>{{ stack.failed ? 'FAILED' : stack.responseCode|default('n/a') }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endset %}

Expand Down
Loading