Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpKernel] Profiler Listener tweaks #3920

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 2 commits into from
Apr 13, 2012
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
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
</form>

<h3>
Main Request
{{ profile.parent ? "Request" : "Main Request" }}
<small>
- {{ collector.events.__section__.totaltime }} ms
{% if profile.parent %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

use Symfony\Component\HttpKernel\Debug\Stopwatch;
use Symfony\Component\HttpKernel\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Profiler\Profile;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;
Expand All @@ -30,6 +32,7 @@ class ContainerAwareTraceableEventDispatcher extends ContainerAwareEventDispatch
private $called;
private $stopwatch;
private $priorities;
private $profiler;

/**
* Constructor.
Expand Down Expand Up @@ -83,11 +86,18 @@ public function dispatch($eventName, Event $event = null)
case 'kernel.response':
$token = $event->getResponse()->headers->get('X-Debug-Token');
$this->stopwatch->stopSection($token);
$this->updateProfile($token);
if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
// The profiles can only be updated once they have been created
// that is after the 'kernel.response' event of the main request
$this->updateProfiles($token, true);
}
break;
case 'kernel.terminate':
$this->stopwatch->stopSection($token);
$this->updateProfile($token);
// The children profiles have been updated by the previous 'kernel.response'
// event. Only the root profile need to be updated with the 'kernel.terminate'
// timing informations.
$this->updateProfiles($token, false);
break;
}

Expand Down Expand Up @@ -255,28 +265,41 @@ private function getListenerInfo($listener, $eventName)
}

/**
* Updates the profile data.
* Updates the stopwatch data in the profile hierarchy.
*
* @param string $token Profile token
* @param string $token Profile token
* @param Boolean $updateChildren Whether to update the children altogether
*/
private function updateProfile($token)
private function updateProfiles($token, $updateChildren)
{
if (!$this->getContainer()->has('profiler')) {
return;
}

$profiler = $this->getContainer()->get('profiler');
if (!$profile = $profiler->loadProfile($token)) {
$this->profiler = $this->getContainer()->get('profiler');

if (!$profile = $this->profiler->loadProfile($token)) {
return;
}

$profile->getCollector('time')->setEvents($this->stopwatch->getSectionEvents($token));
$profiler->saveProfile($profile);
$this->saveStopwatchInfoInProfile($profile, $updateChildren);
}

// children
foreach ($profile->getChildren() as $child) {
$child->getCollector('time')->setEvents($this->stopwatch->getSectionEvents($child->getToken()));
$profiler->saveProfile($child);
/**
* Update the profiles with the timing info and saves them.
*
* @param Profile $profile The root profile
* @param Boolean $updateChildren Whether to update the children altogether
*/
private function saveStopwatchInfoInProfile(Profile $profile, $updateChildren)
{
$profile->getCollector('time')->setEvents($this->stopwatch->getSectionEvents($profile->getToken()));
$this->profiler->saveProfile($profile);

if ($updateChildren) {
foreach ($profile->getChildren() as $child) {
$this->saveStopwatchInfoInProfile($child, true);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Profiler\Profile;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Expand All @@ -34,6 +35,7 @@ class ProfilerListener implements EventSubscriberInterface
protected $exception;
protected $children;
protected $requests;
protected $profiles;

/**
* Constructor.
Expand All @@ -50,6 +52,7 @@ public function __construct(Profiler $profiler, RequestMatcherInterface $matcher
$this->onlyException = (Boolean) $onlyException;
$this->onlyMasterRequests = (Boolean) $onlyMasterRequests;
$this->children = new \SplObjectStorage();
$this->profiles = array();
}

/**
Expand Down Expand Up @@ -99,6 +102,16 @@ public function onKernelResponse(FilterResponseEvent $event)
return;
}

$this->profiles[] = $profile;

if (null !== $exception) {
foreach ($this->profiles as $profile) {
$this->profiler->saveProfile($profile);
}

return;
}

// keep the profile as the child of its parent
if (!$master) {
array_pop($this->requests);
Expand All @@ -109,17 +122,16 @@ public function onKernelResponse(FilterResponseEvent $event)
$this->children[$parent] = $profiles;
}

// store the profile and its children
if (isset($this->children[$request])) {
foreach ($this->children[$request] as $child) {
$child->setParent($profile);
$profile->addChild($child);
$this->profiler->saveProfile($child);
}
$this->children[$request] = array();
}

$this->profiler->saveProfile($profile);
if ($master) {
$this->saveProfiles($profile);
}
}

static public function getSubscribedEvents()
Expand All @@ -128,9 +140,21 @@ static public function getSubscribedEvents()
// kernel.request must be registered as early as possible to not break
// when an exception is thrown in any other kernel.request listener
KernelEvents::REQUEST => array('onKernelRequest', 1024),

KernelEvents::RESPONSE => array('onKernelResponse', -100),
KernelEvents::EXCEPTION => 'onKernelException',
);
}

/**
* Saves the profile hierarchy.
*
* @param Profile $profile The root profile
*/
private function saveProfiles(Profile $profile)
{
$this->profiler->saveProfile($profile);
foreach ($profile->getChildren() as $profile) {
$this->saveProfiles($profile);
}
}
}