From 57915137d2449e91de42dae8e443d58c1f0602dd Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Fri, 27 Jan 2023 22:13:36 +0100 Subject: [PATCH] [FrameworkBundle][HttpKernel] Display warmers duration on debug verbosity for `cache:clear` command --- .../Bundle/FrameworkBundle/CHANGELOG.md | 1 + .../Command/CacheClearCommand.php | 8 +-- .../CacheWarmer/CacheWarmerAggregate.php | 9 +++- .../CacheWarmer/CacheWarmerAggregateTest.php | 51 +++++++++++++++++++ 4 files changed, 64 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index 671253a34f8c2..15f0060b1ca6c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -16,6 +16,7 @@ CHANGELOG * Configure the `ErrorHandler` on `FrameworkBundle::boot()` * Allow setting `debug.container.dump` to `false` to disable dumping the container to XML * Add `framework.http_cache.skip_response_headers` option + * Display warmers duration on debug verbosity for `cache:clear` command 6.2 --- diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php index aa8c47f57166f..612105dfba59c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php @@ -132,7 +132,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $warmer = $kernel->getContainer()->get('cache_warmer'); // non optional warmers already ran during container compilation $warmer->enableOnlyOptionalWarmers(); - $preload = (array) $warmer->warmUp($realCacheDir); + $preload = (array) $warmer->warmUp($realCacheDir, $io); if ($preload && file_exists($preloadFile = $realCacheDir.'/'.$kernel->getContainer()->getParameter('kernel.container_class').'.preload.php')) { Preloader::append($preloadFile, $preload); @@ -145,7 +145,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int if ($output->isVerbose()) { $io->comment('Warming up cache...'); } - $this->warmup($warmupDir, $realCacheDir, !$input->getOption('no-optional-warmers')); + $this->warmup($io, $warmupDir, $realCacheDir, !$input->getOption('no-optional-warmers')); } if (!$fs->exists($warmupDir.'/'.$containerDir)) { @@ -219,7 +219,7 @@ private function isNfs(string $dir): bool return false; } - private function warmup(string $warmupDir, string $realBuildDir, bool $enableOptionalWarmers = true): void + private function warmup(SymfonyStyle $io, string $warmupDir, string $realBuildDir, bool $enableOptionalWarmers = true): void { // create a temporary kernel $kernel = $this->getApplication()->getKernel(); @@ -233,7 +233,7 @@ private function warmup(string $warmupDir, string $realBuildDir, bool $enableOpt $warmer = $kernel->getContainer()->get('cache_warmer'); // non optional warmers already ran during container compilation $warmer->enableOnlyOptionalWarmers(); - $preload = (array) $warmer->warmUp($warmupDir); + $preload = (array) $warmer->warmUp($warmupDir, $io); if ($preload && file_exists($preloadFile = $warmupDir.'/'.$kernel->getContainer()->getParameter('kernel.container_class').'.preload.php')) { Preloader::append($preloadFile, $preload); diff --git a/src/Symfony/Component/HttpKernel/CacheWarmer/CacheWarmerAggregate.php b/src/Symfony/Component/HttpKernel/CacheWarmer/CacheWarmerAggregate.php index e5dba5b77683a..30132921672ca 100644 --- a/src/Symfony/Component/HttpKernel/CacheWarmer/CacheWarmerAggregate.php +++ b/src/Symfony/Component/HttpKernel/CacheWarmer/CacheWarmerAggregate.php @@ -11,6 +11,8 @@ namespace Symfony\Component\HttpKernel\CacheWarmer; +use Symfony\Component\Console\Style\SymfonyStyle; + /** * Aggregates several cache warmers into a single one. * @@ -46,7 +48,7 @@ public function enableOnlyOptionalWarmers(): void $this->onlyOptionalsEnabled = $this->optionalsEnabled = true; } - public function warmUp(string $cacheDir): array + public function warmUp(string $cacheDir, SymfonyStyle $io = null): array { if ($collectDeprecations = $this->debug && !\defined('PHPUNIT_COMPOSER_INSTALL')) { $collectedLogs = []; @@ -93,12 +95,17 @@ public function warmUp(string $cacheDir): array continue; } + $start = microtime(true); foreach ((array) $warmer->warmUp($cacheDir) as $item) { if (is_dir($item) || (str_starts_with($item, \dirname($cacheDir)) && !is_file($item))) { throw new \LogicException(sprintf('"%s::warmUp()" should return a list of files or classes but "%s" is none of them.', $warmer::class, $item)); } $preload[] = $item; } + + if ($io?->isDebug()) { + $io->info(sprintf('"%s" completed in %0.2fms.', $warmer::class, 1000 * (microtime(true) - $start))); + } } } finally { if ($collectDeprecations) { diff --git a/src/Symfony/Component/HttpKernel/Tests/CacheWarmer/CacheWarmerAggregateTest.php b/src/Symfony/Component/HttpKernel/Tests/CacheWarmer/CacheWarmerAggregateTest.php index 563486db15664..fd1e6105808c8 100644 --- a/src/Symfony/Component/HttpKernel/Tests/CacheWarmer/CacheWarmerAggregateTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/CacheWarmer/CacheWarmerAggregateTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\HttpKernel\Tests\CacheWarmer; use PHPUnit\Framework\TestCase; +use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate; use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; @@ -91,4 +92,54 @@ public function testWarmupChecksInvalidFiles() $this->expectException(\LogicException::class); $aggregate->warmUp(__DIR__); } + + public function testWarmupWhenDebugDisplaysWarmupDuration() + { + $warmer = $this->createMock(CacheWarmerInterface::class); + $io = $this->createMock(SymfonyStyle::class); + + $io + ->expects($this->once()) + ->method('isDebug') + ->willReturn(true) + ; + + $io + ->expects($this->once()) + ->method('info') + ->with($this->matchesRegularExpression('/"(.+)" completed in (.+)ms\./')) + ; + + $warmer + ->expects($this->once()) + ->method('warmUp'); + + $aggregate = new CacheWarmerAggregate([$warmer]); + $aggregate->warmUp(__DIR__, $io); + } + + public function testWarmupWhenNotDebugDoesntDisplayWarmupDuration() + { + $warmer = $this->createMock(CacheWarmerInterface::class); + $io = $this->createMock(SymfonyStyle::class); + + $io + ->expects($this->once()) + ->method('isDebug') + ->willReturn(false) + ; + + $io + ->expects($this->never()) + ->method('info') + ->with($this->matchesRegularExpression('/"(.+)" completed in (.+)ms\./')) + ; + + $warmer + ->expects($this->once()) + ->method('warmUp'); + + $aggregate = new CacheWarmerAggregate([$warmer]); + $aggregate->warmUp(__DIR__, $io); + } }