8000 [FrameworkBundle][HttpKernel] Display warmers duration on debug verbosity for `cache:clear` command by alexandre-daubois · Pull Request #49139 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle][HttpKernel] Display warmers duration on debug verbosity for cache:clear command #49139

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
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
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)) {
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\HttpKernel\CacheWarmer;

use Symfony\Component\Console\Style\SymfonyStyle;

/**
* Aggregates several cache warmers into a single one.
*
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
0