Skip to content

Commit 812bbc2

Browse files
Sander Mullerclaude
andcommitted
Evaluate the final pull request code, with and without CI variables
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent e554034 commit 812bbc2

6 files changed

Lines changed: 165 additions & 16 deletions

File tree

‎.github/workflows/tiny-run-bench.yml‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
(cd bench/tools && composer install -q --no-interaction)
3535
php bench/project/vendor/phpstan/phpstan/phpstan.phar --version
3636
git clone -q --depth 1 https://github.com/nikic/PHP-Parser "$RUNNER_TEMP/php-parser"
37-
php bench/setup.php "$W" bench/project/vendor/phpstan/phpstan stock pr pre
37+
php bench/setup.php "$W" bench/project/vendor/phpstan/phpstan stock pr pre prci
3838
if [ "$RUNNER_OS" = Linux ]; then
3939
git clone -q --depth 1 https://github.com/tempestphp/tempest-framework "$RUNNER_TEMP/tempest"
4040
(cd "$RUNNER_TEMP/tempest" && composer install -q --no-interaction --ignore-platform-reqs --no-scripts)
@@ -43,14 +43,16 @@ jobs:
4343
php bench/prepare.php "$W" "$RUNNER_TEMP/php-parser/lib"
4444
fi
4545
php bench/setup.php "$W" bench/project/vendor/phpstan/phpstan --neon-only
46-
cd "$W/projects/tiny" && php "$W/arms/pr/phpstan.phar" diagnose | grep -E 'Turbo extension|Mechanism|Reason'
46+
cd "$W/projects/tiny"
47+
env -u CI -u GITHUB_ACTIONS php "$W/arms/pr/phpstan.phar" diagnose | grep -E 'Turbo extension|trusted|Mechanism'
48+
php "$W/arms/prci/phpstan.phar" diagnose | grep -E 'Turbo extension|trusted|Mechanism'
4749
- name: Tiny project
4850
env:
49-
ARMS: stock,pr,pre
51+
ARMS: stock,pr,pre,prci
5052
run: php bench/suite.php "$RUNNER_TEMP/w" tiny 12
5153
- name: php-parser lib (271 files)
5254
env:
53-
ARMS: stock,pr,pre
55+
ARMS: stock,pr,pre,prci
5456
run: php bench/suite.php "$RUNNER_TEMP/w" parser 7
5557
- name: php-parser lib, spawned workers
5658
env:
@@ -69,7 +71,7 @@ jobs:
6971
cd "$RUNNER_TEMP/w/projects/$p"
7072
rm -rf tmp-stock tmp-pr
7173
php "$RUNNER_TEMP/w/arms/stock/phpstan.phar" analyse -c phpstan-stock.neon --no-progress --error-format=raw --memory-limit=-1 2>/dev/null | sort > stock.txt
72-
php "$RUNNER_TEMP/w/arms/pr/phpstan.phar" analyse -c phpstan-pr.neon --no-progress --error-format=raw --memory-limit=-1 2>/dev/null | sort > pr.txt
74+
env -u CI -u GITHUB_ACTIONS php "$RUNNER_TEMP/w/arms/pr/phpstan.phar" analyse -c phpstan-pr.neon --no-progress --error-format=raw --memory-limit=-1 2>/dev/null | sort > pr.txt
7375
echo "$p: $(wc -l < stock.txt) stock lines, $(wc -l < pr.txt) pr lines"
7476
diff -q stock.txt pr.txt && echo "$p: identical"
7577
done
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Turbo;
4+
5+
use PHPStan\Command\Output;
6+
use PHPStan\DependencyInjection\AutowiredService;
7+
use PHPStan\Diagnose\DiagnoseExtension;
8+
use PHPStan\Php\PhpVersion;
9+
use function ini_get;
10+
use function php_uname;
11+
use function phpversion;
12+
use function sprintf;
13+
use const PHP_DEBUG;
14+
use const PHP_MAJOR_VERSION;
15+
use const PHP_MINOR_VERSION;
16+
use const PHP_OS_FAMILY;
17+
use const PHP_VERSION_ID;
18+
use const PHP_ZTS;
19+
20+
#[AutowiredService]
21+
final class TurboDiagnoseExtension implements DiagnoseExtension
22+
{
23+
24+
public function print(Output $output): void
25+
{
26+
$workerBinary = TurboExtensionSelector::findExtensionForWorkers();
27+
28+
$output->writeLineFormatted(sprintf(
29+
'<info>Turbo extension:</info> %s',
30+
$this->describeStatus($workerBinary),
31+
));
32+
33+
$isMusl = TurboExtensionSelector::isMusl();
34+
$platform = TurboExtensionSelector::resolvePlatformDirectory(PHP_OS_FAMILY, php_uname('m'), $isMusl);
35+
$output->writeLineFormatted(sprintf(
36+
'<info>Turbo platform:</info> %s (os: %s, machine: %s, libc: %s, php: %d.%d, zts: %s, debug: %s)',
37+
$platform ?? 'unsupported',
38+
PHP_OS_FAMILY,
39+
php_uname('m'),
40+
$isMusl ? 'musl' : 'gnu',
41+
PHP_MAJOR_VERSION,
42+
PHP_MINOR_VERSION,
43+
(bool) PHP_ZTS ? 'yes' : 'no',
44+
(bool) PHP_DEBUG ? 'yes' : 'no',
45+
));
46+
47+
if (TurboExtensionEnabler::isLoaded()) {
48+
$restartPath = TurboProcessRestarter::getRestartExtensionPath();
49+
if ($restartPath !== null) {
50+
$workerBinaryLine = sprintf('%s (loaded via process restart)', $restartPath);
51+
} else {
52+
$workerBinaryLine = 'loaded via php.ini, workers inherit it';
53+
}
54+
} elseif ($workerBinary !== null) {
55+
$workerBinaryLine = $workerBinary;
56+
} elseif (PHP_VERSION_ID < TurboExtensionSelector::MINIMUM_PHP_VERSION_ID) {
57+
$workerBinaryLine = sprintf(
58+
'none built for PHP < %s',
59+
(new PhpVersion(TurboExtensionSelector::MINIMUM_PHP_VERSION_ID))->getVersionString(),
60+
);
61+
} else {
62+
$workerBinaryLine = 'none found';
63+
}
64+
$output->writeLineFormatted(sprintf(
65+
'<info>Turbo worker binary:</info> %s',
66+
$workerBinaryLine,
67+
));
68+
$output->writeLineFormatted(sprintf(
69+
'<info>Turbo trusted types:</info> %s',
70+
$this->describeTrustedTypes(),
71+
));
72+
$output->writeLineFormatted('');
73+
}
74+
75+
private function describeTrustedTypes(): string
76+
{
77+
if (TurboExtensionEnabler::isTrustingOwnTypes()) {
78+
return 'on (PHPStan\'s own argument and return type checks are dropped; --debug keeps them)';
79+
}
80+
if (!TurboExtensionEnabler::isActive()) {
81+
return 'off (extension inactive)';
82+
}
83+
$fileCache = ini_get('opcache.file_cache');
84+
if ($fileCache !== false && $fileCache !== '') {
85+
return 'off (the extension skips it while opcache.file_cache is set)';
86+
}
87+
88+
return 'off (--debug, or OPcache is not active)';
89+
}
90+
91+
private function describeStatus(?string $workerBinary): string
92+
{
93+
if (!TurboExtensionEnabler::isLoaded()) {
94+
if ($workerBinary !== null) {
95+
return 'enabled in worker processes (the main process runs without it)';
96+
}
97+
98+
return 'not loaded';
99+
}
100+
101+
$loadedVersion = phpversion('phpstan_turbo');
102+
if ($loadedVersion !== TurboExtensionEnabler::EXPECTED_EXTENSION_VERSION) {
103+
return sprintf(
104+
'inactive (extension version %s, expected %s)',
105+
$loadedVersion === false ? 'unknown' : $loadedVersion,
106+
TurboExtensionEnabler::EXPECTED_EXTENSION_VERSION,
107+
);
108+
}
109+
110+
return sprintf('enabled (version %s)', TurboExtensionEnabler::EXPECTED_EXTENSION_VERSION);
111+
}
112+
113+
}

