-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Console][FrameworkBundle][HttpKernel][WebProfilerBundle] Enable profiling commands #47416
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
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
144 changes: 144 additions & 0 deletions
144
src/Symfony/Bundle/FrameworkBundle/EventListener/ConsoleProfilerListener.php
This file contains hidden or 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,144 @@ | ||
<?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', 4096], | ||
ConsoleEvents::ERROR => ['catch', -2048], | ||
ConsoleEvents::TERMINATE => ['profile', -4096], | ||
]; | ||
} | ||
|
||
public function initialize(ConsoleCommandEvent $event): void | ||
{ | ||
if (!$event->getInput()->getOption('profile')) { | ||
$this->profiler->disable(); | ||
|
||
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(); | ||
} | ||
|
||
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</>', | ||
HeahDude marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$this->urlGenerator->generate('_profiler', ['token' => $token], UrlGeneratorInterface::ABSOLUTE_URL), | ||
$token | ||
)); | ||
} | ||
|
||
$this->profiles = new \SplObjectStorage(); | ||
$this->parents = new \SplObjectStorage(); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.