|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Benchmark for RemoveArgumentFromDefaultParentCallRector. |
| 7 | + * |
| 8 | + * The rule subscribes to Class_. For every class that extends a parent it resolved the class |
| 9 | + * reflection and walked getAncestors() - native reflection over the whole ancestor chain - |
| 10 | + * before even checking whether the class body contains a parent:: call to trim. A subclass |
| 11 | + * that never calls parent:: (the common case) paid for that ancestor lookup only to find |
| 12 | + * nothing to do. |
| 13 | + * |
| 14 | + * The optimization adds a cheap AST pre-scan (hasParentStaticCall): if no method body holds a |
| 15 | + * parent:: static call, the rule bails before resolveClassReflection()/getAncestors(). |
| 16 | + * |
| 17 | + * This bench parses a file of many subclasses that extend a parent but never call parent::, |
| 18 | + * attaches scope (as the real run does), then drives refactor() over every Class_ node. Those |
| 19 | + * nodes never change, so the calls are repeatable. |
| 20 | + * |
| 21 | + * Run on both branches to compare: |
| 22 | + * php bench/remove_argument_from_default_parent_call_bench.php |
| 23 | + * |
| 24 | + * Reports CPU time (user+sys) and wall time; CPU is the reliable metric, wall is noisy |
| 25 | + * under concurrent load. |
| 26 | + */ |
| 27 | + |
| 28 | +use PhpParser\Node\Stmt\Class_; |
| 29 | +use Rector\DeadCode\Rector\ClassMethod\RemoveArgumentFromDefaultParentCallRector; |
| 30 | +use Rector\DependencyInjection\LazyContainerFactory; |
| 31 | +use Rector\PhpParser\Node\BetterNodeFinder; |
| 32 | +use Rector\Testing\TestingParser\TestingParser; |
| 33 | + |
| 34 | +require __DIR__ . '/../vendor/autoload.php'; |
| 35 | + |
| 36 | +const CHILDREN = 150; // subclasses that extend a parent but never call parent:: |
| 37 | +const REPEAT = 60; // outer repeats over the whole class set |
| 38 | + |
| 39 | +$rectorConfig = (new LazyContainerFactory())->create(); |
| 40 | +$rectorConfig->boot(); |
| 41 | + |
| 42 | +/** @var TestingParser $testingParser */ |
| 43 | +$testingParser = $rectorConfig->make(TestingParser::class); |
| 44 | + |
| 45 | +/** @var BetterNodeFinder $betterNodeFinder */ |
| 46 | +$betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class); |
| 47 | + |
| 48 | +$rector = $rectorConfig->make(RemoveArgumentFromDefaultParentCallRector::class); |
| 49 | + |
| 50 | +// generate a file: one parent with a defaulted constructor param, many children that extend it |
| 51 | +// but only call an ordinary function - no parent:: call, so the rule has nothing to trim |
| 52 | +$source = "<?php\n\nnamespace Bench;\n\n"; |
| 53 | +$source .= "class SomeParent\n{\n public function __construct(array \$param = [])\n {\n }\n}\n\n"; |
| 54 | +for ($i = 0; $i < CHILDREN; ++$i) { |
| 55 | + $source .= "class Child{$i} extends \\Bench\\SomeParent\n{\n"; |
| 56 | + $source .= " public function __construct(string \$differentParam)\n {\n"; |
| 57 | + $source .= " strlen(\$differentParam);\n"; |
| 58 | + $source .= " }\n}\n\n"; |
| 59 | +} |
| 60 | + |
| 61 | +$sourceFilePath = tempnam(sys_get_temp_dir(), 'bench_') . '.php'; |
| 62 | +file_put_contents($sourceFilePath, $source); |
| 63 | + |
| 64 | +$stmts = $testingParser->parseFileToDecoratedNodes($sourceFilePath); |
| 65 | + |
| 66 | +/** @var Class_[] $classes */ |
| 67 | +$classes = $betterNodeFinder->findInstanceOf($stmts, Class_::class); |
| 68 | + |
| 69 | +$cpuSeconds = static function (): float { |
| 70 | + $usage = getrusage(); |
| 71 | + return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000 |
| 72 | + + $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000; |
| 73 | +}; |
| 74 | + |
| 75 | +$run = static function () use ($rector, $classes): void { |
| 76 | + foreach ($classes as $class) { |
| 77 | + $rector->refactor($class); |
| 78 | + } |
| 79 | +}; |
| 80 | + |
| 81 | +// warm up autoloading/reflection caches once, excluded from timing |
| 82 | +$run(); |
| 83 | + |
| 84 | +$wallStart = microtime(true); |
| 85 | +$cpuStart = $cpuSeconds(); |
| 86 | + |
| 87 | +for ($repeat = 0; $repeat < REPEAT; ++$repeat) { |
| 88 | + $run(); |
| 89 | +} |
| 90 | + |
| 91 | +$cpu = $cpuSeconds() - $cpuStart; |
| 92 | +$wall = microtime(true) - $wallStart; |
| 93 | + |
| 94 | +@unlink($sourceFilePath); |
| 95 | + |
| 96 | +$totalCalls = REPEAT * count($classes); |
| 97 | + |
| 98 | +printf('classes: %d, repeats: %d, refactor() calls: %d%s', count($classes), REPEAT, $totalCalls, PHP_EOL); |
| 99 | +printf('CPU: %.4f s (%.3f us/call)%s', $cpu, $cpu / $totalCalls * 1_000_000, PHP_EOL); |
| 100 | +printf('wall: %.4f s%s', $wall, PHP_EOL); |
0 commit comments