2009/02 10
今天对几种缓存存储器进行了对比。发现最快的存储还是硬盘。当然,硬盘是很强悍的硬盘: SAS 15K。

对比数据:

Mysql:5.8483519554138
Memcached: 2.5365290641785
File: 0.48778581619263

测试环境:
Mysql5 局域网
Memcached 局域网
File 本机器
PHP5.1.6 测试代码使用PHP运行。均为原生函数 mysql_query, memcache_get, file_get_contents

测试循环次数:
10K

机器配置: CPU 2.8x4
内存 8G
硬盘 SAS 15K


如果不需要考虑多服务器共享, 还是file cache来得方便来得快。
补上测试代码
测试代码如下,头部有mysql和memcache的连接工作,略去。
        echo "Mysql Primary:";
        $startTime = microtime(1);
        
        for($i=0; $i<10000; $i++) {
                $result = mysql_query("select * from Cache Where ID = ".$i);
                $thread = mysql_fetch_row($result);
        }
        echo microtime(true) - $startTime;
        
        echo "\n";
        
        echo "Memcached: ";
        
        $startTime = microtime(1);
        for($i=0;$i<10000;$i++) {
        if(! $thread = memcache_get($memcache_obj, 'sdfsdafsdaf'.$i)) {
        //echo 'Nerver got here';
        $result = mysql_query("select * from Cache Where ID = ".$i);
        $thread = mysql_fetch_row($result);
        if($thread == null) {
        $thread = 'Missing';
        }
        memcache_set($memcache_obj, 'sdfsdafsdaf'.$i, $thread, null, 30);
        }
        }
        echo microtime(true) - $startTime;
        
        echo "\n";
        
        echo "File: ";
        $startTime = microtime(1);
        for($i=0;$i<10000;$i++) {
                if(! $thread = unserialize(file_get_contents('cache/'.'sdfsdafsdaf'.$i . '.cache'))) {
                        //echo 'Nerver got here';
                        $result = mysql_query("select * from Cache Where ID = ".$i);
                        $thread = mysql_fetch_row($result);
                        if($thread == null) {
                                $thread = 'Missing';
                        }
                        file_put_contents('cache/'.'sdfsdafsdaf'.$i . '.cache', serialize($thread));
                }
        }
        
        
        echo microtime(true) - $startTime;
        
        echo "\n";
Defined tags for this entry: ,

Posted by rollenc

Last modified on 2009-12-04 23:36