From b7924d09acc85d4458eae7d5eb6c3e3c8d103dc5 Mon Sep 17 00:00:00 2001 From: filesite Date: Thu, 17 Mar 2022 12:54:39 +0800 Subject: [PATCH] add scan time cost --- lib/DirScanner.php | 24 ++++++++++++++++-------- test/DirScannerTest.php | 8 +++++--- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/DirScanner.php b/lib/DirScanner.php index 7513565..702db33 100644 --- a/lib/DirScanner.php +++ b/lib/DirScanner.php @@ -31,6 +31,7 @@ Class DirScanner { ]; private $rootDir; //当前扫描的根目录 private $scanningDirLevel = 0; //当前扫描的目录深度 + private $scanStartTime = 0; //扫描开始时间,单位:秒 private $scanResults = []; //目录扫描结果 @@ -68,7 +69,7 @@ Class DirScanner { 'm3u8', //视频 ]; - public $scanTimeCost = 0; //上一次目录扫描耗时,单位:微秒 + public $scanTimeCost = 0; //上一次目录扫描耗时,单位:毫秒 //解析描述文件内容 @@ -130,7 +131,7 @@ Class DirScanner { 'id' => $id, 'directory' => basename($realpath), 'realpath' => $realpath, - 'path' => $this->getDirPath($realpath), + 'path' => $this->getDirPath($id), 'files' => $files, ]; @@ -163,7 +164,7 @@ Class DirScanner { 'ctime' => $fstat['ctime'], ], 'realpath' => $realpath, - 'path' => $this->getFilePath( $this->getDirectoryName($pathinfo['dirname'], $dirLevel), $pathinfo['filename'], $extension ), + 'path' => $this->getFilePath( $id, $this->getDirectoryName($pathinfo['dirname'], $dirLevel), $pathinfo['filename'], $extension ), ]; if ($extension == 'url') { @@ -262,7 +263,7 @@ Class DirScanner { } //根据文件生成相对路径 - protected function getFilePath($directory, $filename, $extension) { + protected function getFilePath($id, $directory, $filename, $extension) { if (empty($directory)) { $directory = '/'; } @@ -291,9 +292,9 @@ Class DirScanner { if (!empty($path) && in_array($extension, ['md', 'url', 'm3u8'])) { if ($this->nginxSecureOn && $extension == 'm3u8') { $path = $this->getSecureLink($path); - $path = "{$path}&file=" . urlencode($filename); + $path = "{$path}&id={$id}"; }else { - $path = "{$path}?file=" . urlencode($filename); + $path = "{$path}?id={$id}"; } }else if (!empty($path) && $this->nginxSecureOn) { $path = $this->getSecureLink($path); @@ -303,8 +304,8 @@ Class DirScanner { } //根据目录生成相对路径 - protected function getDirPath($realpath) { - return '/list/?dir=' . urlencode(basename($realpath)); + protected function getDirPath($id) { + return "/list/?id={$id}"; } @@ -408,6 +409,10 @@ Class DirScanner { //扫描目录获取目录和文件列表,支持指定目录扫描深度(目录级数) public function scan($dir, $levels = 3) { + if (empty($this->scanStartTime)) { + $this->scanStartTime = microtime(true); + } + $tree = array(); $ignore_files = array('.', '..'); @@ -457,6 +462,9 @@ Class DirScanner { } } + $time = microtime(true); + $this->scanTimeCost = $this->scanStartTime > 0 ? ceil( ($time - $this->scanStartTime)*1000 ) : 0; + return $tree; } diff --git a/test/DirScannerTest.php b/test/DirScannerTest.php index 31c5120..ff02788 100644 --- a/test/DirScannerTest.php +++ b/test/DirScannerTest.php @@ -67,10 +67,12 @@ $scanner = new DirScannerTest(); //$dirTree = $scanner->scan('./'); //$dirTree = $scanner->scan(__DIR__); $dirTree = $scanner->scan(__DIR__ . '/', 3); +echo "Time cost: {$scanner->scanTimeCost} ms\n"; +echo "\n"; print_r($dirTree); echo "\n"; echo "\n"; -$scanResults = $scanner->getScanResults(); -print_r($scanResults); -echo "\n"; +//$scanResults = $scanner->getScanResults(); +//print_r($scanResults); +//echo "\n";