-
-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
1,052 additions
and
29 deletions.
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,91 @@ | ||
<?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; | ||
|
||
final class DataCollector extends BaseDataCollector implements LateDataCollectorInterface | ||
{ | ||
private $eventDispatcher; | ||
private $handler; | ||
private $metadataDriver; | ||
|
||
public function __construct( | ||
TraceableEventDispatcher $eventDispatcher, | ||
TraceableHandlerRegistry $handler, | ||
TraceableDriver $metadataDriver | ||
) { | ||
$this->eventDispatcher = $eventDispatcher; | ||
$this->handler = $handler; | ||
$this->metadataDriver = $metadataDriver; | ||
|
||
$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'] = []; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return 'jms_serializer'; | ||
} | ||
|
||
public function addTriggeredEvent(array $call): void | ||
{ | ||
$this->data['listeners'][] = $call; | ||
} | ||
|
||
public function getTriggeredListeners(): array | ||
{ | ||
return $this->data['listeners']['called']; | ||
} | ||
|
||
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 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 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,60 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace JMS\SerializerBundle\Debug; | ||
|
||
use JMS\Serializer\Metadata\Driver\XmlDriver; | ||
use Metadata\Cache\CacheInterface; | ||
use Metadata\ClassMetadata; | ||
use Metadata\Driver\DriverInterface; | ||
|
||
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,107 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JMS\SerializerBundle\Debug; | ||
|
||
use JMS\Serializer\EventDispatcher\Event; | ||
use JMS\Serializer\EventDispatcher\EventDispatcher; | ||
use JMS\Serializer\EventDispatcher\EventDispatcherInterface; | ||
use JMS\Serializer\EventDispatcher\EventSubscriberInterface; | ||
use JMS\Serializer\EventDispatcher\LazyEventDispatcher; | ||
use Psr\Container\ContainerInterface; | ||
|
||
final class TraceableEventDispatcher extends LazyEventDispatcher | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $storage = []; | ||
|
||
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')); | ||
} | ||
} |
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,100 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace JMS\SerializerBundle\Debug; | ||
|
||
use JMS\Serializer\Handler\HandlerRegistryInterface; | ||
use JMS\Serializer\Handler\SubscribingHandlerInterface; | ||
|
||
final class TraceableHandlerRegistry implements HandlerRegistryInterface | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $storage = []; | ||
private $registry; | ||
|
||
public function __construct(HandlerRegistryInterface $registry) | ||
{ | ||
$this->registry = $registry; | ||
} | ||
|
||
public function registerSubscribingHandler(SubscribingHandlerInterface $handler): void | ||
{ | ||
$this->registerSubscribingHandler($handler); | ||
} | ||
|
||
public function registerHandler(int $direction, string $typeName, string $format, $handler): void | ||
{ | ||
$this->registry->registerHandler($direction, $typeName, $format, $handler); | ||
} | ||
|
||
public function getHandler(int $direction, string $typeName, string $format) | ||
{ | ||
$handler = $this->registry->getHandler($direction, $typeName, $format); | ||
if ($handler=== null) { | ||
return null; | ||
} | ||
return function (...$args) use($handler, $direction, $typeName, $format) { | ||
try{ | ||
$t = microtime(true); | ||
return call_user_func_array($handler, $args); | ||
} finally { | ||
$this->storage[$direction][$typeName][]= [ | ||
'handler' => $handler, | ||
'format' => $format, | ||
'duration' => microtime(true) - $t | ||
]; | ||
} | ||
|
||
}; | ||
} | ||
|
||
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 getTriggeredHandlers(): array | ||
{ | ||
$result = []; | ||
|
||
foreach ($this->storage as $direction => $handlersByType) { | ||
foreach ($handlersByType as $type => $calls) { | ||
foreach ($calls as $call) { | ||
$handlerName = $this->findNameForListener($call['handler']); | ||
if (!isset($result[$direction][$type][$handlerName])){ | ||
$result[$direction][$type][$handlerName] = [ | ||
'calls' => 0, | ||
'duration' => 0, | ||
]; | ||
} | ||
$result[$direction][$type][$handlerName] = [ | ||
'handler' => $handlerName, | ||
'calls' => $result[$direction][$type][$handlerName]['calls']+1, | ||
'duration' => $result[$direction][$type][$handlerName]['duration']+$call['duration'], | ||
]; | ||
} | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
public function getNotTriggeredHandlers(): array | ||
{ | ||
return []; | ||
$result = []; | ||
|
||
foreach ($this->getTraceableHandlers() as $info) { | ||
if (!$info['calls']) { | ||
$result[$info['direction']][] = $info; | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
} |
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
Oops, something went wrong.