-
Notifications
You must be signed in to change notification settings - Fork 585
Disable PCOV if installed (40% faster on affected systems) #6253
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
Open
AJenbo
wants to merge
1
commit into
phpstan:2.2.x
Choose a base branch
from
AJenbo:pcov
base: 2.2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+276
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Process; | ||
|
|
||
| use function extension_loaded; | ||
| use function getenv; | ||
| use function ini_get; | ||
| use function phpversion; | ||
|
|
||
| /** | ||
| * pcov hooks into every userland function call as soon as the extension starts up, | ||
| * whether or not any coverage is being collected. Nothing in PHPStan collects | ||
| * coverage, so on a machine that has pcov installed - typically a CI image built | ||
| * for a test suite - the hook is pure overhead. It makes an analysis dramatically | ||
| * slower: function calls in the analysed process become several times more | ||
| * expensive, which measures as roughly 40 % of PHPStan's wall clock time. | ||
| * | ||
| * The hook is installed before any PHP code of the process runs and pcov.enabled is | ||
| * PHP_INI_SYSTEM, so a process cannot get rid of it with ini_set(). The only way is | ||
| * to start the process with pcov.enabled=0, which is what PHPStan does for its | ||
| * worker processes (see ProcessHelper). | ||
| */ | ||
| final class PcovHelper | ||
| { | ||
|
|
||
| public const DISABLED_INI_SETTING = 'pcov.enabled=0'; | ||
|
|
||
| public const ALLOW_ENV_VARIABLE = 'PHPSTAN_ALLOW_PCOV'; | ||
|
|
||
| public static function isLoaded(): bool | ||
| { | ||
| return extension_loaded('pcov'); | ||
| } | ||
|
|
||
| /** Whether pcov's call hook is installed in the current process. */ | ||
| public static function isActive(): bool | ||
| { | ||
| if (!self::isLoaded()) { | ||
| return false; | ||
| } | ||
|
|
||
| $enabled = ini_get('pcov.enabled'); | ||
|
|
||
| return $enabled !== false && $enabled !== '' && $enabled !== '0'; | ||
| } | ||
|
|
||
| /** Whether the user asked PHPStan to leave pcov alone. */ | ||
| public static function isAllowed(): bool | ||
| { | ||
| return getenv(self::ALLOW_ENV_VARIABLE) === '1'; | ||
| } | ||
|
|
||
| public static function shouldDisableInSubProcesses(): bool | ||
| { | ||
| return self::isLoaded() && !self::isAllowed(); | ||
| } | ||
|
|
||
| public static function getVersion(): ?string | ||
| { | ||
| $version = phpversion('pcov'); | ||
|
|
||
| return $version === false ? null : $version; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Process; | ||
|
|
||
| use PHPUnit\Framework\Attributes\CoversNothing; | ||
| use PHPUnit\Framework\TestCase; | ||
| use function extension_loaded; | ||
| use function getenv; | ||
| use function putenv; | ||
| use function sprintf; | ||
|
|
||
| #[CoversNothing] | ||
| class PcovHelperTest extends TestCase | ||
| { | ||
|
|
||
| public function testIsActiveRequiresTheExtension(): void | ||
| { | ||
| if (extension_loaded('pcov')) { | ||
| $this->assertTrue(PcovHelper::isLoaded()); | ||
| return; | ||
| } | ||
|
|
||
| $this->assertFalse(PcovHelper::isLoaded()); | ||
| $this->assertFalse(PcovHelper::isActive()); | ||
| $this->assertFalse(PcovHelper::shouldDisableInSubProcesses()); | ||
| } | ||
|
|
||
| public function testShouldNotDisableInSubProcessesWhenAllowed(): void | ||
| { | ||
| $originalValue = getenv(PcovHelper::ALLOW_ENV_VARIABLE); | ||
| putenv(sprintf('%s=1', PcovHelper::ALLOW_ENV_VARIABLE)); | ||
|
|
||
| try { | ||
| $this->assertTrue(PcovHelper::isAllowed()); | ||
| $this->assertFalse(PcovHelper::shouldDisableInSubProcesses()); | ||
| } finally { | ||
| self::restoreAllowEnvVariable($originalValue); | ||
| } | ||
| } | ||
|
|
||
| public function testShouldDisableInSubProcessesEvenWhenNotActiveInThisProcess(): void | ||
| { | ||
| if (!PcovHelper::isLoaded()) { | ||
| $this->markTestSkipped('pcov is not loaded in this process.'); | ||
| } | ||
|
|
||
| $originalValue = getenv(PcovHelper::ALLOW_ENV_VARIABLE); | ||
| putenv(PcovHelper::ALLOW_ENV_VARIABLE); | ||
|
|
||
| try { | ||
| $this->assertFalse(PcovHelper::isAllowed()); | ||
| // sub-processes read the php.ini, not this process' command line | ||
| $this->assertTrue(PcovHelper::shouldDisableInSubProcesses()); | ||
| $this->assertNotNull(PcovHelper::getVersion()); | ||
| } finally { | ||
| self::restoreAllowEnvVariable($originalValue); | ||
| } | ||
| } | ||
|
|
||
| private static function restoreAllowEnvVariable(string|false $originalValue): void | ||
| { | ||
| putenv($originalValue === false | ||
| ? PcovHelper::ALLOW_ENV_VARIABLE | ||
| : sprintf('%s=%s', PcovHelper::ALLOW_ENV_VARIABLE, $originalValue)); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Process; | ||
|
|
||
| use PHPStan\Command\AnalyseCommand; | ||
| use PHPUnit\Framework\Attributes\CoversNothing; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Symfony\Component\Console\Input\ArrayInput; | ||
| use Symfony\Component\Console\Input\InputArgument; | ||
| use Symfony\Component\Console\Input\InputDefinition; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Input\InputOption; | ||
| use function getenv; | ||
| use function putenv; | ||
| use function sprintf; | ||
|
|
||
| #[CoversNothing] | ||
| class ProcessHelperTest extends TestCase | ||
| { | ||
|
|
||
| public function testWorkerCommandDisablesPcov(): void | ||
| { | ||
| if (!PcovHelper::shouldDisableInSubProcesses()) { | ||
| $this->markTestSkipped('pcov is not loaded in this process.'); | ||
| } | ||
|
|
||
| $this->assertStringContainsString( | ||
| sprintf('-d %s', PcovHelper::DISABLED_INI_SETTING), | ||
| self::getWorkerCommand(), | ||
| ); | ||
| } | ||
|
|
||
| public function testWorkerCommandKeepsPcovWhenAllowed(): void | ||
| { | ||
| if (!PcovHelper::isLoaded()) { | ||
| $this->markTestSkipped('pcov is not loaded in this process.'); | ||
| } | ||
|
|
||
| $originalValue = getenv(PcovHelper::ALLOW_ENV_VARIABLE); | ||
| putenv(sprintf('%s=1', PcovHelper::ALLOW_ENV_VARIABLE)); | ||
|
|
||
| try { | ||
| $this->assertStringNotContainsString('pcov', self::getWorkerCommand()); | ||
| } finally { | ||
| putenv($originalValue === false | ||
| ? PcovHelper::ALLOW_ENV_VARIABLE | ||
| : sprintf('%s=%s', PcovHelper::ALLOW_ENV_VARIABLE, $originalValue)); | ||
| } | ||
| } | ||
|
|
||
| public function testWorkerCommandDoesNotMentionPcovWhenItIsNotLoaded(): void | ||
| { | ||
| if (PcovHelper::isLoaded()) { | ||
| $this->markTestSkipped('pcov is loaded in this process.'); | ||
| } | ||
|
|
||
| $this->assertStringNotContainsString('pcov', self::getWorkerCommand()); | ||
| } | ||
|
|
||
| private static function getWorkerCommand(): string | ||
| { | ||
| return ProcessHelper::getWorkerCommand( | ||
| 'bin/phpstan', | ||
| 'worker', | ||
| null, | ||
| [], | ||
| self::createInput(), | ||
| ); | ||
| } | ||
|
|
||
| private static function createInput(): InputInterface | ||
| { | ||
| return new ArrayInput(['paths' => ['src']], new InputDefinition([ | ||
| new InputArgument('paths', InputArgument::IS_ARRAY), | ||
| new InputOption(AnalyseCommand::OPTION_LEVEL, mode: InputOption::VALUE_REQUIRED), | ||
| new InputOption('autoload-file', mode: InputOption::VALUE_REQUIRED), | ||
| new InputOption('memory-limit', mode: InputOption::VALUE_REQUIRED), | ||
| new InputOption('xdebug', mode: InputOption::VALUE_NONE), | ||
| new InputOption('verbose', mode: InputOption::VALUE_NONE), | ||
| ])); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think there's a valid use case? If not you can remove the env var support.
Also how different are the runs with pcov off, and with pcov on but PHPStan restarting? I'm pondering not printing any message at all, the output is really noisy already.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No not really, it was mostly to mirror the other on in case it got split out, and I wasn't sure how it would look in PHPStan's own CI but that turned out to be a non issue.