10000 [FrameworkBundle][HttpKernel] Display warmers duration on debug verbo… · symfony/symfony@07b72de · GitHub
[go: up one dir, main page]

Skip to content

Commit 07b72de

Browse files
[FrameworkBundle][HttpKernel] Display warmers duration on debug verbosity for cache:clear command
1 parent a90a733 commit 07b72de

File tree

5 files changed

+86
-18
lines changed

5 files changed

+86
-18
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ CHANGELOG
1313
* Deprecate the `notifier.logger_notification_listener` service, use the `notifier.notification_logger_listener` service instead
1414
* Allow setting private services with the test container
1515
* Register alias for argument for workflow services with workflow name only
16+
* Display warmers duration on debug verbosity for `cache:clear` command
1617

1718
6.2
1819
---

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Symfony\Component\Filesystem\Filesystem;
2525
use Symfony\Component\Finder\Finder;
2626
use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface;
27+
use Symfony\Component\HttpKernel\Event\Cache\CacheWarmerAggregateItemFinishedEvent;
2728
use Symfony\Component\HttpKernel\RebootableInterface;
2829

2930
/**
@@ -37,14 +38,15 @@
3738
#[AsCommand(name: 'cache:clear', description: 'Clear the cache')]
3839
class CacheClearCommand extends Command
3940
{
40-
private CacheClearerInterface $cacheClearer;
4141
private Filesystem $filesystem;
42+
private EventDispatcher $eventDispatcher;
4243

43-
public function __construct(CacheClearerInterface $cacheClearer, Filesystem $filesystem = null)
44-
{
44+
public function __construct(
45+
private readonly CacheClearerInterface $cacheClearer,
46+
Filesystem $filesystem = null
47+
) {
4548
parent::__construct();
4649

47-
$this->cacheClearer = $cacheClearer;
48 E864 50
$this->filesystem = $filesystem ?? new Filesystem();
4951
}
5052

@@ -107,7 +109,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
107109
$this->cacheClearer->clear($realCacheDir);
108110

109111
// The current event dispatcher is stale, let's not use it anymore
110-
$this->getApplication()->setDispatcher(new EventDispatcher());
112+
$this->eventDispatcher = new EventDispatcher();
113+
$this->getApplication()->setDispatcher($this->eventDispatcher);
111114

112115
$containerFile = (new \ReflectionObject($kernel->getContainer()))->getFileName();
113116
$containerDir = basename(\dirname($containerFile));
@@ -121,6 +124,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
121124
}
122125
$fs->remove($warmupDir);
123126

127+
if ($io->isDebug()) {
128+
$this->eventDispatcher->addListener(CacheWarmerAggregateItemFinishedEvent::class, function (CacheWarmerAggregateItemFinishedEvent $event) use ($io) {
129+
$io->info(sprintf('"%s" finished warmup in %.2f ms.', $event->getWarmerClass(), $event->getTime()));
130+
});
131+
}
132+
124133
if ($_SERVER['REQUEST_TIME'] <= filemtime($containerFile) && filemtime($containerFile) <= time()) {
125134
if ($output->isVerbose()) {
126135
$io->comment('Cache is fresh.');
@@ -132,7 +141,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
132141
$warmer = $kernel->getContainer()->get('cache_warmer');
133142
// non optional warmers already ran during container compilation
134143
$warmer->enableOnlyOptionalWarmers();
135-
$preload = (array) $warmer->warmUp($realCacheDir);
144+
$preload = (array) $warmer->warmUp($realCacheDir, $this->eventDispatcher);
136145

137146
if ($preload && file_exists($preloadFile = $realCacheDir.'/'.$kernel->getContainer()->getParameter('kernel.container_class').'.preload.php')) {
138147
Preloader::append($preloadFile, $preload);
@@ -145,7 +154,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
145154
if ($output->isVerbose()) {
146155
$io->comment('Warming up cache...');
147156
}
148-
$this->warmup($warmupDir, $realCacheDir, !$input->getOption('no-optional-warmers'));
157+
$this->warmup($io, $warmupDir, $realCacheDir, !$input->getOption('no-optional-warmers'));
149158
}
150159

151160
if (!$fs->exists($warmupDir.'/'.$containerDir)) {
@@ -219,7 +228,7 @@ private function isNfs(string $dir): bool
219228
return false;
220229
}
221230

222-
private function warmup(string $warmupDir, string $realBuildDir, bool $enableOptionalWarmers = true): void
231+
private function warmup(SymfonyStyle $io, string $warmupDir, string $realBuildDir, bool $enableOptionalWarmers = true): void
223232
{
224233
// create a temporary kernel
225234
$kernel = $this->getApplication()->getKernel();
@@ -233,7 +242,7 @@ private function warmup(string $warmupDir, string $realBuildDir, bool $enableOpt
233242
$warmer = $kernel->getContainer()->get('cache_warmer');
234243
// non optional warmers already ran during container compilation
235244
$warmer->enableOnlyOptionalWarmers();
236-
$preload = (array) $warmer->warmUp($warmupDir);
245+
$preload = (array) $warmer->warmUp($warmupDir, $this->eventDispatcher);
237246

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

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace Symfony\Component\HttpKernel\CacheWarmer;
1313

14+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
15+
use Symfony\Component\HttpKernel\Event\Cache\CacheWarmerAggregateItemFinishedEvent;
16+
1417
/**
1518
* Aggregates several cache warmers into a single one.
1619
*
@@ -20,20 +23,17 @@
2023
*/
2124
class CacheWarmerAggregate implements CacheWarmerInterface
2225
{
23-
private iterable $warmers;
24-
private bool $debug;
25-
private ?string $deprecationLogsFilepath;
2626
private bool $optionalsEnabled = false;
2727
private bool $onlyOptionalsEnabled = false;
2828

2929
/**
3030
* @param iterable<mixed, CacheWarmerInterface> $warmers
3131
*/
32-
public function __construct(iterable $warmers = [], bool $debug = false, string $deprecationLogsFilepath = null)
33-
{
34-
$this->warmers = $warmers;
35-
$this->debug = $debug;
36-
$this->deprecationLogsFilepath = $deprecationLogsFilepath;
32+
public function __construct(
33+
private iterable $warmers = [],
34+
private bool $debug = false,
35+
private ?string $deprecationLogsFilepath = null,
36+
) {
3737
}
3838

3939
public function enableOptionalWarmers(): void
@@ -46,7 +46,7 @@ public function enableOnlyOptionalWarmers(): void
4646
$this->onlyOptionalsEnabled = $this->optionalsEnabled = true;
4747
}
4848

49-
public function warmUp(string $cacheDir): array
49+
public function warmUp(string $cacheDir, EventDispatcherInterface $eventDispatcher = null): array
5050
{
5151
if ($collectDeprecations = $this->debug && !\defined('PHPUNIT_COMPOSER_INSTALL')) {
5252
$collectedLogs = [];
@@ -93,12 +93,15 @@ public function warmUp(string $cacheDir): array
9393
continue;
9494
}
9595

96+
$start = microtime(true);
9697
foreach ((array) $warmer->warmUp($cacheDir) as $item) {
9798
if (is_dir($item) || (str_starts_with($item, \dirname($cacheDir)) && !is_file($item))) {
9899
throw new \LogicException(sprintf('"%s::warmUp()" should return a list of files or classes but "%s" is none of them.', $warmer::class, $item));
99100
}
100101
$preload[] = $item;
101102
}
103+
104+
$eventDispatcher?->dispatch(new CacheWarmerAggregateItemFinishedEvent($warmer::class, (microtime(true) - $start) * 1000.0));
102105
}
103106
} finally {
104107
if ($collectDeprecations) {
Lines changed: 36 additions & 0 10000 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\Event\Cache;
13+
14+
use Symfony\Contracts\EventDispatcher\Event;
15+
16+
/**
17+
* @author Alexandre Daubois <alex.daubois@gmail.com>
18+
*/
19+
final class CacheWarmerAggregateItemFinishedEvent extends Event
20+
{
21+
public function __construct(
22+
private string $warmerClass,
23+
private float $time,
24+
) {
25+
}
26+
27+
public function getWarmerClass(): string
28+
{
29+
return $this->warmerClass;
30+
}
31+
32+
public function getTime(): float
33+
{
34+
return $this->time;
35+
}
36+
}

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

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

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1516
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate;
1617
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
18+
use Symfony\Component\HttpKernel\Event\Cache\CacheWarmerAggregateItemFinishedEvent;
1719

1820
class CacheWarmerAggregateTest extends TestCase
1921
{
@@ -91,4 +93,21 @@ public function testWarmupChecksInvalidFiles()
9193
$this->expectException(\LogicException::class);
9294
$aggregate->warmUp(__DIR__);
9395
}
96+
97+
public function testWarmupDispatchesEvents()
98+
{
99+
$warmer = $this->createMock(CacheWarmerInterface::class);
100+
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
101+
102+
$eventDispatcher
103+
->expects($this->once())
104+
->method('dispatch')
105+
->with($this->isInstanceOf(CacheWarmerAggregateItemFinishedEvent::class));
106+
107+
$warmer
108+
->expects($this->once())
109+
->method('warmUp');
110+
$aggregate = new CacheWarmerAggregate([$warmer]);
111+
$aggregate->warmUp(__DIR__, $eventDispatcher);
112+
}
94113
}

0 commit comments

Comments
 (0)
0