Skip to content

Commit 065f66a

Browse files
committed
[bench] Add AddDoesNotPerformAssertionToNonAssertingTestRector bench on parent lifecycle methods
1 parent fb40fd2 commit 065f66a

2 files changed

Lines changed: 143 additions & 0 deletions

File tree

‎.github/workflows/code_analysis.yaml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ jobs:
5252
name: 'Bench framework test cases'
5353
run: php bench/does_not_perform_assertion_framework_test_case_bench.php
5454

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

0 commit comments

Comments
 (0)