|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Benchmark for AddDoesNotPerformAssertionToNonAssertingTestRector on calls into PHPUnit's own classes. |
| 7 | + * |
| 8 | + * Every test class extends a base class without setUp(), so "parent::setUp()" lands in PHPUnit's TestCase, and its |
| 9 | + * tests call "$this->markTestSkipped()", declared in PHPUnit's Assert. These files are huge and must not be parsed. |
| 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 TEST_CLASSES = 300; |
| 22 | +const TEST_METHODS = 6; // non-asserting test methods per test class |
| 23 | +const MAX_CPU_SECONDS = 1.0; // fails in CI above this |
| 24 | + |
| 25 | +$directory = sys_get_temp_dir() . '/rector_phpunit_framework_test_case_bench_' . getmypid(); |
| 26 | +@mkdir($directory); |
| 27 | + |
| 28 | +// make generated classes visible to PHPStan reflection |
| 29 | +spl_autoload_register(static function (string $class) use ($directory): void { |
| 30 | + if (! str_starts_with($class, 'Bench\\')) { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + $filePath = $directory . '/' . substr($class, 6) . '.php'; |
| 35 | + if (is_file($filePath)) { |
| 36 | + require_once $filePath; |
| 37 | + } |
| 38 | +}); |
| 39 | + |
| 40 | +// the base class does not override setUp(), so calls to it resolve to PHPUnit's TestCase |
| 41 | +file_put_contents( |
| 42 | + $directory . '/AbstractBaseTestCase.php', |
| 43 | + "<?php\n\nnamespace Bench;\n\nabstract class AbstractBaseTestCase extends \\PHPUnit\\Framework\\TestCase\n{\n protected \$ready;\n}\n" |
| 44 | +); |
| 45 | + |
| 46 | +$testFilePaths = []; |
| 47 | + |
| 48 | +for ($test = 0; $test < TEST_CLASSES; ++$test) { |
| 49 | + $className = sprintf('FrameworkTest%d', $test); |
| 50 | + |
| 51 | + $methods = " protected function setUp(): void\n {\n parent::setUp();\n \$this->ready = true;\n }\n\n"; |
| 52 | + for ($testMethod = 0; $testMethod < TEST_METHODS; ++$testMethod) { |
| 53 | + $methods .= " public function testSomething{$testMethod}(): void\n {\n \$this->markTestSkipped('later');\n }\n\n"; |
| 54 | + } |
| 55 | + |
| 56 | + $filePath = $directory . '/' . $className . '.php'; |
| 57 | + file_put_contents( |
| 58 | + $filePath, |
| 59 | + "<?php\n\nnamespace Bench;\n\nfinal class {$className} extends AbstractBaseTestCase\n{\n{$methods}}\n" |
| 60 | + ); |
| 61 | + |
| 62 | + $testFilePaths[] = $filePath; |
| 63 | +} |
| 64 | + |
| 65 | +$rectorConfig = (new LazyContainerFactory())->create(); |
| 66 | +$rectorConfig->boot(); |
| 67 | + |
| 68 | +/** @var TestingParser $testingParser */ |
| 69 | +$testingParser = $rectorConfig->make(TestingParser::class); |
| 70 | + |
| 71 | +/** @var BetterNodeFinder $betterNodeFinder */ |
| 72 | +$betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class); |
| 73 | + |
| 74 | +$rector = $rectorConfig->make(AddDoesNotPerformAssertionToNonAssertingTestRector::class); |
| 75 | + |
| 76 | +$classes = []; |
| 77 | +foreach ($testFilePaths as $testFilePath) { |
| 78 | + $stmts = $testingParser->parseFileToDecoratedNodes($testFilePath); |
| 79 | + $classes = [...$classes, ...$betterNodeFinder->findInstanceOf($stmts, Class_::class)]; |
| 80 | +} |
| 81 | + |
| 82 | +$cpuSeconds = static function (): float { |
| 83 | + $usage = getrusage(); |
| 84 | + return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000 |
| 85 | + + $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000; |
| 86 | +}; |
| 87 | + |
| 88 | +$wallStart = microtime(true); |
| 89 | +$cpuStart = $cpuSeconds(); |
| 90 | + |
| 91 | +$changedCount = 0; |
| 92 | +foreach ($classes as $class) { |
| 93 | + if ($rector->refactor($class) instanceof Class_) { |
| 94 | + ++$changedCount; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +$cpu = $cpuSeconds() - $cpuStart; |
| 99 | +$wall = microtime(true) - $wallStart; |
| 100 | + |
| 101 | +array_map('unlink', glob($directory . '/*.php')); |
| 102 | +@rmdir($directory); |
| 103 | + |
| 104 | +printf('test classes: %d, changed: %d%s', count($classes), $changedCount, PHP_EOL); |
| 105 | +printf('CPU: %.4f s (%.3f ms/class)%s', $cpu, $cpu / count($classes) * 1000, PHP_EOL); |
| 106 | +printf('wall: %.4f s%s', $wall, PHP_EOL); |
| 107 | + |
| 108 | +if ($cpu > MAX_CPU_SECONDS) { |
| 109 | + printf('CPU %.4f s is over the %.1f s limit%s', $cpu, MAX_CPU_SECONDS, PHP_EOL); |
| 110 | + exit(1); |
| 111 | +} |
0 commit comments