Skip to content

Commit 574c557

Browse files
authored
Print analysis time with -vvv
1 parent c7c9689 commit 574c557

File tree

4 files changed

+32
-17
lines changed

4 files changed

+32
-17
lines changed

bin/phpstan

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ use Symfony\Component\Console\Helper\ProgressBar;
2525

2626
define('__PHPSTAN_RUNNING__', true);
2727

28+
$analysisStartTime = microtime(true);
29+
2830
$devOrPharLoader = require_once __DIR__ . '/../vendor/autoload.php';
2931
require_once __DIR__ . '/../preload.php';
3032
$composer = ComposerHelper::getComposerConfig(getcwd());
@@ -158,7 +160,7 @@ use Symfony\Component\Console\Helper\ProgressBar;
158160
ProgressBar::setFormatDefinition('file_download', ' [%bar%] %percent:3s%% %fileSize%');
159161

160162
$reversedComposerAutoloaderProjectPaths = array_values(array_unique(array_reverse($composerAutoloaderProjectPaths)));
161-
$application->add(new AnalyseCommand($reversedComposerAutoloaderProjectPaths));
163+
$application->add(new AnalyseCommand($reversedComposerAutoloaderProjectPaths, $analysisStartTime));
162164
$application->add(new WorkerCommand($reversedComposerAutoloaderProjectPaths));
163165
$application->add(new ClearResultCacheCommand($reversedComposerAutoloaderProjectPaths));
164166
$application->add(new FixerWorkerCommand($reversedComposerAutoloaderProjectPaths));

src/Command/AnalyseCommand.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class AnalyseCommand extends Command
7070
*/
7171
public function __construct(
7272
private array $composerAutoloaderProjectPaths,
73+
private float $analysisStartTime,
7374
)
7475
{
7576
parent::__construct();
@@ -171,7 +172,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
171172

172173
if ($generateBaselineFile === null && $allowEmptyBaseline) {
173174
$inceptionResult->getStdOutput()->getStyle()->error('You must pass the --generate-baseline option alongside --allow-empty-baseline.');
174-
return $inceptionResult->handleReturn(1, null);
175+
return $inceptionResult->handleReturn(1, null, $this->analysisStartTime);
175176
}
176177

177178
$errorOutput = $inceptionResult->getErrorOutput();
@@ -214,13 +215,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
214215
$baselineExtension = pathinfo($generateBaselineFile, PATHINFO_EXTENSION);
215216
if ($baselineExtension === '') {
216217
$inceptionResult->getStdOutput()->getStyle()->error(sprintf('Baseline filename must have an extension, %s provided instead.', pathinfo($generateBaselineFile, PATHINFO_BASENAME)));
217-
return $inceptionResult->handleReturn(1, null);
218+
return $inceptionResult->handleReturn(1, null, $this->analysisStartTime);
218219
}
219220

220221
if (!in_array($baselineExtension, ['neon', 'php'], true)) {
221222
$inceptionResult->getStdOutput()->getStyle()->error(sprintf('Baseline filename extension must be .neon or .php, .%s was used instead.', $baselineExtension));
222223

223-
return $inceptionResult->handleReturn(1, null);
224+
return $inceptionResult->handleReturn(1, null, $this->analysisStartTime);
224225
}
225226
}
226227

@@ -244,12 +245,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
244245
$inceptionResult->getErrorOutput()->getStyle()->note('No files found to analyse.');
245246
$inceptionResult->getErrorOutput()->getStyle()->warning('This will cause a non-zero exit code in PHPStan 2.0.');
246247

247-
return $inceptionResult->handleReturn(0, null);
248+
return $inceptionResult->handleReturn(0, null, $this->analysisStartTime);
248249
}
249250

250251
$inceptionResult->getErrorOutput()->getStyle()->error('No files found to analyse.');
251252

252-
return $inceptionResult->handleReturn(1, null);
253+
return $inceptionResult->handleReturn(1, null, $this->analysisStartTime);
253254
}
254255

255256
$analysedConfigFiles = array_intersect($files, $container->getParameter('allConfigFiles'));
@@ -275,7 +276,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
275276
if ($fix) {
276277
if ($generateBaselineFile !== null) {
277278
$inceptionResult->getStdOutput()->getStyle()->error('You cannot pass the --generate-baseline option when running PHPStan Pro.');
278-
return $inceptionResult->handleReturn(1, null);
279+
return $inceptionResult->handleReturn(1, null, $this->analysisStartTime);
279280
}
280281

281282
return $this->runFixer($inceptionResult, $container, $onlyFiles, $input, $output, $files);
@@ -331,7 +332,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
331332
$previous = $previous->getPrevious();
332333
}
333334

334-
return $inceptionResult->handleReturn(1, null);
335+
return $inceptionResult->handleReturn(1, null, $this->analysisStartTime);
335336
}
336337

337338
throw $t;
@@ -441,7 +442,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
441442
count($internalErrors) === 1 ? 'An internal error' : 'Internal errors',
442443
));
443444

444-
return $inceptionResult->handleReturn(1, $analysisResult->getPeakMemoryUsageBytes());
445+
return $inceptionResult->handleReturn(1, $analysisResult->getPeakMemoryUsageBytes(), $this->analysisStartTime);
445446
}
446447

447448
return $this->generateBaseline($generateBaselineFile, $inceptionResult, $analysisResult, $output, $allowEmptyBaseline, $baselineExtension, $failWithoutResultCache);
@@ -477,6 +478,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
477478
return $inceptionResult->handleReturn(
478479
$exitCode,
479480
$analysisResult->getPeakMemoryUsageBytes(),
481+
$this->analysisStartTime,
480482
);
481483
}
482484

