|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Benchmark for AddPropertyTypeDeclarationRector (representative of the Add*Type bucket change). |
| 7 | + * |
| 8 | + * AddParamTypeDeclarationRector / AddPropertyTypeDeclarationRector / AddReturnTypeDeclarationRector |
| 9 | + * each scanned their whole configured list per visited node, running isName()/isStringName() on |
| 10 | + * each entry before the class/object-type check. The change buckets the list by lowercased old |
| 11 | + * name once in configure(), so a node only visits entries sharing its name. |
| 12 | + * |
| 13 | + * This bench drives refactor() over many untyped properties whose names are NOT in the map (the |
| 14 | + * common case), isolating the scan cost. |
| 15 | + * |
| 16 | + * Run on both branches to compare: |
| 17 | + * php bench/add_property_type_bucket_bench.php |
| 18 | + * |
| 19 | + * Reports CPU time (user+sys) and wall time; CPU is the reliable metric, wall is noisy under |
| 20 | + * concurrent load. |
| 21 | + */ |
| 22 | + |
| 23 | +use PhpParser\Node\Stmt\Property; |
| 24 | +use PHPStan\Type\StringType; |
| 25 | +use Rector\DependencyInjection\LazyContainerFactory; |
| 26 | +use Rector\PhpParser\Node\BetterNodeFinder; |
| 27 | +use Rector\Testing\TestingParser\TestingParser; |
| 28 | +use Rector\TypeDeclaration\Rector\Property\AddPropertyTypeDeclarationRector; |
| 29 | +use Rector\TypeDeclaration\ValueObject\AddPropertyTypeDeclaration; |
| 30 | + |
| 31 | +require __DIR__ . '/../vendor/autoload.php'; |
| 32 | + |
| 33 | +const ENTRIES = 400; // configured add-property-type entries |
| 34 | +const PROPERTIES = 150; // untyped properties per pass, names not in the map |
| 35 | +const REPEAT = 200; // outer repeats over the whole set |
| 36 | + |
| 37 | +$rectorConfig = (new LazyContainerFactory())->create(); |
| 38 | +$rectorConfig->boot(); |
| 39 | + |
| 40 | +/** @var TestingParser $testingParser */ |
| 41 | +$testingParser = $rectorConfig->make(TestingParser::class); |
| 42 | + |
| 43 | +/** @var BetterNodeFinder $betterNodeFinder */ |
| 44 | +$betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class); |
| 45 | + |
| 46 | +$rector = $rectorConfig->make(AddPropertyTypeDeclarationRector::class); |
| 47 | + |
| 48 | +$addPropertyTypeDeclarations = []; |
| 49 | +for ($i = 0; $i < ENTRIES; ++$i) { |
| 50 | + $addPropertyTypeDeclarations[] = new AddPropertyTypeDeclaration('Bench\\SomeParent' . $i, 'oldProperty' . $i, new StringType()); |
| 51 | +} |
| 52 | + |
| 53 | +$rector->configure($addPropertyTypeDeclarations); |
| 54 | + |
| 55 | +$source = "<?php\n\nnamespace Bench;\n\nclass SomeClass\n{\n"; |
| 56 | +for ($i = 0; $i < PROPERTIES; ++$i) { |
| 57 | + // property names deliberately absent from the configured entries |
| 58 | + $source .= " public \$freshProperty{$i};\n"; |
| 59 | +} |
| 60 | + |
| 61 | +$source .= "}\n"; |
| 62 | + |
| 63 | +$sourceFilePath = tempnam(sys_get_temp_dir(), 'bench_') . '.php'; |
| 64 | +file_put_contents($sourceFilePath, $source); |
| 65 | + |
| 66 | +$stmts = $testingParser->parseFileToDecoratedNodes($sourceFilePath); |
| 67 | + |
| 68 | +/** @var Property[] $properties */ |
| 69 | +$properties = $betterNodeFinder->findInstanceOf($stmts, Property::class); |
| 70 | + |
| 71 | +$cpuSeconds = static function (): float { |
| 72 | + $usage = getrusage(); |
| 73 | + return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000 |
| 74 | + + $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000; |
| 75 | +}; |
| 76 | + |
| 77 | +$run = static function () use ($rector, $properties): void { |
| 78 | + foreach ($properties as $property) { |
| 79 | + $rector->refactor($property); |
| 80 | + } |
| 81 | +}; |
| 82 | + |
| 83 | +// warm up autoloading/reflection caches once, excluded from timing |
| 84 | +$run(); |
| 85 | + |
| 86 | +$wallStart = microtime(true); |
| 87 | +$cpuStart = $cpuSeconds(); |
| 88 | + |
| 89 | +for ($repeat = 0; $repeat < REPEAT; ++$repeat) { |
| 90 | + $run(); |
| 91 | +} |
| 92 | + |
| 93 | +$cpu = $cpuSeconds() - $cpuStart; |
| 94 | +$wall = microtime(true) - $wallStart; |
| 95 | + |
| 96 | +@unlink($sourceFilePath); |
| 97 | + |
| 98 | +$totalCalls = REPEAT * count($properties); |
| 99 | + |
| 100 | +printf('entries: %d, properties: %d, repeats: %d, refactor() calls: %d%s', ENTRIES, count($properties), REPEAT, $totalCalls, PHP_EOL); |
| 101 | +printf('CPU: %.4f s (%.3f us/call)%s', $cpu, $cpu / $totalCalls * 1_000_000, PHP_EOL); |
| 102 | +printf('wall: %.4f s%s', $wall, PHP_EOL); |
0 commit comments