-
-
Notifications
You must be signed in to change notification settings - Fork 313
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
Add symfony profiler panel #858
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JMS\SerializerBundle\Debug; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\DataCollector\DataCollector as BaseDataCollector; | ||
use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class DataCollector extends BaseDataCollector implements LateDataCollectorInterface | ||
{ | ||
private $eventDispatcher; | ||
private $handler; | ||
private $metadataDriver; | ||
private $locator; | ||
private $loadedDirs; | ||
private $runsListener; | ||
|
||
public function __construct( | ||
array $loadedDirs, | ||
TraceableEventDispatcher $eventDispatcher, | ||
TraceableHandlerRegistry $handler, | ||
TraceableDriver $metadataDriver, | ||
TraceableFileLocator $locator, | ||
RunsListener $runsListener | ||
) { | ||
$this->eventDispatcher = $eventDispatcher; | ||
$this->handler = $handler; | ||
$this->metadataDriver = $metadataDriver; | ||
$this->locator = $locator; | ||
$this->loadedDirs = $loadedDirs; | ||
$this->runsListener = $runsListener; | ||
|
||
$this->reset(); | ||
} | ||
|
||
public function collect(Request $request, Response $response, \Throwable $exception = null) | ||
{ | ||
} | ||
|
||
public function reset(): void | ||
{ | ||
$this->data['handlers'] = []; | ||
$this->data['metadata'] = []; | ||
$this->data['listeners'] = []; | ||
$this->data['metadata_files'] = []; | ||
$this->data['loaded_dirs'] = []; | ||
$this->data['runs'] = []; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return 'jms_serializer'; | ||
} | ||
|
||
public function getNumListeners($type): int | ||
{ | ||
return array_sum(array_map(function ($l){ | ||
return count($l); | ||
}, $this->data['listeners'][$type])); | ||
} | ||
|
||
public function getNumHandlers($type): int | ||
{ | ||
return array_sum(array_map(function ($l){ | ||
return count($l); | ||
}, $this->data['handlers'][$type])); | ||
} | ||
|
||
public function getTriggeredListeners(): array | ||
{ | ||
return $this->data['listeners']['called']; | ||
} | ||
|
||
public function getRuns($direction): array | ||
{ | ||
return $this->data['runs'][$direction] ?? []; | ||
} | ||
|
||
public function getLoadedDirs(): array | ||
{ | ||
return $this->data['loaded_dirs']; | ||
} | ||
|
||
public function getNotTriggeredListeners(): array | ||
{ | ||
return $this->data['listeners']['not_called']; | ||
} | ||
|
||
public function getTriggeredHandlers(): array | ||
{ | ||
return $this->data['handlers']['called']; | ||
} | ||
|
||
public function getNotTriggeredHandlers(): array | ||
{ | ||
return $this->data['handlers']['not_called']; | ||
} | ||
|
||
public function getLoadedMetadata(): array | ||
{ | ||
return $this->data['metadata']; | ||
} | ||
|
||
public function getMetadataFiles(): array | ||
{ | ||
return $this->data['metadata_files']; | ||
} | ||
|
||
public function getTriggeredEvents() | ||
{ | ||
return $this->data['triggered_events']; | ||
} | ||
|
||
public function lateCollect(): void | ||
{ | ||
$this->data['listeners'] = [ | ||
'called' => $this->eventDispatcher->getTriggeredListeners(), | ||
'not_called' => $this->eventDispatcher->getNotTriggeredListeners(), | ||
]; | ||
|
||
|
||
$this->data['handlers'] = [ | ||
'called' => $this->handler->getTriggeredHandlers(), | ||
'not_called' => $this->handler->getNotTriggeredHandlers(), | ||
]; | ||
|
||
$this->data['metadata'] = $this->metadataDriver->getLoadedMetadata(); | ||
$this->data['metadata_files'] = $this->locator->getAttemptedFiles(); | ||
$this->data['loaded_dirs'] = $this->loadedDirs; | ||
$this->data['runs'] = $this->runsListener->getRuns(); | ||
$this->data['triggered_events'] = $this->eventDispatcher->getTriggeredEvents(); | ||
ksort($this->data['loaded_dirs']); | ||
ksort($this->data['metadata_files']); | ||
ksort($this->data['metadata']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace JMS\SerializerBundle\Debug; | ||
|
||
use JMS\Serializer\EventDispatcher\Event; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class RunsListener | ||
{ | ||
private $runs = []; | ||
|
||
public function saveRunInfo(Event $event) | ||
{ | ||
$context = $event->getContext(); | ||
$this->runs[$context->getDirection()][spl_object_hash($context)] = true; | ||
} | ||
|
||
public function getRuns(): array | ||
{ | ||
return $this->runs; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace JMS\SerializerBundle\Debug; | ||
|
||
use Metadata\Cache\CacheInterface; | ||
use Metadata\ClassMetadata; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class TraceableDriver implements CacheInterface | ||
{ | ||
/** | ||
* @var CacheInterface | ||
*/ | ||
private $driver; | ||
private $storage = []; | ||
|
||
public function __construct(CacheInterface $driver) | ||
{ | ||
|
||
$this->driver = $driver; | ||
} | ||
|
||
public function getLoadedMetadata() | ||
{ | ||
return $this->storage; | ||
} | ||
|
||
public function load($class): ?ClassMetadata | ||
{ | ||
try{ | ||
return $metadata = $this->driver->load($class); | ||
} finally { | ||
if ($metadata){ | ||
$this->trackMetadata($metadata); | ||
} | ||
} | ||
|
||
} | ||
|
||
private function trackMetadata(ClassMetadata $metadata): void | ||
{ | ||
$class = $metadata->name; | ||
$this->storage[$class] = array_merge( | ||
$this->storage[$class] ?? [], $metadata->fileResources | ||
); | ||
$this->storage[$class] = array_unique($this->storage[$class]); | ||
} | ||
|
||
public function put(ClassMetadata $metadata): void | ||
{ | ||
$this->driver->put($metadata); | ||
$this->trackMetadata($metadata); | ||
} | ||
|
||
public function evict(string $class): void | ||
{ | ||
$this->driver->evict($class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JMS\SerializerBundle\Debug; | ||
|
||
use JMS\Serializer\EventDispatcher\LazyEventDispatcher; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class TraceableEventDispatcher extends LazyEventDispatcher | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $storage = []; | ||
|
||
public function getTriggeredEvents(): array | ||
{ | ||
$data = [ | ||
'count' => 0, | ||
'duration' => 0 | ||
]; | ||
|
||
foreach ($this->storage as $calledOnTypes) { | ||
foreach ($calledOnTypes as $calls) { | ||
$data['count'] += count($calls); | ||
$data['duration'] += $this->calculateTotalDuration($calls); | ||
} | ||
} | ||
return $data; | ||
} | ||
|
||
public function getTriggeredListeners(): array | ||
{ | ||
$resultsByListener = []; | ||
|
||
foreach ($this->storage as $eventName => $calledOnTypes) { | ||
foreach ($calledOnTypes as $type => $calls) { | ||
foreach ($calls as $call) { | ||
$listener = $this->findNameForListener($call['listener']); | ||
$resultsByListener[$eventName][$listener][$type][] = $call; | ||
} | ||
} | ||
} | ||
|
||
foreach ($resultsByListener as $eventName => $calledOnListeners) { | ||
foreach ($calledOnListeners as $listener => $calledOnTypes) { | ||
foreach ($calledOnTypes as $type => $calls) { | ||
$resultsByListener[$eventName][$listener][$type] = [ | ||
'calls' => count($calls), | ||
'duration' => $this->calculateTotalDuration($calls) | ||
]; | ||
} | ||
} | ||
} | ||
|
||
return $resultsByListener; | ||
} | ||
|
||
private function findNameForListener($listener): string | ||
{ | ||
if (is_array($listener)) { | ||
return (is_string($listener[0]) ? $listener[0] : get_class($listener[0])) . '::' . $listener[1]; | ||
} | ||
return 'unknown'; | ||
} | ||
|
||
public function getNotTriggeredListeners(): array | ||
{ | ||
$result = []; | ||
|
||
foreach ($this->getListeners() as $event => $listeners) { | ||
foreach ($listeners as $listener) { | ||
foreach ($this->storage[$event] ?? [] as $calls) { | ||
foreach ($calls as $call) { | ||
if ($call['listener'] == $listener[0]) { | ||
continue 3; | ||
} | ||
} | ||
} | ||
$listenerName = $this->findNameForListener($listener[0]); | ||
$result[$event][$listenerName] = $listenerName; | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function initializeListeners(string $eventName, string $loweredClass, string $format): array | ||
{ | ||
$listeners = parent::initializeListeners($eventName, $loweredClass, $format); | ||
$this->storage = []; | ||
foreach ($listeners as &$listener) { | ||
$listener[0] = $f = function (...$args) use ($listener, &$f) { | ||
$t = microtime(true); | ||
call_user_func_array($listener[0], $args); | ||
|
||
// $args = [$event, $eventName, $class, $format, $dispatcher] | ||
// $listener = [$callable, $class, $format, $interface] | ||
$this->storage[$args[1]][$args[2]][] = [ | ||
'listener' => $listener[0], | ||
'format' => $args[3], | ||
'type' => $args[0]->getType(), | ||
'duration' => microtime(true) - $t | ||
]; | ||
}; | ||
} | ||
|
||
return $listeners; | ||
} | ||
|
||
private function calculateTotalDuration(array $calls): float | ||
{ | ||
return array_sum(array_column($calls, 'duration')) * 1000; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace JMS\SerializerBundle\Debug; | ||
|
||
use Metadata\Driver\FileLocator; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class TraceableFileLocator extends FileLocator | ||
{ | ||
private $files = []; | ||
|
||
public function __construct(array $dirs) | ||
{ | ||
parent::__construct($dirs); | ||
} | ||
|
||
public function getAttemptedFiles() | ||
{ | ||
return $this->files; | ||
} | ||
|
||
protected function loadFileIfFound($prefix, $dir, \ReflectionClass $class, $extension) | ||
{ | ||
$pathData = parent::loadFileIfFound($prefix, $dir, $class, $extension); | ||
|
||
if ($pathData[0] !== null) { | ||
$this->files[$class->getName()][$pathData[0]] = $pathData[1]; | ||
} | ||
return $pathData; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is broken. It doesn't seem to refer to anything in this project, and seems to never be called, thus not logging anything. I fixed it in #894.