‎bench/pr/TurboProcessRestarter.php‎

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use RecursiveDirectoryIterator;
88
use RecursiveIteratorIterator;
99
use Throwable;
10+
use function class_exists;
1011
use function explode;
1112
use function extension_loaded;
1213
use function filemtime;
@@ -15,6 +16,7 @@
1516
use function filesize;
1617
use function function_exists;
1718
use function get_cfg_var;
19+
use function getenv;
1820
use function implode;
1921
use function in_array;
2022
use function ini_get;
@@ -106,8 +108,16 @@ final class TurboProcessRestarter
106108
'opcache.max_accelerated_files',
107109
];
108110

109-
/** The resolved file cache directory, memoized per process; false while unresolved */
110-
private static string|false|null $fileCacheDirectory = false;
111+
/**
112+
* Environment variables that CI services set (to a non-empty value other
113+
* than "false"): a CI job usually starts with an empty temp dir, so a file
114+
* cache there costs its writes on every run and is never read
115+
*/
116+
private const CI_ENVIRONMENT_VARIABLES = ['CI', 'GITHUB_ACTIONS', 'GITLAB_CI', 'BUILDKITE', 'TF_BUILD', 'JENKINS_URL', 'TEAMCITY_VERSION'];
117+
118+
private static ?string $fileCacheDirectory = null;
119+
120+
private static bool $fileCacheDirectoryResolved = false;
111121

