|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Benchmark for AddDoesNotPerformAssertionToNonAssertingTestRector on abstract and final test classes. |
| 7 | + * |
| 8 | + * Half of the test classes are abstract, so the abstract class check runs on every test method. |
| 9 | + * Run on both branches to compare. CPU (user+sys) is the reliable metric; wall is noisy. |
| 10 | + */ |
| 11 | + |
| 12 | +use PhpParser\Node\Stmt\ClassMethod; |
| 13 | +use Rector\DependencyInjection\LazyContainerFactory; |
| 14 | +use Rector\PhpParser\Node\BetterNodeFinder; |
| 15 | +use Rector\PHPUnit\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector; |
| 16 | +use Rector\Testing\TestingParser\TestingParser; |
| 17 | + |
| 18 | +require __DIR__ . '/../vendor/autoload.php'; |
| 19 | + |
| 20 | +const TEST_CLASSES = 300; // every other one is abstract |
| 21 | +const TEST_METHODS = 6; // non-asserting test methods per test class |
| 22 | +const MAX_CPU_SECONDS = 1.0; // fails in CI above this |
| 23 | + |
| 24 | +$directory = sys_get_temp_dir() . '/rector_phpunit_abstract_bench_' . getmypid(); |
| 25 | +@mkdir($directory); |
| 26 | + |
| 27 | +// make generated classes visible to PHPStan reflection |
| 28 | +spl_autoload_register(static function (string $class) use ($directory): void { |
| 29 | + if (! str_starts_with($class, 'Bench\\')) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + $filePath = $directory . '/' . substr($class, 6) . '.php'; |
| 34 | + if (is_file($filePath)) { |
| 35 | + require_once $filePath; |
| 36 | + } |
| 37 | +}); |
| 38 | + |
| 39 | +$testFilePaths = []; |
| 40 | + |
| 41 | +for ($test = 0; $test < TEST_CLASSES; ++$test) { |
| 42 | + $isAbstract = $test % 2 === 0; |
| 43 | + $className = sprintf('%sTest%d', $isAbstract ? 'Abstract' : 'Final', $test); |
| 44 | + |
| 45 | + $methods = ''; |
| 46 | + for ($testMethod = 0; $testMethod < TEST_METHODS; ++$testMethod) { |
| 47 | + $methods .= " public function testSomething{$testMethod}(): void\n {\n \$value = {$testMethod};\n }\n\n"; |
| 48 | + } |
| 49 | + |
| 50 | + $filePath = $directory . '/' . $className . '.php'; |
| 51 | + file_put_contents( |
| 52 | + $filePath, |
| 53 | + "<?php\n\nnamespace Bench;\n\n" . ($isAbstract ? 'abstract' : 'final') |
| 54 | + . " class {$className} extends \PHPUnit\Framework\TestCase\n{\n{$methods}}\n" |
| 55 | + ); |
| 56 | + |
| 57 | + $testFilePaths[] = $filePath; |
| 58 | +} |
| 59 | + |
| 60 | +$rectorConfig = (new LazyContainerFactory())->create(); |
| 61 | +$rectorConfig->boot(); |
| 62 | + |
| 63 | +/** @var TestingParser $testingParser */ |
| 64 | +$testingParser = $rectorConfig->make(TestingParser::class); |
| 65 | + |
| 66 | +/** @var BetterNodeFinder $betterNodeFinder */ |
| 67 | +$betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class); |
| 68 | + |
| 69 | +$rector = $rectorConfig->make(AddDoesNotPerformAssertionToNonAssertingTestRector::class); |
| 70 | + |
| 71 | +$classMethods = []; |
| 72 | +foreach ($testFilePaths as $testFilePath) { |
| 73 | + $stmts = $testingParser->parseFileToDecoratedNodes($testFilePath); |
| 74 | + $classMethods = [...$classMethods, ...$betterNodeFinder->findInstanceOf($stmts, ClassMethod::class)]; |
| 75 | +} |
| 76 | + |
| 77 | +$cpuSeconds = static function (): float { |
| 78 | + $usage = getrusage(); |
| 79 | + return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000 |
| 80 | + + $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000; |
| 81 | +}; |
| 82 | + |
| 83 | +$wallStart = microtime(true); |
| 84 | +$cpuStart = $cpuSeconds(); |
| 85 | + |
| 86 | +$changedCount = 0; |
| 87 | +foreach ($classMethods as $classMethod) { |
| 88 | + if ($rector->refactor($classMethod) instanceof ClassMethod) { |
| 89 | + ++$changedCount; |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +$cpu = $cpuSeconds() - $cpuStart; |
| 94 | +$wall = microtime(true) - $wallStart; |
| 95 | + |
| 96 | +array_map('unlink', glob($directory . '/*.php')); |
| 97 | +@rmdir($directory); |
| 98 | + |
| 99 | +printf('test classes: %d, test methods: %d, changed: %d%s', count($testFilePaths), count($classMethods), $changedCount, PHP_EOL); |
| 100 | +printf('CPU: %.4f s (%.3f ms/method)%s', $cpu, $cpu / count($classMethods) * 1000, PHP_EOL); |
| 101 | +printf('wall: %.4f s%s', $wall, PHP_EOL); |
| 102 | + |
| 103 | +if ($cpu > MAX_CPU_SECONDS) { |
| 104 | + printf('CPU %.4f s is over the %.1f s limit%s', $cpu, MAX_CPU_SECONDS, PHP_EOL); |
| 105 | + exit(1); |
| 106 | +} |
0 commit comments