|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Benchmark for RemoveDeadInstanceOfAssertRector. |
| 7 | + * |
| 8 | + * The rule removes assert($x instanceof Foo) when $x is already of type Foo. To get the asserted |
| 9 | + * class type it read getType($instanceof->class), which runs the type resolver over a class name |
| 10 | + * node whose name is already known. |
| 11 | + * |
| 12 | + * The change reads a concrete class name straight off the node (new ObjectType($name)); the |
| 13 | + * equals()/isSuperTypeOf() comparison against the expression type is unchanged, so the decision is |
| 14 | + * identical. self/static/parent depend on the enclosing class, so those fall back to getType(). |
| 15 | + * |
| 16 | + * This bench parses a file of many functions, each with a typed param and a matching dead |
| 17 | + * instanceof assert, attaches scope (as the real run does), then drives refactor() over every |
| 18 | + * statement-aware node. |
| 19 | + * |
| 20 | + * Run on both branches to compare: |
| 21 | + * php bench/remove_dead_instanceof_assert_name_type_bench.php |
| 22 | + * |
| 23 | + * Reports CPU time (user+sys) and wall time; CPU is the reliable metric, wall is noisy under |
| 24 | + * concurrent load. |
| 25 | + */ |
| 26 | + |
| 27 | +use PhpParser\Node\Stmt\Function_; |
| 28 | +use Rector\DeadCode\Rector\StmtsAwareInterface\RemoveDeadInstanceOfAssertRector; |
| 29 | +use Rector\DependencyInjection\LazyContainerFactory; |
| 30 | +use Rector\PhpParser\Node\BetterNodeFinder; |
| 31 | +use Rector\Testing\TestingParser\TestingParser; |
| 32 | + |
| 33 | +require __DIR__ . '/../vendor/autoload.php'; |
| 34 | + |
| 35 | +const FUNCTIONS = 150; // functions with a dead instanceof assert |
| 36 | +const REPEAT = 60; // outer repeats over the whole set |
| 37 | + |
| 38 | +$rectorConfig = (new LazyContainerFactory())->create(); |
| 39 | +$rectorConfig->boot(); |
| 40 | + |
| 41 | +/** @var TestingParser $testingParser */ |
| 42 | +$testingParser = $rectorConfig->make(TestingParser::class); |
| 43 | + |
| 44 | +/** @var BetterNodeFinder $betterNodeFinder */ |
| 45 | +$betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class); |
| 46 | + |
| 47 | +$rector = $rectorConfig->make(RemoveDeadInstanceOfAssertRector::class); |
| 48 | + |
| 49 | +$source = "<?php\n\nnamespace Bench;\n\n"; |
| 50 | +for ($i = 0; $i < FUNCTIONS; ++$i) { |
| 51 | + $source .= "function run{$i}(\\DateTime \$value): void\n{\n"; |
| 52 | + $source .= " assert(\$value instanceof \\DateTime);\n}\n\n"; |
| 53 | +} |
| 54 | + |
| 55 | +$sourceFilePath = tempnam(sys_get_temp_dir(), 'bench_') . '.php'; |
| 56 | +file_put_contents($sourceFilePath, $source); |
| 57 | + |
| 58 | +$stmts = $testingParser->parseFileToDecoratedNodes($sourceFilePath); |
| 59 | + |
| 60 | +/** @var Function_[] $functions */ |
| 61 | +$functions = $betterNodeFinder->findInstanceOf($stmts, Function_::class); |
| 62 | + |
| 63 | +$cpuSeconds = static function (): float { |
| 64 | + $usage = getrusage(); |
| 65 | + return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000 |
| 66 | + + $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000; |
| 67 | +}; |
| 68 | + |
| 69 | +$run = static function () use ($rector, $functions): void { |
| 70 | + foreach ($functions as $function) { |
| 71 | + // clone so the removal in one pass does not empty the body for the next repeat |
| 72 | + $rector->refactor(clone $function); |
| 73 | + } |
| 74 | +}; |
| 75 | + |
| 76 | +// warm up autoloading/reflection caches once, excluded from timing |
| 77 | +$run(); |
| 78 | + |
| 79 | +$wallStart = microtime(true); |
| 80 | +$cpuStart = $cpuSeconds(); |
| 81 | + |
| 82 | +for ($repeat = 0; $repeat < REPEAT; ++$repeat) { |
| 83 | + $run(); |
| 84 | +} |
| 85 | + |
| 86 | +$cpu = $cpuSeconds() - $cpuStart; |
| 87 | +$wall = microtime(true) - $wallStart; |
| 88 | + |
| 89 | +@unlink($sourceFilePath); |
| 90 | + |
| 91 | +$totalCalls = REPEAT * count($functions); |
| 92 | + |
| 93 | +printf('functions: %d, repeats: %d, refactor() calls: %d%s', count($functions), REPEAT, $totalCalls, PHP_EOL); |
| 94 | +printf('CPU: %.4f s (%.3f us/call)%s', $cpu, $cpu / $totalCalls * 1_000_000, PHP_EOL); |
| 95 | +printf('wall: %.4f s%s', $wall, PHP_EOL); |
0 commit comments