|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Benchmark for AddDoesNotPerformAssertionToNonAssertingTestRector on test classes with deep parent chains. |
| 7 | + * |
| 8 | + * Each test class has parent lifecycle hooks (setUp(), tearDown()) and parent::test*() calls. Resolving these |
| 9 | + * via AstResolver parses and decorates parent files on first use, so this bench measures one cold pass, as in a real run. |
| 10 | + * |
| 11 | + * Run on both branches to compare. CPU (user+sys) is the reliable metric; wall is noisy. |
| 12 | + */ |
| 13 | + |
| 14 | +use PhpParser\Node\Stmt\ClassMethod; |
| 15 | +use Rector\DependencyInjection\LazyContainerFactory; |
| 16 | +use Rector\PhpParser\Node\BetterNodeFinder; |
| 17 | +use Rector\PHPUnit\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector; |
| 18 | +use Rector\Testing\TestingParser\TestingParser; |
| 19 | + |
| 20 | +require __DIR__ . '/../vendor/autoload.php'; |
| 21 | + |
| 22 | +const CHAINS = 10; // independent base class chains |
| 23 | +const DEPTH = 4; // abstract parents per chain, between TestCase and the test class |
| 24 | +const TESTS_PER_CHAIN = 30; // test classes extending the deepest parent of each chain |
| 25 | +const TEST_METHODS = 5; // non-asserting test methods per test class |
| 26 | +const FILLER_METHODS = 15; // extra methods per parent class, to make parsing realistic |
| 27 | +const MAX_CPU_SECONDS = 1.0; // fails in CI above this |
| 28 | + |
| 29 | +$directory = sys_get_temp_dir() . '/rector_phpunit_bench_' . getmypid(); |
| 30 | +@mkdir($directory); |
| 31 | + |
| 32 | +$writeClass = static function (string $className, string $body) use ($directory): string { |
| 33 | + $filePath = $directory . '/' . $className . '.php'; |
| 34 | + file_put_contents($filePath, "<?php\n\nnamespace Bench;\n\n" . $body); |
| 35 | + return $filePath; |
| 36 | +}; |
| 37 | + |
| 38 | +// make generated classes visible to PHPStan reflection |
| 39 | +spl_autoload_register(static function (string $class) use ($directory): void { |
| 40 | + if (! str_starts_with($class, 'Bench\\')) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + $filePath = $directory . '/' . substr($class, 6) . '.php'; |
| 45 | + if (is_file($filePath)) { |
| 46 | + require_once $filePath; |
| 47 | + } |
| 48 | +}); |
| 49 | + |
| 50 | +$testFilePaths = []; |
| 51 | + |
| 52 | +for ($chain = 0; $chain < CHAINS; ++$chain) { |
| 53 | + $parentClassName = '\PHPUnit\Framework\TestCase'; |
| 54 | + |
| 55 | + for ($level = 0; $level < DEPTH; ++$level) { |
| 56 | + $className = sprintf('AbstractChain%dLevel%d', $chain, $level); |
| 57 | + |
| 58 | + $methods = " protected function setUp(): void\n {\n parent::setUp();\n \$this->prepare{$level}();\n }\n\n"; |
| 59 | + $methods .= " protected function tearDown(): void\n {\n \$this->cleanup = null;\n parent::tearDown();\n }\n\n"; |
| 60 | + $methods .= " public function testInherited(): void\n {\n \$value = {$level};\n }\n\n"; |
| 61 | + $methods .= " private function prepare{$level}(): void\n {\n \$this->ready = true;\n }\n\n"; |
| 62 | + |
| 63 | + for ($filler = 0; $filler < FILLER_METHODS; ++$filler) { |
| 64 | + $methods .= " protected function helper{$filler}(array \$items): array\n {\n return array_map(fn (\$item) => \$item * {$filler}, \$items);\n }\n\n"; |
| 65 | + } |
| 66 | + |
| 67 | + $writeClass( |
| 68 | + $className, |
| 69 | + "abstract class {$className} extends {$parentClassName}\n{\n protected \$ready;\n protected \$cleanup;\n\n{$methods}}\n" |
| 70 | + ); |
| 71 | + |
| 72 | + $parentClassName = $className; |
| 73 | + } |
| 74 | + |
| 75 | + for ($test = 0; $test < TESTS_PER_CHAIN; ++$test) { |
| 76 | + $className = sprintf('Chain%dTest%d', $chain, $test); |
| 77 | + |
| 78 | + $methods = " public function testInherited(): void\n {\n parent::testInherited();\n }\n\n"; |
| 79 | + for ($testMethod = 0; $testMethod < TEST_METHODS; ++$testMethod) { |
| 80 | + $methods .= " public function testSomething{$testMethod}(): void\n {\n \$value = \$this->helper{$testMethod}([1, 2, 3]);\n }\n\n"; |
| 81 | + } |
| 82 | + |
| 83 | + $testFilePaths[] = $writeClass( |
| 84 | + $className, |
| 85 | + "final class {$className} extends {$parentClassName}\n{\n{$methods}}\n" |
| 86 | + ); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +$rectorConfig = (new LazyContainerFactory())->create(); |
| 91 | +$rectorConfig->boot(); |
| 92 | + |
| 93 | +/** @var TestingParser $testingParser */ |
| 94 | +$testingParser = $rectorConfig->make(TestingParser::class); |
| 95 | + |
| 96 | +/** @var BetterNodeFinder $betterNodeFinder */ |
| 97 | +$betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class); |
| 98 | + |
| 99 | +$rector = $rectorConfig->make(AddDoesNotPerformAssertionToNonAssertingTestRector::class); |
| 100 | + |
| 101 | +// parse test files up front, parent files are left for the rule to resolve |
| 102 | +$classMethods = []; |
| 103 | +foreach ($testFilePaths as $testFilePath) { |
| 104 | + $stmts = $testingParser->parseFileToDecoratedNodes($testFilePath); |
| 105 | + $classMethods = [...$classMethods, ...$betterNodeFinder->findInstanceOf($stmts, ClassMethod::class)]; |
| 106 | +} |
| 107 | + |
| 108 | +$cpuSeconds = static function (): float { |
| 109 | + $usage = getrusage(); |
| 110 | + return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000 |
| 111 | + + $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000; |
| 112 | +}; |
| 113 | + |
| 114 | +$wallStart = microtime(true); |
| 115 | +$cpuStart = $cpuSeconds(); |
| 116 | + |
| 117 | +$changedCount = 0; |
| 118 | +foreach ($classMethods as $classMethod) { |
| 119 | + if ($rector->refactor($classMethod) instanceof ClassMethod) { |
| 120 | + ++$changedCount; |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +$cpu = $cpuSeconds() - $cpuStart; |
| 125 | +$wall = microtime(true) - $wallStart; |
| 126 | + |
| 127 | +array_map('unlink', glob($directory . '/*.php')); |
| 128 | +@rmdir($directory); |
| 129 | + |
| 130 | +printf('test classes: %d, test methods: %d, changed: %d%s', count($testFilePaths), count($classMethods), $changedCount, PHP_EOL); |
| 131 | +printf('CPU: %.4f s (%.3f ms/method)%s', $cpu, $cpu / count($classMethods) * 1000, PHP_EOL); |
| 132 | +printf('wall: %.4f s%s', $wall, PHP_EOL); |
| 133 | + |
| 134 | +if ($cpu > MAX_CPU_SECONDS) { |
| 135 | + printf('CPU %.4f s is over the %.1f s limit%s', $cpu, MAX_CPU_SECONDS, PHP_EOL); |
| 136 | + exit(1); |
| 137 | +} |
0 commit comments