Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f3722d5

Browse filesBrowse files
feature #49139 [FrameworkBundle][HttpKernel] Display warmers duration on debug verbosity for cache:clear command (alexandre-daubois)
This PR was merged into the 6.3 branch. Discussion ---------- [FrameworkBundle][HttpKernel] Display warmers duration on debug verbosity for `cache:clear` command | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #48994 | License | MIT | Doc PR | Todo From the idea of the issue it fixes. When dealing with huge code base, the cache/warmup process can be a bit long. It would be convenient for developers to understand where it does take time during the process. Here is the result of `cache:clear` ran with debug verbosity now: <img width="1018" alt="image" src="https://user-images.githubusercontent.com/2144837/215203344-ef108571-4a4a-4979-b26c-c64d6a8eea26.png"> Commits ------- 5791513 [FrameworkBundle][HttpKernel] Display warmers duration on debug verbosity for `cache:clear` command
2 parents 55ba068 + 5791513 commit f3722d5
Copy full SHA for f3722d5

File tree

4 files changed

+64
-5
lines changed
Filter options

4 files changed

+64
-5
lines changed

‎src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ CHANGELOG
1616
* Configure the `ErrorHandler` on `FrameworkBundle::boot()`
1717
* Allow setting `debug.container.dump` to `false` to disable dumping the container to XML
1818
* Add `framework.http_cache.skip_response_headers` option
19+
* Display warmers duration on debug verbosity for `cache:clear` command
1920

2021
6.2
2122
---

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php
+4-4Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/CacheWarmer/CacheWarmerAggregate.php
+8-1Lines 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.2fms.', $warmer::class, 1000 * (microtime(true) - $start)));
108+
}
102109
}
103110
} finally {
104111
if ($collectDeprecations) {

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Tests/CacheWarmer/CacheWarmerAggregateTest.php
+51Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.