|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Benchmark for DowngradeArrayKeyFirstLastRector. |
| 7 | + * |
| 8 | + * refactor() used to run ExprInTopStmtMatcher::match() twice per statement - one full subtree |
| 9 | + * traversal to find array_key_first(), then a second full traversal to find array_key_last(). For |
| 10 | + * the overwhelming majority of statements (which contain neither) that is two full traversals of |
| 11 | + * the statement's expression tree where one would do. |
| 12 | + * |
| 13 | + * Matching either name in a single traversal and dispatching on the matched name halves the matcher |
| 14 | + * cost on the common path. |
| 15 | + * |
| 16 | + * This bench drives refactor() over many expression statements that contain neither call - the |
| 17 | + * common case. CPU (user+sys) is the reliable metric; wall is noisy. |
| 18 | + */ |
| 19 | + |
| 20 | +use PhpParser\Node\Stmt\Expression; |
| 21 | +use Rector\DependencyInjection\LazyContainerFactory; |
| 22 | +use Rector\DowngradePhp73\Rector\FuncCall\DowngradeArrayKeyFirstLastRector; |
| 23 | +use Rector\PhpParser\Node\BetterNodeFinder; |
| 24 | +use Rector\Testing\TestingParser\TestingParser; |
| 25 | + |
| 26 | +require __DIR__ . '/../vendor/autoload.php'; |
| 27 | + |
| 28 | +const STMTS = 300; |
| 29 | +const REPEAT = 200; |
| 30 | + |
| 31 | +$rectorConfig = (new LazyContainerFactory())->create(); |
| 32 | +$rectorConfig->boot(); |
| 33 | + |
| 34 | +/** @var TestingParser $testingParser */ |
| 35 | +$testingParser = $rectorConfig->make(TestingParser::class); |
| 36 | + |
| 37 | +/** @var BetterNodeFinder $betterNodeFinder */ |
| 38 | +$betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class); |
| 39 | + |
| 40 | +$rector = $rectorConfig->make(DowngradeArrayKeyFirstLastRector::class); |
| 41 | + |
| 42 | +$source = "<?php\n\nnamespace Bench;\n\nfunction run(\$items)\n{\n"; |
| 43 | +for ($i = 0; $i < STMTS; ++$i) { |
| 44 | + // statements with funccalls that are neither array_key_first nor array_key_last |
| 45 | + $source .= " \$result{$i} = array_map('trim', array_filter(\$items, 'is_string'));\n"; |
| 46 | +} |
| 47 | +$source .= "}\n"; |
| 48 | + |
| 49 | +$sourceFilePath = tempnam(sys_get_temp_dir(), 'bench_') . '.php'; |
| 50 | +file_put_contents($sourceFilePath, $source); |
| 51 | + |
| 52 | +$stmts = $testingParser->parseFileToDecoratedNodes($sourceFilePath); |
| 53 | + |
| 54 | +/** @var Expression[] $expressions */ |
| 55 | +$expressions = $betterNodeFinder->findInstanceOf($stmts, Expression::class); |
| 56 | + |
| 57 | +$cpuSeconds = static function (): float { |
| 58 | + $usage = getrusage(); |
| 59 | + return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000 |
| 60 | + + $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000; |
| 61 | +}; |
| 62 | + |
| 63 | +$run = static function () use ($rector, $expressions): void { |
| 64 | + foreach ($expressions as $expression) { |
| 65 | + $rector->refactor($expression); |
| 66 | + } |
| 67 | +}; |
| 68 | + |
| 69 | +$run(); |
| 70 | + |
| 71 | +$wallStart = microtime(true); |
| 72 | +$cpuStart = $cpuSeconds(); |
| 73 | + |
| 74 | +for ($repeat = 0; $repeat < REPEAT; ++$repeat) { |
| 75 | + $run(); |
| 76 | +} |
| 77 | + |
| 78 | +$cpu = $cpuSeconds() - $cpuStart; |
| 79 | +$wall = microtime(true) - $wallStart; |
| 80 | + |
| 81 | +@unlink($sourceFilePath); |
| 82 | + |
| 83 | +$totalCalls = REPEAT * count($expressions); |
| 84 | + |
| 85 | +printf('statements: %d, repeats: %d, refactor() calls: %d%s', count($expressions), REPEAT, $totalCalls, PHP_EOL); |
| 86 | +printf('CPU: %.4f s (%.3f us/call)%s', $cpu, $cpu / $totalCalls * 1_000_000, PHP_EOL); |
| 87 | +printf('wall: %.4f s%s', $wall, PHP_EOL); |
0 commit comments