diff --git a/bench/phpunit_testcase_call_guard_bench.php b/bench/phpunit_testcase_call_guard_bench.php new file mode 100644 index 00000000..92d93a63 --- /dev/null +++ b/bench/phpunit_testcase_call_guard_bench.php @@ -0,0 +1,89 @@ +create(); +$rectorConfig->boot(); + +/** @var TestingParser $testingParser */ +$testingParser = $rectorConfig->make(TestingParser::class); + +/** @var BetterNodeFinder $betterNodeFinder */ +$betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class); + +$rector = $rectorConfig->make(AssertInstanceOfComparisonRector::class); + +$source = "handle{$i}(\$value, {$i});\n"; +} + +$sourceFilePath = tempnam(sys_get_temp_dir(), 'bench_') . '.php'; +file_put_contents($sourceFilePath, $source); + +$stmts = $testingParser->parseFileToDecoratedNodes($sourceFilePath); + +/** @var MethodCall[] $methodCalls */ +$methodCalls = $betterNodeFinder->findInstanceOf($stmts, MethodCall::class); + +$cpuSeconds = static function (): float { + $usage = getrusage(); + return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000 + + $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000; +}; + +$run = static function () use ($rector, $methodCalls): void { + foreach ($methodCalls as $methodCall) { + $rector->refactor($methodCall); + } +}; + +// warm up caches once, excluded from timing +$run(); + +$wallStart = microtime(true); +$cpuStart = $cpuSeconds(); + +for ($repeat = 0; $repeat < REPEAT; ++$repeat) { + $run(); +} + +$cpu = $cpuSeconds() - $cpuStart; +$wall = microtime(true) - $wallStart; + +@unlink($sourceFilePath); + +$totalCalls = REPEAT * count($methodCalls); + +printf('calls: %d, repeats: %d, refactor() calls: %d%s', count($methodCalls), REPEAT, $totalCalls, PHP_EOL); +printf('CPU: %.4f s (%.3f us/call)%s', $cpu, $cpu / $totalCalls * 1_000_000, PHP_EOL); +printf('wall: %.4f s%s', $wall, PHP_EOL); diff --git a/src/NodeAnalyzer/TestsNodeAnalyzer.php b/src/NodeAnalyzer/TestsNodeAnalyzer.php index cf3d2790..d2487094 100644 --- a/src/NodeAnalyzer/TestsNodeAnalyzer.php +++ b/src/NodeAnalyzer/TestsNodeAnalyzer.php @@ -132,12 +132,16 @@ private function isInTestsNamespace(ClassReflection $classReflection): bool */ public function isPHPUnitMethodCallNames(Node $node, array $names): bool { - if (! $this->isPHPUnitTestCaseCall($node)) { + if (! $node instanceof MethodCall && ! $node instanceof StaticCall) { return false; } - /** @var MethodCall|StaticCall $node */ - return $this->nodeNameResolver->isNames($node->name, $names); + // cheap name check first - a non-matching call bails before any type inference + if (! $this->nodeNameResolver->isNames($node->name, $names)) { + return false; + } + + return $this->isPHPUnitTestCaseCall($node); } public function isPHPUnitTestCaseCall(Node $node): bool