-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Console][FrameworkBundle][HttpKernel][WebProfilerBundle] Enable prof…
…iling commands
- Loading branch information
1 parent
c6930e3
commit 343a318
Showing
34 changed files
with
1,609 additions
and
155 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
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
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
58 changes: 58 additions & 0 deletions
58
src/Symfony/Bundle/FrameworkBundle/Debug/TraceableEventDispatcher.php
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,58 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Debug; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\Console\Debug\CliRequest; | ||
use Symfony\Component\Console\Event\ConsoleCommandEvent; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher as BaseTraceableEventDispatcher; | ||
use Symfony\Component\Stopwatch\Stopwatch; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @author Jules Pietri <jules@heahprod.com> | ||
*/ | ||
final class TraceableEventDispatcher extends BaseTraceableEventDispatcher | ||
{ | ||
public function __construct( | ||
EventDispatcherInterface $dispatcher, | ||
Stopwatch $stopwatch, | ||
LoggerInterface $logger = null, | ||
private readonly ?RequestStack $requestStack = null | ||
) { | ||
parent::__construct($dispatcher, $stopwatch, $logger, $requestStack); | ||
} | ||
|
||
/** | ||
* Starts a stopwatch section before a command runs to profile listeners. | ||
*/ | ||
protected function beforeDispatch(string $eventName, object $event): void | ||
{ | ||
if (!$event instanceof ConsoleCommandEvent) { | ||
parent::beforeDispatch($eventName, $event); | ||
|
||
return; | ||
} | ||
|
||
$request = $this->requestStack->getCurrentRequest(); | ||
|
||
if (!$request instanceof CliRequest || $request->command !== $event->getCommand()) { | ||
return; | ||
} | ||
|
||
$request->attributes->set('_stopwatch_token', substr(hash('sha256', uniqid(mt_rand(), true)), 0, 6)); | ||
$this->stopwatch->openSection(); // section is closed on console.terminate by ConsoleProfilerListener | ||
} | ||
} |
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
133 changes: 133 additions & 0 deletions
133
src/Symfony/Bundle/FrameworkBundle/EventListener/ConsoleProfilerListener.php
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,133 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\EventListener; | ||
|
||
use Symfony\Component\Console\ConsoleEvents; | ||
use Symfony\Component\Console\Debug\CliRequest; | ||
use Symfony\Component\Console\Event\ConsoleCommandEvent; | ||
use Symfony\Component\Console\Event\ConsoleErrorEvent; | ||
use Symfony\Component\Console\Event\ConsoleTerminateEvent; | ||
use Symfony\Component\Console\Output\ConsoleOutputInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\HttpKernel\Profiler\Profile; | ||
use Symfony\Component\HttpKernel\Profiler\Profiler; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
use Symfony\Component\Stopwatch\Stopwatch; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @author Jules Pietri <jules@heahprod.com> | ||
*/ | ||
final class ConsoleProfilerListener implements EventSubscriberInterface | ||
{ | ||
private ?\Throwable $error = null; | ||
/** @var \SplObjectStorage<Request, Profile> */ | ||
private \SplObjectStorage $profiles; | ||
/** @var \SplObjectStorage<Request, ?Request> */ | ||
private \SplObjectStorage $parents; | ||
|
||
public function __construct( | ||
private readonly Profiler $profiler, | ||
private readonly RequestStack $requestStack, | ||
private readonly Stopwatch $stopwatch, | ||
private readonly UrlGeneratorInterface $urlGenerator, | ||
) { | ||
$this->profiles = new \SplObjectStorage(); | ||
$this->parents = new \SplObjectStorage(); | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
ConsoleEvents::COMMAND => ['initialize', 2048], | ||
ConsoleEvents::ERROR => ['catch', -2048], | ||
ConsoleEvents::TERMINATE => ['profile', -2048], | ||
]; | ||
} | ||
|
||
public function initialize(ConsoleCommandEvent $event): void | ||
{ | ||
if (!$event->getInput()->getOption('profile')) { | ||
$this->profiler->disable(); | ||
} | ||
} | ||
|
||
public function catch(ConsoleErrorEvent $event): void | ||
{ | ||
$this->error = $event->getError(); | ||
} | ||
|
||
public function profile(ConsoleTerminateEvent $event): void | ||
{ | ||
if (!$this->profiler->isEnabled()) { | ||
return; | ||
} | ||
|
||
$request = $this->requestStack->getCurrentRequest(); | ||
|
||
if (!$request instanceof CliRequest || $request->command !== $event->getCommand()) { | ||
return; | ||
} | ||
|
||
if (null !== $sectionId = $request->attributes->get('_stopwatch_token')) { | ||
// we must close the section before saving the profile to allow late collect | ||
try { | ||
$this->stopwatch->stopSection($sectionId); | ||
} catch (\LogicException) { | ||
// noop | ||
} | ||
} | ||
|
||
$request->command->exitCode = $event->getExitCode(); | ||
$request->command->interruptedBySignal = $event->getInterruptingSignal(); | ||
|
||
$profile = $this->profiler->collect($request, $request->getResponse(), $this->error); | ||
$this->error = null; | ||
$this->profiles[$request] = $profile; | ||
|
||
if ($this->parents[$request] = $this->requestStack->getParentRequest()) { | ||
// do not save on sub commands | ||
return; | ||
} | ||
|
||
// attach children to parents | ||
foreach ($this->profiles as $request) { | ||
if (null !== $parentRequest = $this->parents[$request]) { | ||
if (isset($this->profiles[$parentRequest])) { | ||
$this->profiles[$parentRequest]->addChild($this->profiles[$request]); | ||
} | ||
} | ||
} | ||
|
||
$output = $event->getOutput(); | ||
$output = $output instanceof ConsoleOutputInterface && $output->isVerbose() ? $output->getErrorOutput() : null; | ||
|
||
// save profiles | ||
foreach ($this->profiles as $r) { | ||
$p = $this->profiles[$r]; | ||
$this->profiler->saveProfile($p); | ||
|
||
$token = $p->getToken(); | ||
$output?->writeln(sprintf( | ||
'See profile <href=%s>%s</>', | ||
$this->urlGenerator->generate('_profiler', ['token' => $token], UrlGeneratorInterface::ABSOLUTE_URL), | ||
$token | ||
)); | ||
} | ||
|
||
$this->profiles = new \SplObjectStorage(); | ||
$this->parents = new \SplObjectStorage(); | ||
} | ||
} |
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.