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

Skip to content

Commit e23e254

Browse files
[FrameworkBundle][HttpKernel] Display warmers duration on debug verbosity for cache:clear command
1 parent 624fd95 commit e23e254

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ CHANGELOG
1414
* Allow setting private services with the test container
1515
* Register alias for argument for workflow services with workflow name only
1616
* Configure the `ErrorHandler` on `FrameworkBundle::boot()`
17+
* Display warmers duration on debug verbosity for `cache:clear` command
1718

1819
6.2
1920
---

src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
132132
$warmer = $kernel->getContainer()->get('cache_warmer');
133133
// non optional warmers already ran during container compilation
134134
$warmer->enableOnlyOptionalWarmers();
135-
$preload = (array) $warmer->warmUp($realCacheDir);
135+
$preload = (array) $warmer->warmUp($realCacheDir, $io);
136136

137137
if ($preload && file_exists($preloadFile = $realCacheDir.'/'.$kernel->getContainer()->getParameter('kernel.container_class').'.preload.php')) {
138138
Preloader::append($preloadFile, $preload);
@@ -145,7 +145,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
145145
if ($output->isVerbose()) {
146146
$io->comment('Warming up cache...');
147147
}
148-
$this->warmup($warmupDir, $realCacheDir, !$input->getOption('no-optional-warmers'));
148+
$this->warmup($io, $warmupDir, $realCacheDir, !$input->getOption('no-optional-warmers'));
149149
}
150150

151151
if (!$fs->exists($warmupDir.'/'.$containerDir)) {
@@ -219,7 +219,7 @@ private function isNfs(string $dir): bool
219219
return false;
220220
}
221221

222-
private function warmup(string $warmupDir, string $realBuildDir, bool $enableOptionalWarmers = true): void
222+
private function warmup(SymfonyStyle $io, string $warmupDir, string $realBuildDir, bool $enableOptionalWarmers = true): void
223223
{
224224
// create a temporary kernel
225225
$kernel = $this->getApplication()->getKernel();
@@ -233,7 +233,7 @@ private function warmup(string $warmupDir, string $realBuildDir, bool $enableOpt
233233
$warmer = $kernel->getContainer()->get('cache_warmer');
234234
// non optional warmers already ran during container compilation
235235
$warmer->enableOnlyOptionalWarmers();
236-
$preload = (array) $warmer->warmUp($warmupDir);
236+
$preload = (array) $warmer->warmUp($warmupDir, $io);
237237

238238
if ($preload && file_exists($preloadFile = $warmupDir.'/'.$kernel->getContainer()->getParameter('kernel.container_class').'.preload.php')) {
239239
Preloader::append($preloadFile, $preload);

src/Symfony/Component/HttpKernel/CacheWarmer/CacheWarmerAggregate.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\HttpKernel\CacheWarmer;
1313

14+
use Symfony\Component\Console\Style\SymfonyStyle;
15+
1416
/**
1517
* Aggregates several cache warmers into a single one.
1618
*
@@ -46,7 +48,7 @@ public function enableOnlyOptionalWarmers(): void
4648
$this->onlyOptionalsEnabled = $this->optionalsEnabled = true;
4749
}
4850

49-
public function warmUp(string $cacheDir): array
51+
public function warmUp(string $cacheDir, SymfonyStyle $io = null): array
5052
{
5153
if ($collectDeprecations = $this->debug && !\defined('PHPUNIT_COMPOSER_INSTALL')) {
5254
$collectedLogs = [];
@@ -93,12 +95,17 @@ public function warmUp(string $cacheDir): array
9395
continue;
9496
}
9597

98+
$start = microtime(true);
9699
foreach ((array) $warmer->warmUp($cacheDir) as $item) {
97100
if (is_dir($item) || (str_starts_with($item, \dirname($cacheDir)) && !is_file($item))) {
98101
throw new \LogicException(sprintf('"%s::warmUp()" should return a list of files or classes but "%s" is none of them.', $warmer::class, $item));
99102
}
100103
$preload[] = $item;
101104
}
105+
106+
if ($io?->isDebug()) {
107+
$io->info(sprintf('"%s" completed in %0.2f ms.', $warmer::class, 1000 * (microtime(true) - $start)));
108+
}
102109
}
103110
} finally {
104111
if ($collectDeprecations) {

src/Symfony/Component/HttpKernel/Tests/CacheWarmer/CacheWarmerAggregateTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\HttpKernel\Tests\CacheWarmer;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Console\Style\SymfonyStyle;
1516
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate;
1617
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
1718

@@ -91,4 +92,54 @@ public function testWarmupChecksInvalidFiles()
9192
$this->expectException(\LogicException::class);
9293
$aggregate->warmUp(__DIR__);
9394
}
95+
96+
public function testWarmupWhenDebugDisplaysWarmupDuration()
97+
{
98+
$warmer = $this->createMock(CacheWarmerInterface::class);
99+
$io = $this->createMock(SymfonyStyle::class);
100+
101+
$io
102+
->expects($this->once())
103+
->method('isDebug')
104+
->willReturn(true)
105+
;
106+
107+
$io
108+
->expects($this->once())
109+
->method('info')
110+
->with($this->matchesRegularExpression('/"(.+)" completed in (.+) ms\./'))
111+
;
112+
113+
$warmer
114+
->expects($this->once())
115+
->method('warmUp');
116+
117+
$aggregate = new CacheWarmerAggregate([$warmer]);
118+
$aggregate->warmUp(__DIR__, $io);
119+
}
120+
121+
public function testWarmupWhenNotDebugDoesntDisplayWarmupDuration()
122+
{
123+
$warmer = $this->createMock(CacheWarmerInterface::class);
124+
$io = $this->createMock(SymfonyStyle::class);
125+
126+
$io
127+
->expects($this->once())
128+
->method('isDebug')
129+
->willReturn(false)
130+
;
131+
132+
$io
133+
->expects($this->never())
134+
->method('info')
135+
->with($this->matchesRegularExpression('/"(.+)" completed in (.+) ms\./'))
136+
;
137+
138+
$warmer
139+
->expects($this->once())
140+
->method('warmUp');
141+
142+
$aggregate = new CacheWarmerAggregate([$warmer]);
143+
$aggregate->warmUp(__DIR__, $io);
144+
}
94145
}

0 commit comments

Comments
 (0)