|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Benchmark for the "skip print on unchanged file" change in FileProcessor::processFile(). |
| 7 | + * |
| 8 | + * Previously every file - including the majority that no rule touches - paid one full |
| 9 | + * printFormatPreserving() pass just to detect "nothing changed". The change breaks out of |
| 10 | + * the convergence loop before that print when the file is still pristine and the pass |
| 11 | + * recorded no RectorWithLineChange. |
| 12 | + * |
| 13 | + * With a bare container (no rules registered) every file is unchanged, so this isolates |
| 14 | + * exactly one skipped print per file as the only difference between the two branches. |
| 15 | + * |
| 16 | + * Run on both branches to compare: |
| 17 | + * php bench/skip_unchanged_file_print_bench.php |
| 18 | + * |
| 19 | + * Reports CPU time (user+sys); CPU is the reliable metric, wall is noisy under concurrent load. |
| 20 | + */ |
| 21 | + |
| 22 | +use Rector\Application\FileProcessor; |
| 23 | +use Rector\Application\Provider\CurrentFileProvider; |
| 24 | +use Rector\Configuration\ConfigurationFactory; |
| 25 | +use Rector\DependencyInjection\LazyContainerFactory; |
| 26 | +use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider; |
| 27 | +use Rector\ValueObject\Application\File; |
| 28 | + |
| 29 | +require __DIR__ . '/../vendor/autoload.php'; |
| 30 | + |
| 31 | +const STATEMENTS = 300; // methods in the source file, sizes the print cost |
| 32 | +const REPEAT = 200; // files processed |
| 33 | + |
| 34 | +$rectorConfig = (new LazyContainerFactory())->create(); |
| 35 | +$rectorConfig->boot(); |
| 36 | + |
| 37 | +$fileProcessor = $rectorConfig->make(FileProcessor::class); |
| 38 | + |
| 39 | +/** @var CurrentFileProvider $currentFileProvider */ |
| 40 | +$currentFileProvider = $rectorConfig->make(CurrentFileProvider::class); |
| 41 | + |
| 42 | +/** @var DynamicSourceLocatorProvider $dynamicSourceLocatorProvider */ |
| 43 | +$dynamicSourceLocatorProvider = $rectorConfig->make(DynamicSourceLocatorProvider::class); |
| 44 | + |
| 45 | +/** @var ConfigurationFactory $configurationFactory */ |
| 46 | +$configurationFactory = $rectorConfig->make(ConfigurationFactory::class); |
| 47 | + |
| 48 | +// build a sizable, already-clean file; no rule is registered so it stays unchanged |
| 49 | +$source = "<?php\n\ndeclare(strict_types=1);\n\nnamespace Bench;\n\nclass SomeClass\n{\n"; |
| 50 | +for ($i = 0; $i < STATEMENTS; ++$i) { |
| 51 | + $source .= " public function method{$i}(int \$value{$i}): int\n {\n return \$value{$i} + {$i};\n }\n\n"; |
| 52 | +} |
| 53 | + |
| 54 | +$source .= "}\n"; |
| 55 | + |
| 56 | +$sourceFilePath = tempnam(sys_get_temp_dir(), 'bench_') . '.php'; |
| 57 | +file_put_contents($sourceFilePath, $source); |
| 58 | + |
| 59 | +$dynamicSourceLocatorProvider->setFilePath($sourceFilePath); |
| 60 | +$configuration = $configurationFactory->createForTests([$sourceFilePath]); |
| 61 | + |
| 62 | +$cpuSeconds = static function (): float { |
| 63 | + $usage = getrusage(); |
| 64 | + return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000 |
| 65 | + + $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000; |
| 66 | +}; |
| 67 | + |
| 68 | +$run = static function () use ($fileProcessor, $currentFileProvider, $sourceFilePath, $configuration): void { |
| 69 | + // File is single-use (hydrateStmtsAndTokens rejects a second hydrate), so make a fresh one |
| 70 | + $file = new File($sourceFilePath, file_get_contents($sourceFilePath)); |
| 71 | + $currentFileProvider->setFile($file); |
| 72 | + $fileProcessor->processFile($file, $configuration); |
| 73 | +}; |
| 74 | + |
| 75 | +// warm up autoloading/reflection/scope caches once, excluded from timing |
| 76 | +$run(); |
| 77 | + |
| 78 | +$wallStart = microtime(true); |
| 79 | +$cpuStart = $cpuSeconds(); |
| 80 | + |
| 81 | +for ($repeat = 0; $repeat < REPEAT; ++$repeat) { |
| 82 | + $run(); |
| 83 | +} |
| 84 | + |
| 85 | +$cpu = $cpuSeconds() - $cpuStart; |
| 86 | +$wall = microtime(true) - $wallStart; |
| 87 | + |
| 88 | +@unlink($sourceFilePath); |
| 89 | + |
| 90 | +printf('statements: %d, files processed: %d%s', STATEMENTS, REPEAT, PHP_EOL); |
| 91 | +printf('CPU: %.4f s (%.3f ms/file)%s', $cpu, $cpu / REPEAT * 1_000, PHP_EOL); |
| 92 | +printf('wall: %.4f s%s', $wall, PHP_EOL); |
0 commit comments