|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Benchmark for AddDoesNotPerformAssertionToNonAssertingTestRector on many test classes sharing helper methods. |
| 7 | + * |
| 8 | + * Each test method calls a helper declared in an abstract parent, and one calls "parent::setUp()" that is only |
| 9 | + * declared in TestCase. This measures the containsAssertCall() cache and the "parent::" resolution. |
| 10 | + * |
| 11 | + * Run on both branches to compare. CPU (user+sys) is the reliable metric; wall is noisy. |
| 12 | + */ |
| 13 | + |
| 14 | +use PhpParser\Node\Stmt\Class_; |
| 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 | + |
| 20 | +require __DIR__ . '/../vendor/autoload.php'; |
| 21 | + |
| 22 | +const PARENTS = 5; // abstract parents extending TestCase, none overrides setUp() |
| 23 | +const TESTS_PER_PARENT = 60; // test classes extending each parent |
| 24 | +const TEST_METHODS = 8; // non-asserting test methods per test class |
| 25 | +const HELPER_STATEMENTS = 5; // statements per helper, to make printing the body realistic |
| 26 | +const MAX_CPU_SECONDS = 1.0; // fails in CI above this |
| 27 | + |
| 28 | +$directory = sys_get_temp_dir() . '/rector_phpunit_cache_bench_' . getmypid(); |
| 29 | +@mkdir($directory); |
| 30 | + |
| 31 | +$writeClass = static function (string $className, string $body) use ($directory): string { |
| 32 | + $filePath = $directory . '/' . $className . '.php'; |
| 33 | + file_put_contents($filePath, "<?php\n\nnamespace Bench;\n\n" . $body); |
| 34 | + return $filePath; |
| 35 | +}; |
| 36 | + |
| 37 | +// make generated classes visible to PHPStan reflection |
| 38 | +spl_autoload_register(static function (string $class) use ($directory): void { |
| 39 | + if (! str_starts_with($class, 'Bench\\')) { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + $filePath = $directory . '/' . substr($class, 6) . '.php'; |
| 44 | + if (is_file($filePath)) { |
| 45 | + require_once $filePath; |
| 46 | + } |
| 47 | +}); |
| 48 | + |
| 49 | +$testFilePaths = []; |
| 50 | + |
| 51 | +for ($parent = 0; $parent < PARENTS; ++$parent) { |
| 52 | + $parentClassName = sprintf('AbstractCacheParent%d', $parent); |
| 53 | + |
| 54 | + $methods = ''; |
| 55 | + for ($helper = 0; $helper < TEST_METHODS; ++$helper) { |
| 56 | + $statements = ''; |
| 57 | + for ($statement = 0; $statement < HELPER_STATEMENTS; ++$statement) { |
| 58 | + $statements .= " \$items = array_map(fn (\$item) => \$item + {$statement}, \$items);\n"; |
| 59 | + } |
| 60 | + |
| 61 | + $methods .= " protected function helper{$helper}(array \$items): array\n {\n{$statements} return \$items;\n }\n\n"; |
| 62 | + } |
| 63 | + |
| 64 | + $writeClass( |
| 65 | + $parentClassName, |
| 66 | + "abstract class {$parentClassName} extends \PHPUnit\Framework\TestCase\n{\n{$methods}}\n" |
| 67 | + ); |
| 68 | + |
| 69 | + for ($test = 0; $test < TESTS_PER_PARENT; ++$test) { |
| 70 | + $className = sprintf('CacheParent%dTest%d', $parent, $test); |
| 71 | + |
| 72 | + $methods = " public function testRestart(): void\n {\n parent::setUp();\n }\n\n"; |
| 73 | + for ($testMethod = 0; $testMethod < TEST_METHODS; ++$testMethod) { |
| 74 | + $methods .= " public function testSomething{$testMethod}(): void\n {\n \$value = \$this->helper{$testMethod}([1, 2, 3]);\n }\n\n"; |
| 75 | + } |
| 76 | + |
| 77 | + $testFilePaths[] = $writeClass( |
| 78 | + $className, |
| 79 | + "final class {$className} extends {$parentClassName}\n{\n{$methods}}\n" |
| 80 | + ); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +$rectorConfig = (new LazyContainerFactory())->create(); |
| 85 | +$rectorConfig->boot(); |
| 86 | + |
| 87 | +/** @var TestingParser $testingParser */ |
| 88 | +$testingParser = $rectorConfig->make(TestingParser::class); |
| 89 | + |
| 90 | +/** @var BetterNodeFinder $betterNodeFinder */ |
| 91 | +$betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class); |
| 92 | + |
| 93 | +$rector = $rectorConfig->make(AddDoesNotPerformAssertionToNonAssertingTestRector::class); |
| 94 | + |
| 95 | +// parse test files up front, parent files are left for the rule to resolve |
| 96 | +$classes = []; |
| 97 | +foreach ($testFilePaths as $testFilePath) { |
| 98 | + $stmts = $testingParser->parseFileToDecoratedNodes($testFilePath); |
| 99 | + $classes = [...$classes, ...$betterNodeFinder->findInstanceOf($stmts, Class_::class)]; |
| 100 | +} |
| 101 | + |
| 102 | +$cpuSeconds = static function (): float { |
| 103 | + $usage = getrusage(); |
| 104 | + return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000 |
| 105 | + + $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000; |
| 106 | +}; |
| 107 | + |
| 108 | +$wallStart = microtime(true); |
| 109 | +$cpuStart = $cpuSeconds(); |
| 110 | + |
| 111 | +$changedCount = 0; |
| 112 | +foreach ($classes as $class) { |
| 113 | + if ($rector->refactor($class) instanceof Class_) { |
| 114 | + ++$changedCount; |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +$cpu = $cpuSeconds() - $cpuStart; |
| 119 | +$wall = microtime(true) - $wallStart; |
| 120 | + |
| 121 | +array_map('unlink', glob($directory . '/*.php')); |
| 122 | +@rmdir($directory); |
| 123 | + |
| 124 | +printf('test classes: %d, changed: %d%s', count($classes), $changedCount, PHP_EOL); |
| 125 | +printf('CPU: %.4f s (%.3f ms/class)%s', $cpu, $cpu / count($classes) * 1000, PHP_EOL); |
| 126 | +printf('wall: %.4f s%s', $wall, PHP_EOL); |
| 127 | + |
| 128 | +if ($cpu > MAX_CPU_SECONDS) { |
| 129 | + printf('CPU %.4f s is over the %.1f s limit%s', $cpu, MAX_CPU_SECONDS, PHP_EOL); |
| 130 | + exit(1); |
| 131 | +} |
0 commit comments