|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use PhpParser\Node\Stmt\Class_; |
| 6 | +use Rector\CodeQuality\Rector\Class_\ConvertStaticToSelfRector; |
| 7 | +use Rector\DependencyInjection\LazyContainerFactory; |
| 8 | +use Rector\PhpParser\Node\BetterNodeFinder; |
| 9 | +use Rector\Testing\TestingParser\TestingParser; |
| 10 | + |
| 11 | +require __DIR__ . '/../vendor/autoload.php'; |
| 12 | + |
| 13 | +const CLASSES = 200; |
| 14 | +const REPEAT = 300; |
| 15 | + |
| 16 | +$rectorConfig = (new LazyContainerFactory())->create(); |
| 17 | +$rectorConfig->boot(); |
| 18 | + |
| 19 | +/** @var TestingParser $testingParser */ |
| 20 | +$testingParser = $rectorConfig->make(TestingParser::class); |
| 21 | + |
| 22 | +/** @var BetterNodeFinder $betterNodeFinder */ |
| 23 | +$betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class); |
| 24 | + |
| 25 | +$rector = $rectorConfig->make(ConvertStaticToSelfRector::class); |
| 26 | + |
| 27 | +$source = "<?php\n\nnamespace Bench;\n\n"; |
| 28 | +for ($i = 0; $i < CLASSES; ++$i) { |
| 29 | + $source .= "final class Service{$i}\n{\n"; |
| 30 | + $source .= " public function run(string \$value): string\n {\n return strtoupper(\$value);\n }\n}\n\n"; |
| 31 | +} |
| 32 | + |
| 33 | +$tempBase = tempnam(sys_get_temp_dir(), 'bench_'); |
| 34 | +$sourceFilePath = $tempBase . '.php'; |
| 35 | +file_put_contents($sourceFilePath, $source); |
| 36 | + |
| 37 | +$stmts = $testingParser->parseFileToDecoratedNodes($sourceFilePath); |
| 38 | + |
| 39 | +/** @var Class_[] $classes */ |
| 40 | +$classes = $betterNodeFinder->findInstanceOf($stmts, Class_::class); |
| 41 | + |
| 42 | +$cpuSeconds = static function (): float { |
| 43 | + $usage = getrusage(); |
| 44 | + return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000 |
| 45 | + + $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000; |
| 46 | +}; |
| 47 | + |
| 48 | +$run = static function () use ($rector, $classes): void { |
| 49 | + foreach ($classes as $class) { |
| 50 | + $rector->refactor($class); |
| 51 | + } |
| 52 | +}; |
| 53 | + |
| 54 | +$run(); |
| 55 | + |
| 56 | +$wallStart = microtime(true); |
| 57 | +$cpuStart = $cpuSeconds(); |
| 58 | + |
| 59 | +for ($repeat = 0; $repeat < REPEAT; ++$repeat) { |
| 60 | + $run(); |
| 61 | +} |
| 62 | + |
| 63 | +$cpu = $cpuSeconds() - $cpuStart; |
| 64 | +$wall = microtime(true) - $wallStart; |
| 65 | + |
| 66 | +if (is_file($sourceFilePath)) { |
| 67 | + unlink($sourceFilePath); |
| 68 | +} |
| 69 | + |
| 70 | +if (is_file($tempBase)) { |
| 71 | + unlink($tempBase); |
| 72 | +} |
| 73 | + |
| 74 | +$totalCalls = REPEAT * count($classes); |
| 75 | + |
| 76 | +printf('classes: %d, repeats: %d, refactor() calls: %d%s', count($classes), REPEAT, $totalCalls, PHP_EOL); |
| 77 | +printf('CPU: %.4f s (%.3f us/call)%s', $cpu, $cpu / $totalCalls * 1_000_000, PHP_EOL); |
| 78 | +printf('wall: %.4f s%s', $wall, PHP_EOL); |
0 commit comments