Skip to content

Commit e051bb5

Browse files
alexandre-dauboisnicolas-grekas
authored andcommitted
[FrameworkBundle][HttpKernel] Display warmers duration on debug verbosity for cache:clear command
1 parent 9368112 commit e051bb5

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

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.2fms.', $warmer::class, 1000 * (microtime(true) - $start)));
108+
}
102109
}
103110
} finally {
104111
if ($collectDeprecations) {

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)