|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Benchmark for skipping wrapBinaryOpWithBrackets() on CallLike nodes in BetterStandardPrinter::p(). |
| 7 | + * |
| 8 | + * wrapBinaryOpWithBrackets() is a no-op for CallLike nodes, yet it ran its ExprAnalyzer |
| 9 | + * instanceof chain for every printed call. The source is call-heavy and printed fresh |
| 10 | + * (no original tokens), so every node goes through p(). |
| 11 | + * |
| 12 | + * Run on both branches to compare: |
| 13 | + * php bench/better_standard_printer_call_like_bench.php |
| 14 | + * |
| 15 | + * Reports CPU time (user+sys); CPU is the reliable metric, wall is noisy under concurrent load. |
| 16 | + */ |
| 17 | + |
| 18 | +use PhpParser\ParserFactory; |
| 19 | +use Rector\DependencyInjection\LazyContainerFactory; |
| 20 | +use Rector\PhpParser\Printer\BetterStandardPrinter; |
| 21 | + |
| 22 | +require __DIR__ . '/../vendor/autoload.php'; |
| 23 | + |
| 24 | +const STATEMENTS = 500; // pairs of call-heavy + binary-op statements |
| 25 | +const REPEAT = 300; // prints |
| 26 | + |
| 27 | +$rectorConfig = (new LazyContainerFactory())->create(); |
| 28 | +$rectorConfig->boot(); |
| 29 | + |
| 30 | +/** @var BetterStandardPrinter $betterStandardPrinter */ |
| 31 | +$betterStandardPrinter = $rectorConfig->make(BetterStandardPrinter::class); |
| 32 | + |
| 33 | +$source = "<?php\n\n"; |
| 34 | +for ($i = 0; $i < STATEMENTS; ++$i) { |
| 35 | + $source .= "\$a{$i} = foo(\$x->bar(baz(\$i, qux(1, 2)), Some::call(\$y)), new Obj(\$z->m()));\n"; |
| 36 | + $source .= "\$b{$i} = \$c + \$d * strlen(\$e) && ! \$f;\n"; |
| 37 | +} |
| 38 | + |
| 39 | +$stmts = (new ParserFactory())->createForNewestSupportedVersion()->parse($source); |
| 40 | + |
| 41 | +$cpuSeconds = static function (): float { |
| 42 | + $usage = getrusage(); |
| 43 | + return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000 |
| 44 | + + $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000; |
| 45 | +}; |
| 46 | + |
| 47 | +// warm up autoloading once, excluded from timing; output hash guards against behaviour change |
| 48 | +$printed = $betterStandardPrinter->prettyPrintFile($stmts); |
| 49 | + |
| 50 | +$cpuStart = $cpuSeconds(); |
| 51 | +for ($i = 0; $i < REPEAT; ++$i) { |
| 52 | + $betterStandardPrinter->prettyPrintFile($stmts); |
| 53 | +} |
| 54 | + |
| 55 | +$cpu = $cpuSeconds() - $cpuStart; |
| 56 | + |
| 57 | +printf("statements: %d\n", STATEMENTS * 2); |
| 58 | +printf("prints: %d\n", REPEAT); |
| 59 | +printf("CPU total: %.3f s\n", $cpu); |
| 60 | +printf("CPU per print: %.3f ms\n", $cpu / REPEAT * 1000); |
| 61 | +printf("output md5: %s\n", md5($printed)); |
0 commit comments