|
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 | */ |
|
26 | 33 | const FILLER_METHODS = 15; // extra methods per parent class, to make parsing realistic |
27 | 34 | const MAX_CPU_SECONDS = 1.0; // fails in CI above this |
28 | 35 |
|
29 | | -$directory = sys_get_temp_dir() . '/rector_phpunit_bench_' . getmypid(); |
30 | | -@mkdir($directory); |
| 36 | +const SCENARIOS = [ |
| 37 | + 'inherited' => [ |
| 38 | + 'own_set_up' => false, |
| 39 | + 'asserting_hooks' => false, |
| 40 | + ], |
| 41 | + 'own set up' => [ |
| 42 | + 'own_set_up' => true, |
| 43 | + 'asserting_hooks' => false, |
| 44 | + ], |
| 45 | + 'asserting hooks' => [ |
| 46 | + 'own_set_up' => false, |
| 47 | + 'asserting_hooks' => true, |
| 48 | + ], |
| 49 | +]; |
31 | 50 |
|
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; |
| 51 | +$cpuSeconds = static function (): float { |
| 52 | + $usage = getrusage(); |
| 53 | + return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000 |
| 54 | + + $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000; |
36 | 55 | }; |
37 | 56 |
|
| 57 | +$directory = sys_get_temp_dir() . '/rector_phpunit_bench_' . getmypid(); |
| 58 | +@mkdir($directory); |
| 59 | + |
38 | 60 | // make generated classes visible to PHPStan reflection |
39 | 61 | spl_autoload_register(static function (string $class) use ($directory): void { |
40 | 62 | if (! str_starts_with($class, 'Bench\\')) { |
|
47 | 69 | } |
48 | 70 | }); |
49 | 71 |
|
50 | | -$testFilePaths = []; |
| 72 | +$isFailed = false; |
| 73 | +$scenarioIndex = 0; |
51 | 74 |
|
52 | | -for ($chain = 0; $chain < CHAINS; ++$chain) { |
53 | | - $parentClassName = '\PHPUnit\Framework\TestCase'; |
| 75 | +foreach (SCENARIOS as $scenarioName => $scenario) { |
| 76 | + // own class names per scenario, so no scenario profits from the parsed files of another |
| 77 | + $prefix = 'S' . $scenarioIndex++; |
54 | 78 |
|
55 | | - for ($level = 0; $level < DEPTH; ++$level) { |
56 | | - $className = sprintf('AbstractChain%dLevel%d', $chain, $level); |
| 79 | + $writeClass = static function (string $className, string $body) use ($directory): string { |
| 80 | + $filePath = $directory . '/' . $className . '.php'; |
| 81 | + file_put_contents($filePath, "<?php\n\nnamespace Bench;\n\n" . $body); |
| 82 | + return $filePath; |
| 83 | + }; |
57 | 84 |
|
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"; |
| 85 | + $testFilePaths = []; |
62 | 86 |
|
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 | | - } |
| 87 | + for ($chain = 0; $chain < CHAINS; ++$chain) { |
| 88 | + $parentClassName = '\PHPUnit\Framework\TestCase'; |
66 | 89 |
|
67 | | - $writeClass( |
68 | | - $className, |
69 | | - "abstract class {$className} extends {$parentClassName}\n{\n protected \$ready;\n protected \$cleanup;\n\n{$methods}}\n" |
70 | | - ); |
| 90 | + for ($level = 0; $level < DEPTH; ++$level) { |
| 91 | + $className = sprintf('%sAbstractChain%dLevel%d', $prefix, $chain, $level); |
71 | 92 |
|
72 | | - $parentClassName = $className; |
73 | | - } |
| 93 | + $methods = " protected function setUp(): void\n {\n parent::setUp();\n \$this->prepare{$level}();\n }\n\n"; |
| 94 | + $methods .= " protected function tearDown(): void\n {\n \$this->cleanup = null;\n parent::tearDown();\n }\n\n"; |
| 95 | + $methods .= " public function testInherited(): void\n {\n \$value = {$level};\n }\n\n"; |
| 96 | + $methods .= " private function prepare{$level}(): void\n {\n \$this->ready = true;\n }\n\n"; |
| 97 | + |
| 98 | + if ($scenario['asserting_hooks'] && $level === 0) { |
| 99 | + // the asserting hook is in the top parent, furthest away from the test classes |
| 100 | + $methods .= match ($chain % 3) { |
| 101 | + 0 => " protected function assertPostConditions(): void\n {\n \$this->assertTrue(\$this->ready);\n }\n\n", |
| 102 | + 1 => " #[\PHPUnit\Framework\Attributes\Before]\n protected function checkReady(): void\n {\n \$this->assertNotNull(\$this->cleanup);\n }\n\n", |
| 103 | + default => " protected function assertPreConditions(): void\n {\n \$this->assertFalse(\$this->ready);\n }\n\n", |
| 104 | + }; |
| 105 | + } |
74 | 106 |
|
75 | | - for ($test = 0; $test < TESTS_PER_CHAIN; ++$test) { |
76 | | - $className = sprintf('Chain%dTest%d', $chain, $test); |
| 107 | + for ($filler = 0; $filler < FILLER_METHODS; ++$filler) { |
| 108 | + $methods .= " protected function helper{$filler}(array \$items): array\n {\n return array_map(fn (\$item) => \$item * {$filler}, \$items);\n }\n\n"; |
| 109 | + } |
77 | 110 |
|
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"; |
| 111 | + $writeClass( |
| 112 | + $className, |
| 113 | + "abstract class {$className} extends {$parentClassName}\n{\n protected \$ready;\n protected \$cleanup;\n\n{$methods}}\n" |
| 114 | + ); |
| 115 | + |
| 116 | + $parentClassName = $className; |
81 | 117 | } |
82 | 118 |
|
83 | | - $testFilePaths[] = $writeClass( |
84 | | - $className, |
85 | | - "final class {$className} extends {$parentClassName}\n{\n{$methods}}\n" |
86 | | - ); |
| 119 | + for ($test = 0; $test < TESTS_PER_CHAIN; ++$test) { |
| 120 | + $className = sprintf('%sChain%dTest%d', $prefix, $chain, $test); |
| 121 | + |
| 122 | + $methods = ''; |
| 123 | + if ($scenario['own_set_up']) { |
| 124 | + $methods .= " protected function setUp(): void\n {\n parent::setUp();\n \$this->ready = true;\n }\n\n"; |
| 125 | + } |
| 126 | + |
| 127 | + $methods .= " public function testInherited(): void\n {\n parent::testInherited();\n }\n\n"; |
| 128 | + for ($testMethod = 0; $testMethod < TEST_METHODS; ++$testMethod) { |
| 129 | + $methods .= " public function testSomething{$testMethod}(): void\n {\n \$value = \$this->helper{$testMethod}([1, 2, 3]);\n }\n\n"; |
| 130 | + } |
| 131 | + |
| 132 | + $testFilePaths[] = $writeClass( |
| 133 | + $className, |
| 134 | + "final class {$className} extends {$parentClassName}\n{\n{$methods}}\n" |
| 135 | + ); |
| 136 | + } |
87 | 137 | } |
88 | | -} |
89 | 138 |
|
90 | | -$rectorConfig = (new LazyContainerFactory())->create(); |
91 | | -$rectorConfig->boot(); |
| 139 | + $rectorConfig = (new LazyContainerFactory())->create(); |
| 140 | + $rectorConfig->boot(); |
92 | 141 |
|
93 | | -/** @var TestingParser $testingParser */ |
94 | | -$testingParser = $rectorConfig->make(TestingParser::class); |
| 142 | + /** @var TestingParser $testingParser */ |
| 143 | + $testingParser = $rectorConfig->make(TestingParser::class); |
95 | 144 |
|
96 | | -/** @var BetterNodeFinder $betterNodeFinder */ |
97 | | -$betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class); |
| 145 | + /** @var BetterNodeFinder $betterNodeFinder */ |
| 146 | + $betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class); |
98 | 147 |
|
99 | | -$rector = $rectorConfig->make(AddDoesNotPerformAssertionToNonAssertingTestRector::class); |
| 148 | + $rector = $rectorConfig->make(AddDoesNotPerformAssertionToNonAssertingTestRector::class); |
100 | 149 |
|
101 | | -// parse test files up front, parent files are left for the rule to resolve |
102 | | -$classes = []; |
103 | | -foreach ($testFilePaths as $testFilePath) { |
104 | | - $stmts = $testingParser->parseFileToDecoratedNodes($testFilePath); |
105 | | - $classes = [...$classes, ...$betterNodeFinder->findInstanceOf($stmts, Class_::class)]; |
106 | | -} |
| 150 | + // parse test files up front, parent files are left for the rule to resolve |
| 151 | + $classes = []; |
| 152 | + foreach ($testFilePaths as $testFilePath) { |
| 153 | + $stmts = $testingParser->parseFileToDecoratedNodes($testFilePath); |
| 154 | + $classes = [...$classes, ...$betterNodeFinder->findInstanceOf($stmts, Class_::class)]; |
| 155 | + } |
107 | 156 |
|
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 | | -}; |
| 157 | + $wallStart = microtime(true); |
| 158 | + $cpuStart = $cpuSeconds(); |
113 | 159 |
|
114 | | -$wallStart = microtime(true); |
115 | | -$cpuStart = $cpuSeconds(); |
| 160 | + $changedCount = 0; |
| 161 | + foreach ($classes as $class) { |
| 162 | + if ($rector->refactor($class) instanceof Class_) { |
| 163 | + ++$changedCount; |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + $cpu = $cpuSeconds() - $cpuStart; |
| 168 | + $wall = microtime(true) - $wallStart; |
| 169 | + |
| 170 | + printf('%s: test classes: %d, changed: %d%s', $scenarioName, count($classes), $changedCount, PHP_EOL); |
| 171 | + printf('CPU: %.4f s (%.3f ms/class)%s', $cpu, $cpu / count($classes) * 1000, PHP_EOL); |
| 172 | + printf('wall: %.4f s%s', $wall, PHP_EOL); |
116 | 173 |
|
117 | | -$changedCount = 0; |
118 | | -foreach ($classes as $class) { |
119 | | - if ($rector->refactor($class) instanceof Class_) { |
120 | | - ++$changedCount; |
| 174 | + if ($cpu > MAX_CPU_SECONDS) { |
| 175 | + printf('CPU %.4f s is over the %.1f s limit%s', $cpu, MAX_CPU_SECONDS, PHP_EOL); |
| 176 | + $isFailed = true; |
121 | 177 | } |
122 | | -} |
123 | 178 |
|
124 | | -$cpu = $cpuSeconds() - $cpuStart; |
125 | | -$wall = microtime(true) - $wallStart; |
| 179 | +} |
126 | 180 |
|
127 | 181 | array_map('unlink', glob($directory . '/*.php')); |
128 | 182 | @rmdir($directory); |
129 | 183 |
|
130 | | -printf('test classes: %d, changed: %d%s', count($classes), $changedCount, PHP_EOL); |
131 | | -printf('CPU: %.4f s (%.3f ms/class)%s', $cpu, $cpu / count($classes) * 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 | | -} |
| 184 | +exit($isFailed ? 1 : 0); |
0 commit comments