|
6 | 6 | * Benchmark for AddDoesNotPerformAssertionToNonAssertingTestRector on test classes with deep parent chains. |
7 | 7 | * |
8 | 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. |
| 9 | + * via AstResolver parses and decorates parent files on first use, so every scenario measures one cold pass, as in a real run. |
| 10 | + * |
| 11 | + * Scenarios, all with the same shape (see the constants below): |
| 12 | + * - inherited: parents have non-asserting hooks, the test classes have none. Every test method changes. |
| 13 | + * - own set up: like "inherited", but each test class has its own setUp() calling parent::setUp(). The file of |
| 14 | + * the test class is already parsed by Rector, it must not be parsed a second time to look at its hooks. |
| 15 | + * - asserting hooks: the top parent of each chain has an asserting hook (setUp(), assertPostConditions() or a |
| 16 | + * #[Before] method), so the lifecycle analyzer walks the whole chain and no test method changes. |
10 | 17 | * |
11 | 18 | * Run on both branches to compare. CPU (user+sys) is the reliable metric; wall is noisy. |
12 | 19 | */ |
|
28 | 35 | const FILLER_METHODS = 15; // extra methods per parent class, to make parsing realistic |
29 | 36 | const MAX_CPU_SECONDS = 1.0; // fails in CI above this |
30 | 37 |
|
31 | | -$directory = sys_get_temp_dir() . '/rector_phpunit_bench_' . getmypid(); |
32 | | -@mkdir($directory); |
| 38 | +const SCENARIOS = [ |
| 39 | + 'inherited' => [ |
| 40 | + 'own_set_up' => false, |
| 41 | + 'asserting_hooks' => false, |
| 42 | + ], |
| 43 | + 'own set up' => [ |
| 44 | + 'own_set_up' => true, |
| 45 | + 'asserting_hooks' => false, |
| 46 | + ], |
| 47 | + 'asserting hooks' => [ |
| 48 | + 'own_set_up' => false, |
| 49 | + 'asserting_hooks' => true, |
| 50 | + ], |
| 51 | +]; |
33 | 52 |
|
34 | | -$writeClass = static function (string $className, string $body) use ($directory): string { |
35 | | - $filePath = $directory . '/' . $className . '.php'; |
36 | | - file_put_contents($filePath, "<?php\n\nnamespace Bench;\n\n" . $body); |
37 | | - return $filePath; |
| 53 | +$cpuSeconds = static function (): float { |
| 54 | + $usage = getrusage(); |
| 55 | + return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000 |
| 56 | + + $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000; |
38 | 57 | }; |
39 | 58 |
|
| 59 | +$directory = sys_get_temp_dir() . '/rector_phpunit_bench_' . getmypid(); |
| 60 | +@mkdir($directory); |
| 61 | + |
40 | 62 | // make generated classes visible to PHPStan reflection |
41 | 63 | spl_autoload_register(static function (string $class) use ($directory): void { |
42 | 64 | if (! str_starts_with($class, 'Bench\\')) { |
|
49 | 71 | } |
50 | 72 | }); |
51 | 73 |
|
52 | | -$testFilePaths = []; |
| 74 | +$isFailed = false; |
| 75 | +$scenarioIndex = 0; |
53 | 76 |
|
54 | | -for ($chain = 0; $chain < CHAINS; ++$chain) { |
55 | | - $parentClassName = '\PHPUnit\Framework\TestCase'; |
| 77 | +foreach (SCENARIOS as $scenarioName => $scenario) { |
| 78 | + // own class names per scenario, so no scenario profits from the parsed files of another |
| 79 | + $prefix = 'S' . $scenarioIndex++; |
56 | 80 |
|
57 | | - for ($level = 0; $level < DEPTH; ++$level) { |
58 | | - $className = sprintf('AbstractChain%dLevel%d', $chain, $level); |
| 81 | + $writeClass = static function (string $className, string $body) use ($directory): string { |
| 82 | + $filePath = $directory . '/' . $className . '.php'; |
| 83 | + file_put_contents($filePath, "<?php\n\nnamespace Bench;\n\n" . $body); |
| 84 | + return $filePath; |
| 85 | + }; |
59 | 86 |
|
60 | | - $methods = " protected function setUp(): void\n {\n parent::setUp();\n \$this->prepare{$level}();\n }\n\n"; |
61 | | - $methods .= " protected function tearDown(): void\n {\n \$this->cleanup = null;\n parent::tearDown();\n }\n\n"; |
62 | | - $methods .= " public function testInherited(): void\n {\n \$value = {$level};\n }\n\n"; |
63 | | - $methods .= " private function prepare{$level}(): void\n {\n \$this->ready = true;\n }\n\n"; |
| 87 | + $testFilePaths = []; |
64 | 88 |
|
65 | | - for ($filler = 0; $filler < FILLER_METHODS; ++$filler) { |
66 | | - $methods .= " protected function helper{$filler}(array \$items): array\n {\n return array_map(fn (\$item) => \$item * {$filler}, \$items);\n }\n\n"; |
67 | | - } |
| 89 | + for ($chain = 0; $chain < CHAINS; ++$chain) { |
| 90 | + $parentClassName = '\PHPUnit\Framework\TestCase'; |
68 | 91 |
|
69 | | - $writeClass( |
70 | | - $className, |
71 | | - "abstract class {$className} extends {$parentClassName}\n{\n protected \$ready;\n protected \$cleanup;\n\n{$methods}}\n" |
72 | | - ); |
| 92 | + for ($level = 0; $level < DEPTH; ++$level) { |
| 93 | + $className = sprintf('%sAbstractChain%dLevel%d', $prefix, $chain, $level); |
73 | 94 |
|
74 | | - $parentClassName = $className; |
75 | | - } |
| 95 | + $methods = " protected function setUp(): void\n {\n parent::setUp();\n \$this->prepare{$level}();\n }\n\n"; |
| 96 | + $methods .= " protected function tearDown(): void\n {\n \$this->cleanup = null;\n parent::tearDown();\n }\n\n"; |
| 97 | + $methods .= " public function testInherited(): void\n {\n \$value = {$level};\n }\n\n"; |
| 98 | + $methods .= " private function prepare{$level}(): void\n {\n \$this->ready = true;\n }\n\n"; |
| 99 | + |
| 100 | + if ($scenario['asserting_hooks'] && $level === 0) { |
| 101 | + // the asserting hook is in the top parent, furthest away from the test classes |
| 102 | + $methods .= match ($chain % 3) { |
| 103 | + 0 => " protected function assertPostConditions(): void\n {\n \$this->assertTrue(\$this->ready);\n }\n\n", |
| 104 | + 1 => " #[\PHPUnit\Framework\Attributes\Before]\n protected function checkReady(): void\n {\n \$this->assertNotNull(\$this->cleanup);\n }\n\n", |
| 105 | + default => " protected function assertPreConditions(): void\n {\n \$this->assertFalse(\$this->ready);\n }\n\n", |
| 106 | + }; |
| 107 | + } |
76 | 108 |
|
77 | | - for ($test = 0; $test < TESTS_PER_CHAIN; ++$test) { |
78 | | - $className = sprintf('Chain%dTest%d', $chain, $test); |
| 109 | + for ($filler = 0; $filler < FILLER_METHODS; ++$filler) { |
| 110 | + $methods .= " protected function helper{$filler}(array \$items): array\n {\n return array_map(fn (\$item) => \$item * {$filler}, \$items);\n }\n\n"; |
| 111 | + } |
79 | 112 |
|
80 | | - $methods = " public function testInherited(): void\n {\n parent::testInherited();\n }\n\n"; |
81 | | - for ($testMethod = 0; $testMethod < TEST_METHODS; ++$testMethod) { |
82 | | - $methods .= " public function testSomething{$testMethod}(): void\n {\n \$value = \$this->helper{$testMethod}([1, 2, 3]);\n }\n\n"; |
| 113 | + $writeClass( |
| 114 | + $className, |
| 115 | + "abstract class {$className} extends {$parentClassName}\n{\n protected \$ready;\n protected \$cleanup;\n\n{$methods}}\n" |
| 116 | + ); |
| 117 | + |
| 118 | + $parentClassName = $className; |
83 | 119 | } |
84 | 120 |
|
85 | | - $testFilePaths[] = $writeClass( |
86 | | - $className, |
87 | | - "final class {$className} extends {$parentClassName}\n{\n{$methods}}\n" |
88 | | - ); |
| 121 | + for ($test = 0; $test < TESTS_PER_CHAIN; ++$test) { |
| 122 | + $className = sprintf('%sChain%dTest%d', $prefix, $chain, $test); |
| 123 | + |
| 124 | + $methods = ''; |
| 125 | + if ($scenario['own_set_up']) { |
| 126 | + $methods .= " protected function setUp(): void\n {\n parent::setUp();\n \$this->ready = true;\n }\n\n"; |
| 127 | + } |
| 128 | + |
| 129 | + $methods .= " public function testInherited(): void\n {\n parent::testInherited();\n }\n\n"; |
| 130 | + for ($testMethod = 0; $testMethod < TEST_METHODS; ++$testMethod) { |
| 131 | + $methods .= " public function testSomething{$testMethod}(): void\n {\n \$value = \$this->helper{$testMethod}([1, 2, 3]);\n }\n\n"; |
| 132 | + } |
| 133 | + |
| 134 | + $testFilePaths[] = $writeClass( |
| 135 | + $className, |
| 136 | + "final class {$className} extends {$parentClassName}\n{\n{$methods}}\n" |
| 137 | + ); |
| 138 | + } |
89 | 139 | } |
90 | | -} |
91 | 140 |
|
92 | | -$rectorConfig = (new LazyContainerFactory())->create(); |
93 | | -$rectorConfig->boot(); |
| 141 | + $rectorConfig = (new LazyContainerFactory())->create(); |
| 142 | + $rectorConfig->boot(); |
94 | 143 |
|
95 | | -/** @var TestingParser $testingParser */ |
96 | | -$testingParser = $rectorConfig->make(TestingParser::class); |
| 144 | + /** @var TestingParser $testingParser */ |
| 145 | + $testingParser = $rectorConfig->make(TestingParser::class); |
97 | 146 |
|
98 | | -/** @var BetterNodeFinder $betterNodeFinder */ |
99 | | -$betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class); |
| 147 | + /** @var BetterNodeFinder $betterNodeFinder */ |
| 148 | + $betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class); |
100 | 149 |
|
101 | | -$rector = $rectorConfig->make(AddDoesNotPerformAssertionToNonAssertingTestRector::class); |
| 150 | + $rector = $rectorConfig->make(AddDoesNotPerformAssertionToNonAssertingTestRector::class); |
102 | 151 |
|
103 | | -/** @var CurrentFileProvider $currentFileProvider */ |
104 | | -$currentFileProvider = $rectorConfig->make(CurrentFileProvider::class); |
| 152 | + // parse test files up front, parent files are left for the rule to resolve |
| 153 | + // the rule finds the class of a method in the stmts of the current file, as it does in a real run |
| 154 | + /** @var CurrentFileProvider $currentFileProvider */ |
| 155 | + $currentFileProvider = $rectorConfig->make(CurrentFileProvider::class); |
105 | 156 |
|
106 | | -// parse test files up front, parent files are left for the rule to resolve |
107 | | -// the rule finds the class of a method in the stmts of the current file, as in a real run |
108 | | -$classMethodsByFile = []; |
109 | | -foreach ($testFilePaths as $testFilePath) { |
110 | | - $stmts = $testingParser->parseFileToDecoratedNodes($testFilePath); |
111 | | - $file = new File($testFilePath, (string) file_get_contents($testFilePath)); |
112 | | - $file->hydrateStmtsAndTokens($stmts, $stmts, []); |
113 | | - $classMethodsByFile[] = [$file, $betterNodeFinder->findInstanceOf($stmts, ClassMethod::class)]; |
114 | | -} |
| 157 | + $classMethodsByFile = []; |
| 158 | + $methodCount = 0; |
| 159 | + foreach ($testFilePaths as $testFilePath) { |
| 160 | + $stmts = $testingParser->parseFileToDecoratedNodes($testFilePath); |
115 | 161 |
|
116 | | -$cpuSeconds = static function (): float { |
117 | | - $usage = getrusage(); |
118 | | - return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000 |
119 | | - + $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000; |
120 | | -}; |
| 162 | + $file = new File($testFilePath, (string) file_get_contents($testFilePath)); |
| 163 | + $file->hydrateStmtsAndTokens($stmts, $stmts, []); |
121 | 164 |
|
122 | | -$wallStart = microtime(true); |
123 | | -$cpuStart = $cpuSeconds(); |
| 165 | + $classMethods = $betterNodeFinder->findInstanceOf($stmts, ClassMethod::class); |
| 166 | + $classMethodsByFile[] = [$file, $classMethods]; |
| 167 | + $methodCount += count($classMethods); |
| 168 | + } |
| 169 | + |
| 170 | + $wallStart = microtime(true); |
| 171 | + $cpuStart = $cpuSeconds(); |
124 | 172 |
|
125 | | -$changedCount = 0; |
126 | | -$classMethodCount = 0; |
127 | | -foreach ($classMethodsByFile as [$file, $classMethods]) { |
128 | | - $currentFileProvider->setFile($file); |
| 173 | + $changedCount = 0; |
| 174 | + foreach ($classMethodsByFile as [$file, $classMethods]) { |
| 175 | + $currentFileProvider->setFile($file); |
129 | 176 |
|
130 | | - foreach ($classMethods as $classMethod) { |
131 | | - ++$classMethodCount; |
132 | | - if ($rector->refactor($classMethod) instanceof ClassMethod) { |
133 | | - ++$changedCount; |
| 177 | + foreach ($classMethods as $classMethod) { |
| 178 | + if ($rector->refactor($classMethod) instanceof ClassMethod) { |
| 179 | + ++$changedCount; |
| 180 | + } |
134 | 181 | } |
135 | 182 | } |
136 | | -} |
137 | 183 |
|
138 | | -$cpu = $cpuSeconds() - $cpuStart; |
139 | | -$wall = microtime(true) - $wallStart; |
| 184 | + $cpu = $cpuSeconds() - $cpuStart; |
| 185 | + $wall = microtime(true) - $wallStart; |
| 186 | + |
| 187 | + printf( |
| 188 | + '%s: test classes: %d, test methods: %d, changed: %d%s', |
| 189 | + $scenarioName, |
| 190 | + count($testFilePaths), |
| 191 | + $methodCount, |
| 192 | + $changedCount, |
| 193 | + PHP_EOL |
| 194 | + ); |
| 195 | + printf('CPU: %.4f s (%.3f ms/method)%s', $cpu, $cpu / $methodCount * 1000, PHP_EOL); |
| 196 | + printf('wall: %.4f s%s', $wall, PHP_EOL); |
| 197 | + |
| 198 | + if ($cpu > MAX_CPU_SECONDS) { |
| 199 | + printf('CPU %.4f s is over the %.1f s limit%s', $cpu, MAX_CPU_SECONDS, PHP_EOL); |
| 200 | + $isFailed = true; |
| 201 | + } |
| 202 | + |
| 203 | +} |
140 | 204 |
|
141 | 205 | array_map('unlink', glob($directory . '/*.php')); |
142 | 206 | @rmdir($directory); |
143 | 207 |
|
144 | | -printf('test classes: %d, test methods: %d, changed: %d%s', count($testFilePaths), $classMethodCount, $changedCount, PHP_EOL); |
145 | | -printf('CPU: %.4f s (%.3f ms/method)%s', $cpu, $cpu / $classMethodCount * 1000, PHP_EOL); |
146 | | -printf('wall: %.4f s%s', $wall, PHP_EOL); |
147 | | - |
148 | | -if ($cpu > MAX_CPU_SECONDS) { |
149 | | - printf('CPU %.4f s is over the %.1f s limit%s', $cpu, MAX_CPU_SECONDS, PHP_EOL); |
150 | | - exit(1); |
151 | | -} |
| 208 | +exit($isFailed ? 1 : 0); |
0 commit comments