|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Benchmark for AddDoesNotPerformAssertionToNonAssertingTestRector on test classes with own lifecycle methods. |
| 7 | + * |
| 8 | + * Every test class has its own setUp() and tearDown(), every other one asserts in setUp(). The class is taken from |
| 9 | + * the current file, so the rule looks into its lifecycle methods for every test method. |
| 10 | + * Run on both branches to compare. CPU (user+sys) is the reliable metric; wall is noisy. |
| 11 | + */ |
| 12 | + |
| 13 | +use PhpParser\Node\Stmt\ClassMethod; |
| 14 | +use Rector\Application\Provider\CurrentFileProvider; |
| 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 | +use Rector\ValueObject\Application\File; |
| 20 | + |
| 21 | +require __DIR__ . '/../vendor/autoload.php'; |
| 22 | + |
| 23 | +const TEST_CLASSES = 300; // every other one asserts in setUp() |
| 24 | +const TEST_METHODS = 6; // non-asserting test methods per test class |
| 25 | +const MAX_CPU_SECONDS = 1.0; // fails in CI above this |
| 26 | + |
| 27 | +$directory = sys_get_temp_dir() . '/rector_phpunit_own_lifecycle_bench_' . getmypid(); |
| 28 | +@mkdir($directory); |
| 29 | + |
| 30 | +// make generated classes visible to PHPStan reflection |
| 31 | +spl_autoload_register(static function (string $class) use ($directory): void { |
| 32 | + if (! str_starts_with($class, 'Bench\\')) { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + $filePath = $directory . '/' . substr($class, 6) . '.php'; |
| 37 | + if (is_file($filePath)) { |
| 38 | + require_once $filePath; |
| 39 | + } |
| 40 | +}); |
| 41 | + |
| 42 | +$testFilePaths = []; |
| 43 | + |
| 44 | +for ($test = 0; $test < TEST_CLASSES; ++$test) { |
| 45 | + $isAsserting = $test % 2 === 0; |
| 46 | + $className = sprintf('%sTest%d', $isAsserting ? 'Asserting' : 'NonAsserting', $test); |
| 47 | + |
| 48 | + $setUpBody = $isAsserting ? '$this->assertTrue(true);' : '$this->ready = true;'; |
| 49 | + $methods = " protected function setUp(): void\n {\n parent::setUp();\n {$setUpBody}\n }\n\n"; |
| 50 | + $methods .= " protected function tearDown(): void\n {\n \$this->ready = null;\n parent::tearDown();\n }\n\n"; |
| 51 | + for ($testMethod = 0; $testMethod < TEST_METHODS; ++$testMethod) { |
| 52 | + $methods .= " public function testSomething{$testMethod}(): void\n {\n \$value = {$testMethod};\n }\n\n"; |
| 53 | + } |
| 54 | + |
| 55 | + $filePath = $directory . '/' . $className . '.php'; |
| 56 | + file_put_contents( |
| 57 | + $filePath, |
| 58 | + "<?php\n\nnamespace Bench;\n\nfinal class {$className} extends \\PHPUnit\\Framework\\TestCase\n{\n protected \$ready;\n\n{$methods}}\n" |
| 59 | + ); |
| 60 | + |
| 61 | + $testFilePaths[] = $filePath; |
| 62 | +} |
| 63 | + |
| 64 | +$rectorConfig = (new LazyContainerFactory())->create(); |
| 65 | +$rectorConfig->boot(); |
| 66 | + |
| 67 | +/** @var TestingParser $testingParser */ |
| 68 | +$testingParser = $rectorConfig->make(TestingParser::class); |
| 69 | + |
| 70 | +/** @var BetterNodeFinder $betterNodeFinder */ |
| 71 | +$betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class); |
| 72 | + |
| 73 | +$rector = $rectorConfig->make(AddDoesNotPerformAssertionToNonAssertingTestRector::class); |
| 74 | + |
| 75 | +/** @var CurrentFileProvider $currentFileProvider */ |
| 76 | +$currentFileProvider = $rectorConfig->make(CurrentFileProvider::class); |
| 77 | + |
| 78 | +// the rule finds the class of a method in the stmts of the current file, as in a real run |
| 79 | +$classMethodsByFile = []; |
| 80 | +foreach ($testFilePaths as $testFilePath) { |
| 81 | + $stmts = $testingParser->parseFileToDecoratedNodes($testFilePath); |
| 82 | + $file = new File($testFilePath, (string) file_get_contents($testFilePath)); |
| 83 | + $file->hydrateStmtsAndTokens($stmts, $stmts, []); |
| 84 | + $classMethodsByFile[] = [$file, $betterNodeFinder->findInstanceOf($stmts, ClassMethod::class)]; |
| 85 | +} |
| 86 | + |
| 87 | +$cpuSeconds = static function (): float { |
| 88 | + $usage = getrusage(); |
| 89 | + return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000 |
| 90 | + + $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000; |
| 91 | +}; |
| 92 | + |
| 93 | +$wallStart = microtime(true); |
| 94 | +$cpuStart = $cpuSeconds(); |
| 95 | + |
| 96 | +$changedCount = 0; |
| 97 | +$classMethodCount = 0; |
| 98 | +foreach ($classMethodsByFile as [$file, $classMethods]) { |
| 99 | + $currentFileProvider->setFile($file); |
| 100 | + |
| 101 | + foreach ($classMethods as $classMethod) { |
| 102 | + ++$classMethodCount; |
| 103 | + if ($rector->refactor($classMethod) instanceof ClassMethod) { |
| 104 | + ++$changedCount; |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +$cpu = $cpuSeconds() - $cpuStart; |
| 110 | +$wall = microtime(true) - $wallStart; |
| 111 | + |
| 112 | +array_map('unlink', glob($directory . '/*.php')); |
| 113 | +@rmdir($directory); |
| 114 | + |
| 115 | +printf('test classes: %d, test methods: %d, changed: %d%s', count($testFilePaths), $classMethodCount, $changedCount, PHP_EOL); |
| 116 | +printf('CPU: %.4f s (%.3f ms/method)%s', $cpu, $cpu / $classMethodCount * 1000, PHP_EOL); |
| 117 | +printf('wall: %.4f s%s', $wall, PHP_EOL); |
| 118 | + |
| 119 | +if ($cpu > MAX_CPU_SECONDS) { |
| 120 | + printf('CPU %.4f s is over the %.1f s limit%s', $cpu, MAX_CPU_SECONDS, PHP_EOL); |
| 121 | + exit(1); |
| 122 | +} |
0 commit comments