112122
/**
113123
* The extension path this process was given through -d — by the restart,
@@ -289,20 +299,26 @@ public static function getOpcacheArgs(): array
289299
* as opcodes, unchecked, inside PHPStan.
290300
*
291301
* Only runs from the phar get one: a source checkout changes all the time
292-
* and has no build to key the directory by. Not on Windows either: every
302+
* and has no build to key the directory by. Not in CI (see
303+
* resolveContinuousIntegration()): an empty temp dir at the start of every
304+
* job would make the cache pure cost, and with a file cache the
305+
* extension's trusted-types pass is off. Not on Windows either: every
293306
* spawned worker there gets its own opcache.cache_id (see ProcessHelper),
294307
* and OPcache then keeps a separate file cache per worker that no later
295308
* run reuses: 2 GB after one benchmark run on a GitHub runner, and cold
296309
* runs 27-50% slower.
297310
*/
298311
private static function getFileCacheDirectory(): ?string
299312
{
300-
if (self::$fileCacheDirectory !== false) {
313+
if (self::$fileCacheDirectoryResolved) {
301314
return self::$fileCacheDirectory;
302315
}
303316

304-
self::$fileCacheDirectory = null;
305-
if (PHP_OS_FAMILY === 'Windows' || !function_exists('posix_geteuid')) {
317+
self::$fileCacheDirectoryResolved = true;
318+
if (PHP_OS_FAMILY === 'Windows' || !function_exists('posix_geteuid') || !class_exists('Phar', false)) {
319+
return null;
320+
}
321+
if (self::resolveContinuousIntegration(getenv())) {
306322
return null;
307323
}
308324

@@ -357,6 +373,21 @@ public static function resolveFileCacheKey(string $pharSignature, string $turboB
357373
return substr(sha1(implode("\0", [$pharSignature, $turboBinary, $debug ? 'debug' : ''])), 0, 16);
358374
}
359375

376+
/**
377+
* @param array<string, string> $environment getenv()
378+
*/
379+
public static function resolveContinuousIntegration(array $environment): bool
380+
{
381+
foreach (self::CI_ENVIRONMENT_VARIABLES as $name) {
382+
$value = $environment[$name] ?? '';
383+
if ($value !== '' && strtolower($value) !== 'false') {
384+
return true;
385+
}
386+
}
387+
388+
return false;
389+
}
390+
360391
/**
361392
* The same answer before the restart and in the restarted process: the
362393
* binary the restart loads with -d, or the version of one loaded by the
@@ -378,7 +409,7 @@ private static function describeTurboBinary(): string
378409
return 'none';
379410
}
380411

381-
private static function isPrivateDirectory(string $directory, int $userId): bool
412+
public static function isPrivateDirectory(string $directory, int $userId): bool
382413
{
383414
if (is_link($directory) || !is_dir($directory)) {
384415
return false;
@@ -395,7 +426,7 @@ private static function isPrivateDirectory(string $directory, int $userId): bool
395426
* no longer installed. Runs only when a new key directory was created, so
396427
* about once per update.
397428
*/
398-
private static function pruneFileCacheDirectories(string $baseDirectory, string $currentKey, int $now): void
429+
public static function pruneFileCacheDirectories(string $baseDirectory, string $currentKey, int $now): void
399430
{
400431
$entries = @scandir($baseDirectory);
401432
if ($entries === false) {

‎bench/put-restarter.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
[$_, $pharPath, $file] = $argv;
66
$phar = new Phar($pharPath);
77
$phar['src/Turbo/TurboProcessRestarter.php'] = file_get_contents($file);
8+
$phar['src/Turbo/TurboDiagnoseExtension.php'] = file_get_contents(dirname($file) . '/TurboDiagnoseExtension.php');
89
unset($phar);
910
$util = new Seld\PharUtils\Timestamps($pharPath);
1011
$util->updateTimestamps(new DateTimeImmutable('2026-09-30 06:00:00'));

‎bench/setup.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
$fc = "$w/fcdir-$arm";
2121
@mkdir($fc, 0777, true);
2222
$args = match (true) {
23-
$arm === 'stock', $arm === 'daemon', $arm === 'pr', $arm === 'pre' => ['sha512'],
23+
$arm === 'stock', $arm === 'daemon', $arm === 'pr', $arm === 'pre', $arm === 'prci' => ['sha512'],
2424
$arm === 'fc0' => ['sha512', $fc],
2525
default => ['sha512', $fc, 'vts'],
2626
};
2727
passthru(escapeshellarg(PHP_BINARY) . ' -d phar.readonly=0 ' . escapeshellarg(__DIR__ . '/make-arm.php') . ' ' . implode(' ', array_map('escapeshellarg', ["$dst/phpstan.phar", ...$args])), $ec);
2828
if ($ec !== 0) exit($ec);
29-
if ($arm === 'pr' || $arm === 'pre') {
29+
if ($arm === 'pr' || $arm === 'pre' || $arm === 'prci') {
3030
passthru(escapeshellarg(PHP_BINARY) . ' -d phar.readonly=0 ' . escapeshellarg(__DIR__ . '/put-restarter.php') . ' ' . escapeshellarg("$dst/phpstan.phar") . ' ' . escapeshellarg(__DIR__ . '/pr/TurboProcessRestarter.php'), $ec);
3131
if ($ec !== 0) exit($ec);
3232
}

‎bench/suite.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
$specs = [];
1717
foreach ($arms as $arm) {
1818
$phar = escapeshellarg("$w/arms/$arm/phpstan.phar");
19-
$bin = $arm === 'daemon' ? $client : ($arm === 'pre' ? 'TMPDIR=' . escapeshellarg("$w/systemp-pre") . ' ' : '') . "$php$flags $phar";
19+
// GitHub Actions sets CI, which turns the file cache off; prci keeps it to show that
20+
$noCi = in_array($arm, ['pr', 'pre'], true) ? 'env -u CI -u GITHUB_ACTIONS ' : '';
21+
$bin = $arm === 'daemon' ? $client : $noCi . ($arm === 'pre' ? 'TMPDIR=' . escapeshellarg("$w/systemp-pre") . ' ' : '') . "$php$flags $phar";
2022
$analyse = "$bin analyse -c phpstan-$arm.neon --no-progress --memory-limit=-1";
2123
exec('cd ' . escapeshellarg($project) . " && $analyse > " . (PHP_OS_FAMILY === 'Windows' ? 'NUL' : '/dev/null') . ' 2>&1', $o, $ec);
2224
echo "primed $suite/$arm, exit $ec\n";

0 commit comments

Comments
 (0)