Skip to content

Print analysis time with -vvv #3239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion bin/phpstan
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ use Symfony\Component\Console\Helper\ProgressBar;

define('__PHPSTAN_RUNNING__', true);

$analysisStartTime = microtime(true);

$devOrPharLoader = require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../preload.php';
$composer = ComposerHelper::getComposerConfig(getcwd());
Expand Down Expand Up @@ -158,7 +160,7 @@ use Symfony\Component\Console\Helper\ProgressBar;
ProgressBar::setFormatDefinition('file_download', ' [%bar%] %percent:3s%% %fileSize%');

$reversedComposerAutoloaderProjectPaths = array_values(array_unique(array_reverse($composerAutoloaderProjectPaths)));
$application->add(new AnalyseCommand($reversedComposerAutoloaderProjectPaths));
$application->add(new AnalyseCommand($reversedComposerAutoloaderProjectPaths, $analysisStartTime));
$application->add(new WorkerCommand($reversedComposerAutoloaderProjectPaths));
$application->add(new ClearResultCacheCommand($reversedComposerAutoloaderProjectPaths));
$application->add(new FixerWorkerCommand($reversedComposerAutoloaderProjectPaths));
Expand Down
31 changes: 17 additions & 14 deletions src/Command/AnalyseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class AnalyseCommand extends Command
*/
public function __construct(
private array $composerAutoloaderProjectPaths,
private float $analysisStartTime,
)
{
parent::__construct();
Expand Down Expand Up @@ -171,7 +172,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

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

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

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

return $inceptionResult->handleReturn(1, null);
return $inceptionResult->handleReturn(1, null, $this->analysisStartTime);
}
}

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

return $inceptionResult->handleReturn(0, null);
return $inceptionResult->handleReturn(0, null, $this->analysisStartTime);
}

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

return $inceptionResult->handleReturn(1, null);
return $inceptionResult->handleReturn(1, null, $this->analysisStartTime);
}

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

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

return $inceptionResult->handleReturn(1, null);
return $inceptionResult->handleReturn(1, null, $this->analysisStartTime);
}

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

return $inceptionResult->handleReturn(1, $analysisResult->getPeakMemoryUsageBytes());
return $inceptionResult->handleReturn(1, $analysisResult->getPeakMemoryUsageBytes(), $this->analysisStartTime);
}

return $this->generateBaseline($generateBaselineFile, $inceptionResult, $analysisResult, $output, $allowEmptyBaseline, $baselineExtension, $failWithoutResultCache);
Expand Down Expand Up @@ -477,6 +478,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return $inceptionResult->handleReturn(
$exitCode,
$analysisResult->getPeakMemoryUsageBytes(),
$this->analysisStartTime,
);
}

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

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

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

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

return $inceptionResult->handleReturn(1, $analysisResult->getPeakMemoryUsageBytes());
return $inceptionResult->handleReturn(1, $analysisResult->getPeakMemoryUsageBytes(), $this->analysisStartTime);
}

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

return $inceptionResult->handleReturn(1, $analysisResult->getPeakMemoryUsageBytes());
return $inceptionResult->handleReturn(1, $analysisResult->getPeakMemoryUsageBytes(), $this->analysisStartTime);
}

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

return $inceptionResult->handleReturn(1, $analysisResult->getPeakMemoryUsageBytes());
return $inceptionResult->handleReturn(1, $analysisResult->getPeakMemoryUsageBytes(), $this->analysisStartTime);
}

$errorsCount = 0;
Expand Down Expand Up @@ -636,7 +639,7 @@ private function generateBaseline(string $generateBaselineFile, InceptionResult
$exitCode = 2;
}

return $inceptionResult->handleReturn($exitCode, $analysisResult->getPeakMemoryUsageBytes());
return $inceptionResult->handleReturn($exitCode, $analysisResult->getPeakMemoryUsageBytes(), $this->analysisStartTime);
}

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

return $inceptionResult->handleReturn(1, null);
return $inceptionResult->handleReturn(1, null, $this->analysisStartTime);
}

/** @var FixerApplication $fixerApplication */
Expand Down
11 changes: 10 additions & 1 deletion src/Command/InceptionResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use PHPStan\Internal\BytesHelper;
use function max;
use function memory_get_peak_usage;
use function microtime;
use function round;
use function sprintf;

class InceptionResult
Expand Down Expand Up @@ -84,7 +86,7 @@ public function getGenerateBaselineFile(): ?string
return $this->generateBaselineFile;
}

public function handleReturn(int $exitCode, ?int $peakMemoryUsageBytes): int
public function handleReturn(int $exitCode, ?int $peakMemoryUsageBytes, float $analysisStartTime): int
{
if ($peakMemoryUsageBytes !== null && $this->getErrorOutput()->isVerbose()) {
$this->getErrorOutput()->writeLineFormatted(sprintf(
Expand All @@ -93,6 +95,13 @@ public function handleReturn(int $exitCode, ?int $peakMemoryUsageBytes): int
));
}

if ($this->getErrorOutput()->isDebug()) {
$this->getErrorOutput()->writeLineFormatted(sprintf(
'Analysis time: %0.1f seconds',
round(microtime(true) - $analysisStartTime, 1),
));
}

return $exitCode;
}

Expand Down
3 changes: 2 additions & 1 deletion tests/PHPStan/Command/AnalyseCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Throwable;
use function chdir;
use function getcwd;
use function microtime;
use function realpath;
use function sprintf;
use const DIRECTORY_SEPARATOR;
Expand Down Expand Up @@ -104,7 +105,7 @@ public static function autoDiscoveryPathsProvider(): array
*/
private function runCommand(int $expectedStatusCode, array $parameters = []): string
{
$commandTester = new CommandTester(new AnalyseCommand([]));
$commandTester = new CommandTester(new AnalyseCommand([], microtime(true)));

$commandTester->execute([
'paths' => [__DIR__ . DIRECTORY_SEPARATOR . 'test'],
Expand Down
Loading