linux单目录文件不要过多,多少是过多?

我们都知道一个文件夹文件不宜过多,文件过多,会导致性能下降,那到底怎样算过多呢?

今天板子决定蹂躏下自己的新mac

因本文旨在讨论相同状态下,性能随单目录文件数量变化而发生的变化,所以未做其他特别设置

准备:
Mac Pro (ssd)一台 (安装ubuntu 12.04 虚拟机 LNMP 环境)
5个文件夹,各分别包含1w、5w、10w、50w、100w个同样内容小文件
压测命令:ab -n 10000 -c 100 ‘http://s1.localhost.com/files.php?num=xxx’

files.php:

$num = empty($_GET['num']) ? 10000 : $_GET['num'];
echo file_get_contents("/..filepath../$num/file_".rand(1,$num));

以下是测试结果:

继续阅读

分析pdf文件总页数+fgets()函数无法识别换行符问题

有个小需求,批量分析一批pdf文件的总页数,写(找)了个小函数,如下:


function getPageTotal($path){
    if (!$fp = @fopen($path, 'r')) {
        return false;
    }
    $max=0;
    while(!feof($fp)) {
        $line = fgets($fp,255);
        if (preg_match('/\/Count [0-9]+/', $line, $matches)){
            preg_match('/[0-9]+/',$matches[0], $matches2);
            if ($max<$matches2[0]) $max=$matches2[0];
        }
    }
    fclose($fp);
    return $max;
}

实际使用中,发现有极个别文件识别失败,取样分析后,发现出问题的文件都是mac格式的,那自然就是fgets对mac换行符的识别问题了

查了下手册: fgets

Note: If PHP is not properly recognizing the line endings when reading files either on or created by a Macintosh computer, enabling the auto_detect_line_endings run-time configuration option may help resolve the problem.

auto_detect_line_endings 这个运行时配置到是从没注意过,ok、稍作修改


function getPageTotal($path){
    ini_set("auto_detect_line_endings",true);

    if (!$fp = @fopen($path, 'r')) {
        return false;
    }
    $max=0;
    while(!feof($fp)) {
        $line = fgets($fp,255);
        if (preg_match('/\/Count [0-9]+/', $line, $matches)){
            preg_match('/[0-9]+/',$matches[0], $matches2);
            if ($max<$matches2[0]) $max=$matches2[0];
        }
    }
    fclose($fp);
    return $max;
}

问题解决!

关于auto-detect-line-endings的解释:

When turned on, PHP will examine the data read by fgets() and file() to see if it is using Unix, MS-Dos or Macintosh line-ending conventions.

This enables PHP to interoperate with Macintosh systems, but defaults to Off, as there is a very small performance penalty when detecting the EOL conventions for the first line, and also because people using carriage-returns as item separators under Unix systems would experience non-backwards-compatible behaviour.

Note: This configuration option was introduced in PHP 4.3.0