From 21f0b3950b07691054901c243abcc2c8ae76305d Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 19 Oct 2026 00:10:45 +0200 Subject: [PATCH] [bench] Add FileInfoMatcher::matchPattern realpath benchmark --- bench/file_info_matcher_bench.php | 79 +++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 bench/file_info_matcher_bench.php diff --git a/bench/file_info_matcher_bench.php b/bench/file_info_matcher_bench.php new file mode 100644 index 00000000000..39747b883ba --- /dev/null +++ b/bench/file_info_matcher_bench.php @@ -0,0 +1,79 @@ + is_file($filePath) +)); + +// wildcard patterns whose realpath() is always false (nonexistent dirs) - the uncached hot case +$filePatterns = [ + '*/__no_such_dir_a__/*', + '*/__no_such_dir_b__/*', + '*/__no_such_dir_c__/*', +]; + +$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 ($fileInfoMatcher, $filePaths, $filePatterns): void { + foreach ($filePaths as $filePath) { + $fileInfoMatcher->matchPattern($filePath, $filePatterns); + } +}; + +$measure = static function () use ($run, $cpuSeconds): array { + // warm up autoloading once, excluded from timing + $run(); + + $cpuStart = $cpuSeconds(); + $wallStart = hrtime(true); + + for ($repeat = 0; $repeat < REPEAT; ++$repeat) { + $run(); + } + + return [ + 'cpu' => $cpuSeconds() - $cpuStart, + 'wall' => (hrtime(true) - $wallStart) / 1_000_000_000, + ]; +}; + +$result = $measure(); + +printf('FileInfoMatcher::matchPattern benchmark%s', PHP_EOL); +printf(' files=%d patterns=%d repeat=%d%s', count($filePaths), count($filePatterns), REPEAT, PHP_EOL); +printf('%s', PHP_EOL); +printf(' matchPattern cpu=%.4fs wall=%.4fs%s', $result['cpu'], $result['wall'], PHP_EOL);