@@ -528,7 +530,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
528530

529531
$bleedingEdge = (bool) $container->getParameter('featureToggles')['projectServicesNotInAnalysedPaths'];
530532
if ($bleedingEdge) {
531-
return $inceptionResult->handleReturn(1, $analysisResult->getPeakMemoryUsageBytes());
533+
return $inceptionResult->handleReturn(1, $analysisResult->getPeakMemoryUsageBytes(), $this->analysisStartTime);
532534
}
533535

534536
$errorOutput->getStyle()->warning('This will cause a non-zero exit code in PHPStan 2.0.');
@@ -540,6 +542,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
540542
return $inceptionResult->handleReturn(
541543
$exitCode,
542544
$analysisResult->getPeakMemoryUsageBytes(),
545+
$this->analysisStartTime,
543546
);
544547
}
545548

@@ -558,7 +561,7 @@ private function generateBaseline(string $generateBaselineFile, InceptionResult
558561
$inceptionResult->getStdOutput()->getStyle()->error('No errors were found during the analysis. Baseline could not be generated.');
559562
$inceptionResult->getStdOutput()->writeLineFormatted('To allow generating empty baselines, pass <fg=cyan>--allow-empty-baseline</> option.');
560563

561-
return $inceptionResult->handleReturn(1, $analysisResult->getPeakMemoryUsageBytes());
564+
return $inceptionResult->handleReturn(1, $analysisResult->getPeakMemoryUsageBytes(), $this->analysisStartTime);
562565
}
563566

564567
$streamOutput = $this->createStreamOutput();
@@ -588,15 +591,15 @@ private function generateBaseline(string $generateBaselineFile, InceptionResult
588591
} catch (DirectoryCreatorException $e) {
589592
$inceptionResult->getStdOutput()->writeLineFormatted($e->getMessage());
590593

591-
return $inceptionResult->handleReturn(1, $analysisResult->getPeakMemoryUsageBytes());
594+
return $inceptionResult->handleReturn(1, $analysisResult->getPeakMemoryUsageBytes(), $this->analysisStartTime);
592595
}
593596

594597
try {
595598
FileWriter::write($generateBaselineFile, $baselineContents);
596599
} catch (CouldNotWriteFileException $e) {
597600
$inceptionResult->getStdOutput()->writeLineFormatted($e->getMessage());
598601

599-
return $inceptionResult->handleReturn(1, $analysisResult->getPeakMemoryUsageBytes());
602+
return $inceptionResult->handleReturn(1, $analysisResult->getPeakMemoryUsageBytes(), $this->analysisStartTime);
600603
}
601604

602605
$errorsCount = 0;
@@ -636,7 +639,7 @@ private function generateBaseline(string $generateBaselineFile, InceptionResult
636639
$exitCode = 2;
637640
}
638641

639-
return $inceptionResult->handleReturn($exitCode, $analysisResult->getPeakMemoryUsageBytes());
642+
return $inceptionResult->handleReturn($exitCode, $analysisResult->getPeakMemoryUsageBytes(), $this->analysisStartTime);
640643
}
641644

642645
/**
@@ -648,7 +651,7 @@ private function runFixer(InceptionResult $inceptionResult, Container $container
648651
if ($ciDetector->isCiDetected()) {
649652
$inceptionResult->getStdOutput()->writeLineFormatted('PHPStan Pro can\'t run in CI environment yet. Stay tuned!');
650653

651-
return $inceptionResult->handleReturn(1, null);
654+
return $inceptionResult->handleReturn(1, null, $this->analysisStartTime);
652655
}
653656

654657
/** @var FixerApplication $fixerApplication */

src/Command/InceptionResult.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use PHPStan\Internal\BytesHelper;
88
use function max;
99
use function memory_get_peak_usage;
10+
use function microtime;
11+
use function round;
1012
use function sprintf;
1113

1214
class InceptionResult
@@ -84,7 +86,7 @@ public function getGenerateBaselineFile(): ?string
8486
return $this->generateBaselineFile;
8587
}
8688

87-
public function handleReturn(int $exitCode, ?int $peakMemoryUsageBytes): int
89+
public function handleReturn(int $exitCode, ?int $peakMemoryUsageBytes, float $analysisStartTime): int
8890
{
8991
if ($peakMemoryUsageBytes !== null && $this->getErrorOutput()->isVerbose()) {
9092
$this->getErrorOutput()->writeLineFormatted(sprintf(
@@ -93,6 +95,13 @@ public function handleReturn(int $exitCode, ?int $peakMemoryUsageBytes): int
9395
));
9496
}
9597

98+
if ($this->getErrorOutput()->isDebug()) {
99+
$this->getErrorOutput()->writeLineFormatted(sprintf(
100+
'Analysis time: %0.1f seconds',
101+
round(microtime(true) - $analysisStartTime, 1),
102+
));
103+
}
104+
96105
return $exitCode;
97106
}
98107

tests/PHPStan/Command/AnalyseCommandTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Throwable;
99
use function chdir;
1010
use function getcwd;
11+
use function microtime;
1112
use function realpath;
1213
use function sprintf;
1314
use const DIRECTORY_SEPARATOR;
@@ -104,7 +105,7 @@ public static function autoDiscoveryPathsProvider(): array
104105
*/
105106
private function runCommand(int $expectedStatusCode, array $parameters = []): string
106107
{
107-
$commandTester = new CommandTester(new AnalyseCommand([]));
108+
$commandTester = new CommandTester(new AnalyseCommand([], microtime(true)));
108109

109110
$commandTester->execute([
110111
'paths' => [__DIR__ . DIRECTORY_SEPARATOR . 'test'],

0 commit comments

Comments
 (0)